香草实体用户 -
public class User implements Serializable {
private static final long serialVersionUID = 262950482349139355L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "FIRST_NAME", nullable = false, unique = false)
@Convert(converter = NameConverter.class)
private String firstName;
@Column(name = "LAST_NAME", nullable = false, unique = false)
@Convert(converter = NameConverter.class)
private String lastName;
@Column(name = "PHONE_NUM", nullable = false, unique = false)
@Convert(converter = PhoneNumberConverter.class)
private String phoneNum;
@Column(name = "EMAIL", nullable = true, unique = false)
@Convert(converter = OptionalStringConverter.class)
private Optional<String> email;
}
URL 中主机名已更改的搜索结果 - 存在 2 个 _links
{
"_links" : {
"self" : {
"href" : "http://hostname/users/search/findByLastName?lastName=doe{&page,size,sort}",
"templated" : true
}
},
"_embedded" : {
"users" : [ {
"firstName" : "John",
"lastName" : "Doe",
"phoneNum" : "111-111-1111",
"email" : null,
"_links" : { },
"_embedded" : { },
"_links" : {
"self" : {
"href" : "http://hostname/users/1",
"templated" : false
}
}
} ]
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
编辑:
单独访问资源时,不存在重复链接。但是,当客户端(在我的例子中是微服务)使用 a 发出请求RestTemplate
时,响应返回 2 _links
,如上所示。
@Override
public Collection<Long> getUserIdsByFirstName(String firstName) {
String findByFirstNameUri = UriComponentsBuilder.fromUriString(userServiceUrl)
.path("/users/search/findByFirstName").queryParam("firstName", firstName).toUriString();
// This is where ResponseEntity.body would have the 2 _links, if ResponseEntity<String> were used. I used a custom object to weed that out.
ResponseEntity<UserSearchResult> userSearchResult = restTemplate.exchange(findByFirstNameUri, GET, dummyEntity,
UserSearchResult.class);
...
}
我正在使用 Spring Data JPA、Spring Data Rest、Spring HATEOAS 和 Spring Cloud。这是我的 Github 上的项目链接。
完全披露:我在 Spring HATEOAS Github 上提交了一个问题。