所以我浏览了一些关于使用spring hateoas(使用spring-boot)的文档和示例。因此,通过以下示例,我创建了2个控制器。
以下是第一个片段:
@RestController
@RequestMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(method = RequestMethod.GET)
public HttpEntity<Resources<Resource<UserResponse>>> findAll() {
List<UserResponse> userList = userService.finaAll();
List<Resource<UserResponse>> resources = Lists.newArrayList();
for (UserResponse user : userList) {
Resource<UserResponse> userResource = new Resource<UserResponse>(user);
resources.add(userResource);
userResource.add(linkTo(methodOn(UserController.class).findAll()).withSelfRel());
userResource.add(linkTo(methodOn(UserController.class).findById(user.getId())).withRel("viewUser"));
}
return new ResponseEntity(new Resources(resources), HttpStatus.OK);
}
}
这是UserResponse DTO:
public class UserResponse {
private Long id;
private String firstName;
private String lastName;
private String socialNumber;
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateOfBirth;
}
为了简洁起见,删除了getter和setter,唯一可能值得一提的是@JsonIgnore和@JsonProperty在ID属性的getter / setter上。
现在我得到的回应如下:
{
"_embedded" : {
"userResponses" : [ {
"firstName" : "Brand",
"lastName" : "White",
"socialNumber" : "342asd3423",
"dateOfBirth" : "1987-04-04",
"_links" : {
"self" : {
"href" : "http://localhost:8080/users"
},
"viewUser" : {
"href" : "http://localhost:8080/users/10"
}
}
}, {
"firstName" : "Snow",
"lastName" : "Green",
"socialNumber" : "3423cxvx423",
"dateOfBirth" : "1987-01-12",
"_links" : {
"self" : {
"href" : "http://localhost:8080/users"
},
"viewUser" : {
"href" : "http://localhost:8080/users/11"
}
}
} ]
}
}
问题一:根据http://docs.spring.io/spring-hateoas/docs/current/reference/html/,格式应该是一致的:
链接:[{rel:“self”,href:“http://myhost/people”}]} 在我的例子中,它看起来像“rel”是一个属性。
我有第二个基本上是复制粘贴的控制器:
@RestController
@RequestMapping(value = "/users/{userId}/details", produces = MediaType.APPLICATION_JSON_VALUE)
public class DetailsController {
//... services ...
@RequestMapping(method = RequestMethod.GET)
public HttpEntity<Resources<Resource<DetailsResponse>>> findAllUserDetails(@PathVariable("userId") Long userId) {
Iterable<UserDetails> details = userDetails.findAll();
List<Resource<DetailsResponse>> hyperList = Lists.newArrayList();
for (UserDetails detail : details) {
Resource<DetailsResponse> hyperRes = new Resource<DetailsResponse>(new DetailsResponse(details));
hyperRes.add(linkTo(DetailsController.class, userId).withSelfRel());
hyperRes.add(linkTo(DetailsController.class, userId).slash("/" + detail.getId()).withRel("viewDetail"));
hyperRes.add(linkTo(DetailsController.class, userId).slash("/" + detail.getId()).withRel("updateDetail"));
hyperList.add(hyperRes);
}
return new ResponseEntity(hyperList, HttpStatus.OK);
}
}
这个单一方法会产生这样的响应:
{
"lifespanDays": 60,
"status": "deceased",
"dateBorn": [
2015,
4,
5
],
"dateDied": [
2015,
6,
4
],
"links": [
{
"rel": "self",
"href": "http://localhost:8080/users/10/details"
},
{
"rel": "viewDetail",
"href": "http://localhost:8080/users/10/details/30"
},
{
"rel": "updateDetail",
"href": "http://localhost:8080/users/10/details/30"
}
]
}
第二个控制器的DTO:
public class UserDetailResponse {
private Long lifespanDays;
private String status;
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateBorn;
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateDied;
}
我不知道JSON的格式是否正确?尽管在类型上指定的产品MediaType是两者的application / json,但我也不明白为什么它们不同。另外,UserDetail响应中的日期格式也发生了变化....
两个控制器在相同的包下都被注释相同。这是开箱即用的Spring-Boot,包含一系列stater依赖项:
compile('org.springframework.boot:spring-boot-starter-web:1.2.2.RELEASE')
compile('org.springframework.hateoas:spring-hateoas:0.17.0.RELEASE')
compile('org.springframework.boot:spring-boot-starter-data-rest:1.2.2.RELEASE') { transitive = true; }
compile('com.google.code.gson:gson:2.3.1');
compile('org.springframework.boot:spring-boot-starter-data-jpa:1.2.2.RELEASE') { transitive = true; };
compile('com.google.guava:guava:18.0')
compile('commons-beanutils:commons-beanutils:1.9.2')
runtime('org.hsqldb:hsqldb:2.3.2');
答案 0 :(得分:2)
问题一:
Spring Boot中响应的默认格式是HAL,这就是你得到的。确实链接的序列化方式与您预期的不同。 Spring HATEOAS为Jackson
注册了一个负责该模块的模块。顺便说一下:文档没有明确说明链接的呈现。
问题二:
OP在评论中回答。
FWIW:两个回答都是正确的JSON。