Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have this method that consumes "application/x-www-form-urlencoded", this is the endpoint for one of my forms in the frontend.
@PostMapping(path = "/register", consumes = "application/x-www-form-urlencoded")
public ResponseEntity<User> register(User user) {
User registered = userService.register(user);
return ResponseEntity
.created(
ServletUriComponentsBuilder.fromCurrentRequest().path("/register").buildAndExpand(user).toUri()
.body(registered);
I want to overload this method with another one thath consumes "application/json", so its easy for me to test the endpoint, this method will look like this:
@PostMapping(path = "/register", consumes = "application/json")
public ResponseEntity<User> register(@RequestBody User user) {
User registered = userService.register(user);
return ResponseEntity
.created(
ServletUriComponentsBuilder.fromCurrentRequest().path("/register").buildAndExpand(user).toUri()
.body(registered);
Note that this one has the @RequestBody annotation.
How can I do this?
Here your method has not the same @PostMapping annotation too.
Seems not possible to do it as you want.
The best is to change the name of your controller method to rely on the functionality it deserves, so when you read it, you understand it fastly.
register
your html form becomes registerForm
register
your User object from json becomes registerUser
You can write a unit test like that : https://stackoverflow.com/a/35855593/390462
Usually we are writing hundreds of unit tests when developing a new application / software. It's not complexifying the job to change to a new meaningfull name.
The problem with overloading in your case is that the Java method signatures are the same.
Both have public ResponseEntity<User> register(User user)
As annotations do not change the signature, your current code won't compile.
However, it is enough to rename just one of the methods and the following code works totally fine:
@PostMapping(path = "/register", consumes = "application/x-www-form-urlencoded")
public ResponseEntity<User> register(User user) {
User registered = userService.register(user);
return ResponseEntity
.created(
ServletUriComponentsBuilder.fromCurrentRequest().path("/register").buildAndExpand(user).toUri())
.body(registered);
@PostMapping(path = "/register", consumes = "application/json")
public ResponseEntity<User> registerWithJson(@RequestBody User user) {
User registered = userService.register(user);
return ResponseEntity
.created(
ServletUriComponentsBuilder.fromCurrentRequest().path("/register").buildAndExpand(user).toUri()
.body(registered);
If needed, more information about request mappings in Spring controllers can be found in the official documentation:
https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-requestmapping
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.