자동으로 REST
끝점을 만드는 여러 컨트롤러가 있습니다. 사용자 지정 컨트롤러에서 봄 REST 응답이 다릅니다.
@RepositoryRestResource(collectionResourceRel = "books", path = "books")
public interface BooksRepository extends CrudRepository<Books, Integer> {
public Page<Books> findTopByNameOrderByFilenameDesc(String name);
}
내가 방문 할 때 :
http://localhost:8080/Books
내가 다시 얻을 : 나는 내 자신의 컨트롤러를 만들 때
{
"_embedded": {
"Books": [{
"id": ,
"filename": "Test123",
"name": "test123",
"_links": {
"self": {
"href": "http://localhost:8080/books/123"
},
"Books": {
"href": "http://localhost:8080/books/123"
}
}
}]
},
"_links": {
"self": {
"href": "http://localhost:8080/books"
},
"profile": {
"href": "http://localhost:8080/profile/books"
},
"search": {
"href": "http://localhost:8080/books/search"
},
"page": {
"size": 20,
"totalElements": 81,
"totalPages": 5,
"number": 0
}
}
}
가 :
@Controller
@RequestMapping(value = "/CustomBooks")
public class CustomBooksController {
@Autowired
public CustomBookService customBookService;
@RequestMapping("/search")
@ResponseBody
public Page<Book> search(@RequestParam(value = "q", required = false) String query,
@PageableDefault(page = 0, size = 20) Pageable pageable) {
return customBookService.findAll();
}
}
내가 다시 응답을 보이는 것 자동 생성 된 컨트롤러 응답과 같은 것 :
{
"content": [{
"filename": "Test123",
"name" : "test123"
}],
"totalPages": 5,
"totalElements": 81,
"size": 20,
"number": 0,
}
응답을 자동으로 생성 된 응답처럼 보이게하려면 어떻게해야합니까? 일관성있게 유지하고 싶기 때문에 다른 응답을 위해 코드를 다시 작성할 필요가 없습니다. 내가 다른 방식으로해야할까요?
편집 :이 찾았 Enable HAL serialization in Spring Boot for custom controller method
을하지만, 내가 수 있도록 내 REST 컨트롤러에서 변경해야하는 것을 이해하지 않습니다 PersistentEntityResourceAssembler
. Google에서 PersistentEntityResourceAssembler
을 검색했지만 예제가 없어도 유사한 페이지로 돌아갈 수 있습니다. (또는 예제가 나에게 적합하지 않은 것 같습니다.)
'@ResourceRestContro 러너'? – chrylis
귀하의 질문을 정확하게 이해하고 있는지 잘 모르겠지만 귀하가 찾고있는 것이 HATEOAS라고 생각됩니다. https://spring.io/guides/gs/rest-hateoas/ – Markus
하나는 페이지를 반환합니다. s), 그러나 나는 그것이 오타이라고 생각한다? –