问题
I'm using Spring Boot, Spring Data REST, Spring HATEOAS, Hibernate, JPA.
I'm using extensively Spring Data REST in my application and I expose all Repositories of my entities. Unfortunately there are some particular cases that are not so easy to manage. One of that is this:
I've a custom controller:
@Api(tags = "CreditTransfer Entity")
@RepositoryRestController
@RequestMapping(path = "/api/v1")
@PreAuthorize("isAuthenticated()")
public class CreditTransferController {
@RequestMapping(method = RequestMethod.GET, path = "/creditTransfers/{id}")
public ResponseEntity<?> findAll(@PathVariable("id") long id, HttpServletRequest request, Locale locale,
PersistentEntityResourceAssembler resourceAssembler) {
//my code
}
@RequestMapping(method = RequestMethod.DELETE, path = "/creditTransfers/{id}")
public ResponseEntity<?> deleteMovement(@PathVariable("id") long id, HttpServletRequest request, Locale locale,
PersistentEntityResourceAssembler resourceAssembler) {
//my code
}
The problem here is that overriding these endpoints, I am hiding the /search endpoint that Spring Data REST create. And that is very important to me.
I did't find any smart way to make this to work without interfer with defaults endpoints provided from Spring Data REST.
Is there a way to solve my problem?
======================================================================
A small enhancement is using a mapping like this:
@RequestMapping(method = RequestMethod.DELETE, path = "/creditTransfers/{id:[0-9]+}")
In this way my controller doesn't catch the url localhost:8080/api/v1/creditTransfers/search
but still, if I override just the DELETE method, when I try to GET localhost:8080/api/v1/creditTransfers
I've the error Request method 'GET' not supported
. Seems my controller override ALL methods for a specific path and not just the one I set.
回答1:
As explained in this thread, and originally here, if you annotate your controller with @RepositoryRestController
AND @RequestMapping
, you lose the benefit of Spring generating the "default" REST endpoints for you.
The only way to prevent this, i.e. to get both the automatically generated endpoints and your custom endpoints, is to use method-level requestmapping only:
@Api(tags = "CreditTransfer Entity")
@RepositoryRestController
@PreAuthorize("isAuthenticated()")
public class CreditTransferController {
@GetMapping("/api/v1/creditTransfers/{id}")
public ResponseEntity<?> findAll(@PathVariable("id") long id, HttpServletRequest request, Locale locale,
PersistentEntityResourceAssembler resourceAssembler) {
//my code
}
@DeleteMapping("/api/v1/creditTransfers/{id}")
public ResponseEntity<?> deleteMovement(@PathVariable("id") long id, HttpServletRequest request, Locale locale,
PersistentEntityResourceAssembler resourceAssembler) {
//my code
}
}
Side note: I also used the mapping shortcuts GetMapping and DeleteMapping.
回答2:
You can add the
@RestResource(exported=false)
over the methods that you want to override in the repository.
来源:https://stackoverflow.com/questions/47557276/custom-controller-with-spring-data-rest-hide-default-endpoints