Spring returns 纯 JSON 资源在包含 spring 数据剩余时不是 HAL 格式
Spring returns Resource in pure JSON not in HAL Format when including spring data rest
当我为我的实体使用默认控制器时,由 Spring Data Rest 提供,一切正常。输出如下所示:
{
"_links" : {
"search" : {
"href" : "http://localhost:8080/users/search"
}
},
"_embedded" : {
"users" : [ {
"firstName" : "Max",
"lastName" : "Mustermann",
"email" : "mail@max-mustermann.de",
"_links" : {
"self" : {
"href" : "http://localhost:8080/users/myadmin"
}
}
} ]
}
}
但如果我使用自己的控制器,输出将如下所示:
[ {
"firstName" : "Max",
"lastName" : "Mustermann",
"email" : "mail@max-mustermann.de",
"links" : [ {
"rel" : "self",
"href" : "http://localhost:8080/user/myadmin"
} ]
} ]
我的控制器
@RestController
@RequestMapping("/user")
@EnableHypermediaSupport(type = {HypermediaType.HAL})
public class UserController {
@Autowired
UserRepository userRepository;
@RequestMapping(method=RequestMethod.GET)
public HttpEntity<List<User>> getUsers(){
ArrayList<User> users = Lists.newArrayList(userRepository.findAll());
for(User user : users){
user.add(linkTo(methodOn(UserController.class).getUser(user.getUsername())).withSelfRel());
}
return new ResponseEntity<List<User>>(users, HttpStatus.OK);
}
@RequestMapping(value="/{username}", method= RequestMethod.GET)
public HttpEntity<User> getUser(@PathVariable String username){
User user = userRepository.findByUsername(username);
user.add(linkTo(methodOn(UserController.class).getUser(user.getUsername())).withSelfRel());
return new ResponseEntity<User>(user, HttpStatus.OK);
}
}
我的用户:
@Entity
@Table(name="users")
public class User extends ResourceSupport{
@Id
private String username;
private String firstName;
private String lastName;
@JsonIgnore
private boolean enabled;
@JsonIgnore
private String password;
@Column(unique = true)
private String email;
public User(){
enabled = false;
}
//Getters and Setters
}
如果我删除 spring 数据剩余依赖项并包括 spring-hateoas、spring-plugin-core 和 json-path (com.jayway.jsonpath ) 它有效.. 但我想对其他一些实体使用 spring-data-rest
两个问题:
- 为什么 HAL 不是包含 spring 剩余数据的默认值?
- 如何将 HAL 设置为输出格式
您需要 return ResourceSupport
的子类型(通常是 Resource
或 Resources
)才能让 HAL Jackson 转换器启动。
此外,@EnableHypermediaSupport
必须用于 JavaConfig 配置 class,而不是控制器。
当我为我的实体使用默认控制器时,由 Spring Data Rest 提供,一切正常。输出如下所示:
{
"_links" : {
"search" : {
"href" : "http://localhost:8080/users/search"
}
},
"_embedded" : {
"users" : [ {
"firstName" : "Max",
"lastName" : "Mustermann",
"email" : "mail@max-mustermann.de",
"_links" : {
"self" : {
"href" : "http://localhost:8080/users/myadmin"
}
}
} ]
}
}
但如果我使用自己的控制器,输出将如下所示:
[ {
"firstName" : "Max",
"lastName" : "Mustermann",
"email" : "mail@max-mustermann.de",
"links" : [ {
"rel" : "self",
"href" : "http://localhost:8080/user/myadmin"
} ]
} ]
我的控制器
@RestController
@RequestMapping("/user")
@EnableHypermediaSupport(type = {HypermediaType.HAL})
public class UserController {
@Autowired
UserRepository userRepository;
@RequestMapping(method=RequestMethod.GET)
public HttpEntity<List<User>> getUsers(){
ArrayList<User> users = Lists.newArrayList(userRepository.findAll());
for(User user : users){
user.add(linkTo(methodOn(UserController.class).getUser(user.getUsername())).withSelfRel());
}
return new ResponseEntity<List<User>>(users, HttpStatus.OK);
}
@RequestMapping(value="/{username}", method= RequestMethod.GET)
public HttpEntity<User> getUser(@PathVariable String username){
User user = userRepository.findByUsername(username);
user.add(linkTo(methodOn(UserController.class).getUser(user.getUsername())).withSelfRel());
return new ResponseEntity<User>(user, HttpStatus.OK);
}
}
我的用户:
@Entity
@Table(name="users")
public class User extends ResourceSupport{
@Id
private String username;
private String firstName;
private String lastName;
@JsonIgnore
private boolean enabled;
@JsonIgnore
private String password;
@Column(unique = true)
private String email;
public User(){
enabled = false;
}
//Getters and Setters
}
如果我删除 spring 数据剩余依赖项并包括 spring-hateoas、spring-plugin-core 和 json-path (com.jayway.jsonpath ) 它有效.. 但我想对其他一些实体使用 spring-data-rest
两个问题:
- 为什么 HAL 不是包含 spring 剩余数据的默认值?
- 如何将 HAL 设置为输出格式
您需要 return ResourceSupport
的子类型(通常是 Resource
或 Resources
)才能让 HAL Jackson 转换器启动。
此外,@EnableHypermediaSupport
必须用于 JavaConfig 配置 class,而不是控制器。