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"
}
}
} ]
}
}
しかし、独自の Controller を使用すると、出力は次のようになります。
[ {
"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 Data Rest の依存関係を削除し、spring-hateoas、spring-plugin-core、および json-path (com.jayway.jsonpath) を含めると、機能します..しかし、他のいくつかのエンティティに spring-data-rest を使用したい
2 つの質問:
- Spring Data Rest が含まれている HAL がデフォルトではないのはなぜですか?
- HAL を出力形式として設定するにはどうすればよいですか