8

Is it possible to map same path (uri) in request mapping for two different post methods, only difference is request body.

Example

  @RequestMapping(value = "/hello", method = RequestMethod.POST)
  public String helloEmployee(@RequestBody Employee employee) {
    return "Hello Employee";
  }

  @RequestMapping(value = "/hello", method = RequestMethod.POST)
  public String helloStudent(@RequestBody Student student) {
    return "Hello Student";
  }
Ravikumar
  • 413
  • 1
  • 7
  • 18

3 Answers3

18

No, you can't give same url in request mapping of post method having different request body type but same media type. Below won't work:

  @PostMapping(path = "/hello", consumes = MediaType.APPLICATION_JSON_VALUE)
  public String hello(@RequestBody Pojo1 val) {
    return "Hello";
  }

  @PostMapping(path = "/hello", consumes = MediaType.APPLICATION_JSON_VALUE)
  public String hello(@RequestBody Pojo2 val) {
    return "Hello";
  }

If you have different media type, then it will. Below will work:

  @PostMapping(path = "/hello", consumes = MediaType.APPLICATION_JSON_VALUE)
  public String hello(@RequestBody Pojo val) {
    return "Hello";
  }

  @PostMapping(path = "/hello", consumes = MediaType.TEXT_PLAIN_VALUE)
  public String hello(@RequestBody String val) {
    return "Hello";
  }

Your RequestMapping should differ on at least one of the conditions; path,method,params,headers,consumes,produces

Sukhpal Singh
  • 2,130
  • 9
  • 20
  • I believe it answered your query. Please read: [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Sukhpal Singh Nov 28 '18 at 13:27
5

I needed the same url post mapping but it was giving me an error so I added different params and it worked for me

//url1 post mapping
@PostMapping(value = "/applicant/{step}", params = "validatedata")

//url2 post mapping
@PostMapping(value = "/applicant/{step}", params = "data")

if any of the below is different(as mentioned by the above answers)then you can have the same URL post mapping path,method,params,headers,consumes,produces

In my case params was diffrent

Stephane
  • 11,836
  • 25
  • 112
  • 175
  • can you pls quote the source for this statement - if any of the below is different(as mentioned by the above answers)then you can have the same URL post mapping path,method,params,headers,consumes,produces I am having different method name but still I believe Spring is causing conflicts and throwing error. So want to verify from some source. – Swapnil Jaju Jun 18 '21 at 03:56
1

Yes you can do that but you need to specify unique parameters signature in RequestMapping annotation:

public class MyController {

@RequestMapping(method = RequestMethod.POST, params = {"!name", "!name2"})
public String action(HttpServletRequest request, HttpServletResponse response){
    // body
}

@RequestMapping(method = RequestMethod.POST, params = "name")
public String action(HttpServletRequest request, HttpServletResponse response,
                        @RequestParam(value = "name", required = true) String name) {
    // body
}

}

`

mwdev
  • 469
  • 3
  • 5