java - Spring HATEOAS 和 HAL : Change array name in _embedded

标签 java spring spring-hateoas

我正在尝试使用 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”数组的名称。理想情况下,它应该说“砖 block ”。我该如何改变它?

这是产生输出的 Controller :

@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/,repository 在 database/,BrickDomain 在 domain/。

谢谢!

最佳答案

只需使用 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 { … }

关于java - Spring HATEOAS 和 HAL : Change array name in _embedded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28834796/

相关文章:

java - Spring Data Rest/Spring Hateoas 自定义 Controller - PersistentEntityResourceAssembler

java - activemq-all "5.15.3"不适用于 Spring 5

java - 如何从 Java 中的 for 循环返回多个字符串?

java - 调用函数时 JNI C++ android 应用程序崩溃

java - AuthenticationProvider authenticate 在 IE 中调用两次,登录失败

rest - HATEOAS 链接到带有可选请求参数的方法

java - PersonResource 与 spring-hateoas 中的 Person 是一样的吗

java else if.不知道哪里出了问题

spring - 尝试使用 hibernate 适配器设置 spring 和 jpa 时出现 Autowiring 异常

java - 使用Spring-XD将数据加载到Hadoop 2.2