Prevents HTTP method in spring-data-rest

I am using spring-data-rest.

The following repository is specified:

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

The annotation @RestResource(exported = false)by the method save()causes the framework to return an error 405 Method Not Allowedwhen using the POST, PUT and PATCH methods.

My question is: how can I just return a 405 error for the PUT method while POST and PATCH are still allowed for this repository?

Thanks!

+4
source share
2 answers

@SWiggels Thank you for your answer :) Your solution did not work for me ... PUT is always allowed.

For others, I found this one that worked:

@BasePathAwareController
public class MyEntityController {

    @RequestMapping(value = "/myentity/{id}", method = RequestMethod.PUT)
    public ResponseEntity<?> preventsPut() {
        return new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
    }
}
+2
source

.

@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.

0

Source: https://habr.com/ru/post/1627020/


All Articles