0

I checked with params attribute we can narrow down the request to specific handler in case there are multiple defined for same URI, how can we achieve the same in case of PostMapping.

@PostMapping(value="newUser")
public String addNewUser(@RequestBody User user)
{}

@PostMapping(value="newUser")
public String addAnotherUser(@RequestBody AnotherUser user)
{}

Basically, two different handlers with different input request body type parameter with same URI.

Kshitiz
  • 21
  • 3

1 Answers1

0

you need to tell spring how to route your request. by default spring can't figure out that from the payload. you can implement yours https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/mvc/condition/RequestCondition.html

BUT i think if you need to pass two different payloads to create a user, you should review your API design.

a common practice is to use the API path to be more specific about the resource you want to manipulate. like

/newUser

/newUser/anotherUser or /v2/newUser ...

  • Thanks, was looking for the same URL, but seems like within the same URL mapping can't do for different input data type – Kshitiz Jan 07 '22 at 09:12