繁体   English   中英

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

[英]Prevents HTTP method in spring-data-rest

我正在使用spring-data-rest。

给出以下存储库:

@RepositoryRestResource
public interface MyRepository extends PagingAndSortingRepository<MyEntity, Long> {}

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<Void> 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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM