0
votes

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.

1
I think this might answer your question.S. Kostadinov

1 Answers

0
votes

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