Spring hateoas 如何设置关系并为单个 link 设置数组
Spring hateoas how to set relationship and have array for single link also
有人可以告诉我如何使用 hateoas 获得以下响应吗
{
"ticketID" : 345,
"links": [ {
"rel": "self",
"href": "rest/INL_TM/rest/v1/ticket/345"
} ]
}
但我得到的回复是
{
"ticketID" : 345,
"links": {
"rel": "self",
"href": "rest/INL_TM/rest/v1/ticket/345"
}
}
另外,如何将 rel 设置为自定义值?我想要像 rel = "getTicket"
这样的东西
Greeting.java
public class Greeting extends ResourceSupport {
private final String content;
@JsonCreator
public Greeting(@JsonProperty("content") String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
GreetingController.java
@Controller
public class GreetingController {
private static final String TEMPLATE = "Hello, %s!";
@RequestMapping("/greeting")
@ResponseBody
public HttpEntity<Greeting> greeting(
@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
Greeting greeting = new Greeting(String.format(TEMPLATE, name));
greeting.add(linkTo(methodOn(GreetingController.class).greeting(name)).withSelfRel());
return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
}
}
提前致谢
扩展模型 class 中的 ResourceSupport。
Return 资源而不是 ResponseEntity
GreetingController.java
@Controller
public class GreetingController {
@RequestMapping("/greeting")
@ResponseBody
public Resource<Greeting> greeting(
@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
Resource<Greeting> resourceResponse = new Resource<Greeting>("Some String");
resourceResponse.add(linkTo(methodOn(GreetingController.class).greeting("Some String")).withSelfRel());
return resourceResponse;
}
}
Maven 依赖
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.16.0.RELEASE</version>
</dependency>
有人可以告诉我如何使用 hateoas 获得以下响应吗
{
"ticketID" : 345,
"links": [ {
"rel": "self",
"href": "rest/INL_TM/rest/v1/ticket/345"
} ]
}
但我得到的回复是
{
"ticketID" : 345,
"links": {
"rel": "self",
"href": "rest/INL_TM/rest/v1/ticket/345"
}
}
另外,如何将 rel 设置为自定义值?我想要像 rel = "getTicket"
这样的东西Greeting.java
public class Greeting extends ResourceSupport {
private final String content;
@JsonCreator
public Greeting(@JsonProperty("content") String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
GreetingController.java
@Controller
public class GreetingController {
private static final String TEMPLATE = "Hello, %s!";
@RequestMapping("/greeting")
@ResponseBody
public HttpEntity<Greeting> greeting(
@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
Greeting greeting = new Greeting(String.format(TEMPLATE, name));
greeting.add(linkTo(methodOn(GreetingController.class).greeting(name)).withSelfRel());
return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
}
}
提前致谢
扩展模型 class 中的 ResourceSupport。 Return 资源而不是 ResponseEntity
GreetingController.java
@Controller
public class GreetingController {
@RequestMapping("/greeting")
@ResponseBody
public Resource<Greeting> greeting(
@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
Resource<Greeting> resourceResponse = new Resource<Greeting>("Some String");
resourceResponse.add(linkTo(methodOn(GreetingController.class).greeting("Some String")).withSelfRel());
return resourceResponse;
}
}
Maven 依赖
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.16.0.RELEASE</version>
</dependency>