在spring-data-rest中阻止HTTP方法

我正在使用spring-data-rest。

给出以下存储库:

@RepositoryRestResource public interface MyRepository extends PagingAndSortingRepository {} 

save()方法上的注释@RestResource(exported = false)使框架在使用POST,PUT和PATCH方法时返回405 Method Not Allowed错误。

我的问题:如何在PUT方法上返回405错误,而此存储库仍然允许POST和PATCH?

谢谢 !

@SWiggels感谢您的回复:)您的解决方案对我不起作用…… PUT始终是允许的。

对于其他人,我发现这个有效:

 @BasePathAwareController public class MyEntityController { @RequestMapping(value = "/myentity/{id}", method = RequestMethod.PUT) public ResponseEntity preventsPut() { return new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED); } } 

您可以在options-probe的响应中添加允许的方法。

 @RequestMapping(method = RequestMethod.OPTIONS) ResponseEntity getProposalsOptions() { HttpHeaders headers = new HttpHeaders(); headers.setAllow(new HashSet<>(Arrays.asList(OPTIONS, PATCH, POST))); return new ResponseEntity<>(headers, HttpStatus.NO_CONTENT); } 

这仅允许Options, Patch, Post作为请求方法。 对于其他所有尝试过的方法,您会收到HTTP-405-Error。