我正在使用spring-boot-1.5.8。我想在我的项目中实现一个超媒体(HATEOAS)链接,并且已经实现了。但是超媒体链接的格式不正确。您可以找到项目here
模型
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Person extends ResourceSupport {
private String firstName;
private String lastName;
private int age;
private String sex;
private String personUid;
private String personId;
}
控制器
@GetMapping(value = "/getAllPersonHal")
public ResponseEntity<List<Person>> getPersonListHal(){
List<PersonDTO> allPersons = personRepository.findAll();
List<Person> personList = new ArrayList<>();
for (PersonDTO personDTO : allPersons) {
Person person = new Person();
modelMapper.map(personDTO,person);
person.add(linkTo(methodOn(PersonController.class).getPersonById(personDTO.getId())).withSelfRel());
person.add(linkTo(methodOn(PersonController.class).getPersonById(personDTO.getId())).withRel("persons"));
person.add(linkTo(methodOn(PersonController.class)
.getPersonById(personDTO.getId()))
.withRel("urn:persons"));
personList.add(person);
}
return new ResponseEntity<>(personList, HttpStatus.OK);
}
点击API时会收到以下响应,
实际输出
[
{
"firstName": "Philip",
"lastName": "webb",
"age": 23,
"sex": "M",
"personUid": "1994",
"personId": "26",
"links": [
{
"rel": "self",
"href": "http://localhost:9086/getPersonById/26"
},
{
"rel": "persons",
"href": "http://localhost:9086/getPersonById/26"
},
{
"rel": "urn:persons",
"href": "http://localhost:9086/getPersonById/26"
}
]
}]
预期产量
[
{
"firstName": "Philip",
"lastName": "webb",
"age": 23,
"sex": "M",
"personUid": "1994",
"personId": "26",
"links": {
"self":{
"href": "http://localhost:9086/getPersonById/26"
},
"persons":{
"href": "http://localhost:9086/getPersonById/26"
},
"urn:persons":{
"href": "http://localhost:9086/getPersonById/26"
}
}
}]
我不确定我应该怎么做才能获得预期的输出。 另外,连同“ href”属性以及链接,我想添加一些有关链接的更多信息,例如类型(GET / POST / PUT),标头,接受类型等。
任何提示或帮助都应该是有意义的。