我在项目中使用spring-boot-1.5.8和spring-boot-hateoas。请在下面找到代码
@SpringBootApplication
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class SpringBootK8Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootK8Application.class, args);
}
}
控制器
@GetMapping("/greeting")
public ResponseEntity<Greeting> greeting(@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
Greeting greeting = new Greeting(String.format("Hello, %s!", name));
greeting.add(linkTo(methodOn(HelloController.class).greeting(name)).withSelfRel());
Link link = new Link("http://localhost:8080/something").withRel("ACTIONS");
SuperLink superLink = new SuperLink(link, MediaTypes.HAL_JSON_VALUE,"GET","/v1/books","v1/bookBy");
greeting.add(superLink);
return new ResponseEntity<>(greeting, HttpStatus.OK);
}
实际输出:
{
"content": "Hello, World!",
"_links": {
"self": {
"href": "http://localhost:9086/greeting?name=World"
},
"ACTIONS": {
"href": "http://localhost:8080/something",
"template": "http://localhost:8080/something/v1/books",
"type": "application/hal+json",
"method": "GET",
"describedBy": "http://localhost:8080/something/v1/bookBy"
}
}
}
预期输出:
{
"content": "Hello, World!",
"_links": {
"self": {
"href": "http://localhost:9086/greeting?name=World"
},
"ACTIONS": [
"href": "http://localhost:8080/something",
"template": "http://localhost:8080/something/v1/books",
"type": "application/hal+json",
"method": "GET",
"describedBy": "http://localhost:8080/something/v1/describedBy"
]
}
}
我想将动作显示为数组。如果我在ACTIONS属性中添加多个元素,则它显示数组,但如果我们只有一个元素,则显示为对象。我总是想将该元素显示为数组。任何帮助都是非常可观的。