[英]Spring HATEOAS & HAL: Change array name in _embedded
I'm trying to build a HAL-compliant REST API with Spring HATEOAS. 我正在尝试使用Spring HATEOAS构建符合HAL的REST API。
After some fiddling I managed to get to work mostly like expected. 一些摆弄后,我设法大多工作像预期。 The (sample) output looks like this right now:
(样本)输出现在看起来像这样:
{
"_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"
}
}
}
]
}
}
I don't like the "brickDomainList" array's name. 我不喜欢“ brickDomainList”数组的名称。 It should say "bricks", ideally.
理想情况下,它应该说“砖”。 How can I change it?
我该如何更改?
Here's the controller that produces the output: 这是产生输出的控制器:
@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);
}
}
Is there some Annotation or something I can add to change the array's name? 是否有一些注释或可以添加以更改数组名称的内容?
If you want/need to look at the BrickResource class or the Repositories or something look here: https://github.com/ttheuer/sybil/tree/mvctest/src/main/java/org/synyx/sybil 如果您希望/需要查看BrickResource类或存储库或其他内容,请参见此处: https : //github.com/ttheuer/sybil/tree/mvctest/src/main/java/org/synyx/sybil
The BrickResource is in api/resources/, the repository is in database/, and the BrickDomain in domain/. BrickResource位于api / resources /中,存储库位于database /中,而BrickDomain位于domain /中。
Thanks! 谢谢!
Just use Evo Inflector . 只需使用Evo Inflector即可 。 If you have a Maven project then add the dependency
如果您有Maven项目,请添加依赖项
<dependency>
<groupId>org.atteo</groupId>
<artifactId>evo-inflector</artifactId>
<version>1.2</version>
</dependency>
Or you can add @Relation(collectionRelation = "bricks")
to the BrickDomain
class 或者,您可以将
@Relation(collectionRelation = "bricks")
到BrickDomain
类
@Relation(collectionRelation = "bricks")
public class BrickDomain { … }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.