我正在使用Spring Boot和HATEOAS来构建REST API,当我的API返回一个集合时,它被包装在一个"_embedded"属性中,如下所示:
{
"_links":{
"self":{
"href":"http://localhost:8080/technologies"
}
},
"_embedded":{
"technologies":[
{
"id":1,
"description":"A",
"_links":{
"self":{
"href":"http://localhost:8080/technologies/1"
}
}
},
{
"id":2,
"description":"B",
"_links":{
"self":{
"href":"http://localhost:8080/technologies/2"
}
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我希望响应是这样的:
{
"_links":{
"self":{
"href":"http://localhost:8080/technologies"
}
},
"technologies":[
{
"id":1,
"description":"A",
"_links":{
"self":{
"href":"http://localhost:8080/technologies/1"
}
}
},
{
"id":2,
"description":"B",
"_links":{
"self":{
"href":"http://localhost:8080/technologies/2"
}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的技术控制器:
@RestController
@ExposesResourceFor(Technology.class)
@RequestMapping(value = "/technologies")
public class TechnologiesController {
...
@ResquestMapping(method = RequestMethod.GET, produces = "application/vnd.xpto-technologies.text+json")
public Resources<Resource<Technology>> getAllTechnologies() …Run Code Online (Sandbox Code Playgroud) 我有一个带有请求参数的方法,并且正在尝试从另一个资源链接到此方法。我希望链接是这样的:
"rel":{
"href":".../resources{?param}",
"templated":true
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下操作但未成功:
//First attempt
resources.add(linkTo(methodOn(Controller.class).method(null)).withRel("rel")
//Second attempt
resources.add(linkTo(methodOn(Controller.class).method("{parameter}")).withRel("rel")
//Third attempt
resources.add(entityLinks.linkToCollectionResource(LinkedResource.class).withRel("rel");
Run Code Online (Sandbox Code Playgroud)