【问题标题】:Spring HATEOAS & HAL: Change array name in _embeddedSpring HATEOAS & HAL:更改 _embedded 中的数组名称
【发布时间】:2015-03-03 14:49:42
【问题描述】:

我正在尝试使用 Spring HATEOAS 构建符合 HAL 的 REST API。

经过一番摆弄,我设法大部分按预期开始工作。 (示例)输出现在看起来像这样:

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/sybil/configuration/bricks"
        }
    },
    "_embedded": {
        "brickDomainList": [
            {
                "hostname": "localhost",
                "port": 4223,
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/sybil/configuration/bricks/localhost"
                    }
                }
            },
            {
                "hostname": "synerforge001",
                "port": 4223,
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/sybil/configuration/bricks/synerforge001"
                    }
                }
            }
        ]
    }
}

我不喜欢“brickDomainList”数组的名称。理想情况下,它应该说“砖”。我怎样才能改变它?

这是产生输出的控制器:

@RestController
@RequestMapping("/configuration/bricks")
public class ConfigurationBricksController {

    private BrickRepository brickRepository;
    private GraphDatabaseService graphDatabaseService;

    @Autowired
    public ConfigurationBricksController(BrickRepository brickRepository, GraphDatabaseService graphDatabaseService) {

        this.brickRepository = brickRepository;
        this.graphDatabaseService = graphDatabaseService;
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, produces = "application/hal+json")
    public Resources<BrickResource> bricks() {

        List<BrickDomain> bricks;
        List<BrickResource> resources = new ArrayList<>();
        List<Link> links = new ArrayList<>();

        Link self = linkTo(ConfigurationBricksController.class).withSelfRel();
        links.add(self);

        try(Transaction tx = graphDatabaseService.beginTx()) { // begin transaction

            // get all Bricks from database and cast them into a list so that they're actually fetched
            bricks = new ArrayList<>(IteratorUtil.asCollection(brickRepository.findAll()));

            // end transaction
            tx.success();
        }

        for (BrickDomain brick : bricks) {
            self = linkTo(methodOn(ConfigurationBricksController.class).brick(brick.getHostname())).withSelfRel();

            BrickResource resource = new BrickResource(brick, self);

            resources.add(resource);
        }

        return new Resources<>(resources, links);
    }
}

是否有一些注释或我可以添加的东西来更改数组的名称?

如果您想/需要查看 BrickResource 类或存储库或其他内容,请查看此处:https://github.com/ttheuer/sybil/tree/mvctest/src/main/java/org/synyx/sybil

BrickResource 在 api/resources/ 中,存储库在 database/ 中,BrickDomain 在 domain/ 中。

谢谢!

【问题讨论】:

    标签: java spring spring-hateoas


    【解决方案1】:

    只需使用Evo Inflector。如果你有一个 Maven 项目然后添加依赖项

    <dependency>
      <groupId>org.atteo</groupId>
      <artifactId>evo-inflector</artifactId>
      <version>1.2</version>
    </dependency>
    

    或者您可以将@Relation(collectionRelation = "bricks") 添加到BrickDomain

    @Relation(collectionRelation = "bricks")
    public class BrickDomain { … }
    

    【讨论】:

    • 太好了,非常感谢,@Relation 注释是我正在寻找的 :) 我会支持你的答案,但我还没有足够的声誉,抱歉 :(跨度>
    • Tobold:为你点赞。 jose_p:您可能还想提及RelProvider 提供程序接口。它允许外部化 rels 的计算,以防使用 @Relation 注释域对象是不可能或不想要的。
    • 不。将依赖项放在我的 .pom 和 @SpringBootApplication 中,甚至注入 EvoReflectorBean ......什么也没发生。是不是少了一些注释???发现总是看到 domainClassResource:[...] 非常令人沮丧
    猜你喜欢
    • 2015-11-30
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多