-3

I created PatchMapping in Spring boot When I want to modified and I use method Put it

====== error : Uncaught Exception : class org.springframework.web.HttpRequestMethodNotSupportedException : Request method 'PUT' not supported ==================

If I use Method patch it working normal.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100

2 Answers2

0

If you want to invoke a service by doing PUT verb, you need to add spring-boot annotation:

    @RequestMapping(path = "/yourURL", method = RequestMethod.PUT)

PATCH is not the same as PUT. You can find more info on:

What is the difference between PUT, POST and PATCH?

Nacho Soriano
  • 589
  • 5
  • 15
  • Exception tells us `Request method 'PUT' not supported`. Not related with `POST` or `PATCH`. – Ataur Rahman Munna Apr 02 '18 at 10:58
  • OK. If you need to do an update, you need to declare service as method=RequestMethod.PUT, instead of POST. Annotation @RequestBody will be necessary too on the input parameter that contains the values to update. – Nacho Soriano Apr 02 '18 at 11:05
  • Thank you but when i use patch working normal but i want to handle when use method put , now error { "status": 500, "message": "internal server error" } – Sean Meng Kong Apr 02 '18 at 11:05
  • i want handle it and return 400 . how to catch exception method in restcontroller in spring – Sean Meng Kong Apr 02 '18 at 11:06
  • You need one class that implements ErrorController. @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity processValidationError(MethodArgumentNotValidException ex) { RestInvalidParameter error = new RestInvalidParameter(ex); LOGGER.log(Level.WARNING, "Error: {0}", error); return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST); } – Nacho Soriano Apr 02 '18 at 11:10
  • @NachoSoriano, Then update your answer for fellow users. – Ataur Rahman Munna Apr 02 '18 at 11:11
  • sorry i'm not clear about that can you give me simple and another reference . thank you for help – Sean Meng Kong Apr 02 '18 at 11:16
  • @SeanMengKong All these comments are symptomatic of the question being poorly asked. Fix the question please. – Mark Schultheiss Apr 02 '18 at 11:16
0

If you want to catch the exception, create one class that implements ErrorController:

@Controller
@ControllerAdvice
public class RestErrorHandler implements ErrorController {

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseEntity<RestInvalidParameter> processValidationError(MethodArgumentNotValidException ex) {
        return new ResponseEntity<>(ex, HttpStatus.BAD_REQUEST);
    }
}
Nacho Soriano
  • 589
  • 5
  • 15
  • @Nacho It is not necessary to implements `ErrorController` for the class `RestErrorHandler`. – Ataur Rahman Munna Apr 03 '18 at 04:47
  • @ExceptionHandler(Exception.class) public void methodNotAllowed(Exception ex) throws ResourceNotFoundException, URISyntaxException, ResourceViolationException,ResourceNotFoundException ,ResourceNotFoundException{ throw ex; } – Sean Meng Kong Apr 03 '18 at 06:25
  • please solve ResourceNotFoundException, URISyntaxException, ResourceViolationException,ResourceNotFoundException ,ResourceNotFoundException that is class extend from exception but i don't know which class have error when i use exception . – Sean Meng Kong Apr 03 '18 at 06:31
  • @Nacho any idea please – Sean Meng Kong Apr 03 '18 at 08:11
  • You can set collection to annotation @ExceptionHandler({ResourceNotFoundException.class, URISyntaxException.class, ResourceViolationException.class}, for example, and inside the method, use "instanceof" in order to throw distinct HttpStatus for each exception. – Nacho Soriano Apr 03 '18 at 08:23
  • @SeanMengKong, could you confirm if my last comment solves your problem? – Nacho Soriano Apr 03 '18 at 13:14
  • @NachoSoriano Thank so much for your help sir, but another one i want handle only HttpRequestMethodNotSupportedException and another exception i want to ignore without handler. what should i do ? if have any comment please advice thank you. – Sean Meng Kong Apr 06 '18 at 03:09