Raj
Raj

Reputation: 747

@PutMapping and @PostMapping annotations on same method

I want to apply both Put and Post mapping request to a method as show below. It does work for PUT, but not for POST requests. What am I dong wrong?

@RestController
@RequestMapping("/PQR")
public class XController {

    @PutMapping("xyz")
    @PostMapping("xyz")
    public MyDomainObject createOrUpdateDAO(
            HttpServletRequest request, 
            @RequestBody String body) throws IOException {
        //...
    }
}

When I make a POST request, I get a 405 HTTP status code:

[nio-8080-exec-3] o.s.web.servlet.PageNotFound: Request method 'POST' not supported

If I look at this example, same method has same method is mapped for GET and POST requests.

@RequestMapping(value="/method3", method = { RequestMethod.POST,RequestMethod.GET })
@ResponseBody
public String method3() {
    return "method3";
}

Upvotes: 13

Views: 12122

Answers (1)

ThomasEdwin
ThomasEdwin

Reputation: 2145

Remove @PostMapping and @PutMapping annotations and add method to your @RequestMapping, i.e:

@RequestMapping(value={"/PQR", "xyz"},
    method={RequestMethod.POST,RequestMethod.PUT})

Upvotes: 21

Related Questions