香草实体用户 -
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;
}
在网址中更改主机名的搜索结果 - 2 _links present
{
"_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
}
}
修改:
当资源被自己访问时,不存在重复链接。但是,当客户端(我的情况下是微服务)使用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上的link to the project。
完全披露:我已经在Spring HATEOAS Github上提出了一个问题。
答案 0 :(得分:0)
首先,你使用spring-data-rest而不仅仅是spring-hateoas。所以你没有资源,也没有控制器。我认为这是一个主要的误解。
我认为您的自定义存储库出了问题。
我会改变两件事:
所以
public interface UserRepository extends JpaRepository<User, Long>, UserRepositoryCustom
和
public class UserRepositoryCustomImpl implements UserRepositoryCustom
为了实现自定义存储库方法,您需要这样做: