[SPRING] 봄 데이터 나머지 저장소 메서드를 호출하면 링크가 반환되지 않습니다.
SPRING봄 데이터 나머지 저장소 메서드를 호출하면 링크가 반환되지 않습니다.
나는 "ClientRepository"라는 저장소를 가지고있다 :
public interface ClientRepository extends PagingAndSortingRepository<Client, Long> {
}
http : // localhost : 8080 / clients / 1을 요청하면 서버가 응답합니다.
{
"algorithmId" : 1,
"lastNameTxt" : "***",
"firstNameTxt" : "**",
"middleNameTxt" : "**",
"_links" : {
"self" : {
"href" : "http://localhost:8080/clients/1121495168"
},
"client" : {
"href" : "http://localhost:8080/clients/1121495168"
}
}
}
응답에 예상대로 링크가 있습니다.
다른 컨트롤러에서 저장소 상속 된 메소드 findOne을 호출 할 때
@RestController
public class SearchRestController {
@Autowired
public SearchRestController(ClientRepository clientRepository) {
this.clientRepository = clientRepository;
}
@RequestMapping(value = "/search", method = RequestMethod.GET)
Client readAgreement(@RequestParam(value = "query") String query,
@RequestParam(value = "category") String category) {
return clientRepository.findOne(Long.parseLong(query));
}
}
그것은 반응한다.
{
"algorithmId" : 1,
"lastNameTxt" : "***",
"firstNameTxt" : "**",
"middleNameTxt" : "**"
}
왜 응답에 두 번째 경우의 링크가 포함되어 있지 않습니까? Spring이 응답에 추가하도록하려면 어떻게해야할까요?
해결법
-
==============================
1.HATEOAS 기능은 @RepositoryRestResource로 주석 처리 된 Spring 데이터 jpa 저장소에 대해서만 사용할 수 있습니다. 그러면 자동으로 나머지 끝점이 노출되고 링크가 추가됩니다.
HATEOAS 기능은 @RepositoryRestResource로 주석 처리 된 Spring 데이터 jpa 저장소에 대해서만 사용할 수 있습니다. 그러면 자동으로 나머지 끝점이 노출되고 링크가 추가됩니다.
컨트롤러에서 리파지토리를 사용하면 객체를 얻게되고 jackson mapper는이를 json에 매핑합니다.
Spring MVC 컨트롤러를 사용할 때 링크를 추가하려면 여기를 살펴보십시오.
-
==============================
2.Spring은 클라이언트가 반환하도록 지시 한 것을 반환하기 때문에 : 클라이언트.
Spring은 클라이언트가 반환하도록 지시 한 것을 반환하기 때문에 : 클라이언트.
컨트롤러 메소드에서 Resource
를 빌드하고 리턴해야합니다. 코드를 기반으로 다음과 같이하면 원하는 것을 얻을 수 있습니다.
@RequestMapping(value = "/search", method = RequestMethod.GET) Client readAgreement(@RequestParam(value = "query") String query, @RequestParam(value = "category") String category) { Client client = clientRepository.findOne(Long.parseLong(query)); BasicLinkBuilder builder = BasicLinkBuilder.linkToCurrentMapping() .slash("clients") .slash(client.getId()); return new Resource<>(client, builder.withSelfRel(), builder.withRel("client")); }
이것에서 위로, 나는 또한 당신을 건의 할 것입니다 :
그게 당신에게 뭔가를 줄 것입니다 :
@RepositoryRestController @RequestMapping("/clients") @ResponseBody public class SearchRestController { @Autowired private ClientRepository clientRepository; @RequestMapping(value = "/search", method = RequestMethod.GET) Client readAgreement(@RequestParam(value = "query") String query, @RequestParam(value = "category") String category) { Client client = clientRepository.findOne(Long.parseLong(query)); ControllerLinkBuilder builder = linkTo(SearchRestController.class).slash(client.getId()); return new Resource<>(client, builder.withSelfRel(), builder.withRel("client")); } }
from https://stackoverflow.com/questions/36335951/calling-spring-data-rest-repository-method-doesnt-return-links by cc-by-sa and MIT license