Post and Get for the Same Url - Controller - Spring

Spring - is it possible to give same url (path) in request mapping for two different post methods?

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

Spring - two different POST methods with the same URL but different produced content type

@RequiredArgsConstructor
@RestController
public class SearchController {

@PostMapping(value = "/search", produces = {APPLICATION_JSON})
public SearchResponse search(@Valid @RequestBody SearchRequest request,
Pageable pageable) {
}

@PostMapping(value = "/search", produces = {TEXT_CSV})
public ResponseEntity<Resource> export(@Valid @RequestBody SearchRequest request, Pageable pageable) throws IOException {

}


}

RestController with GET + POST on same method?

The best solution to your problem seems to be something like this:

@RestController
public class MyServlet {
@RequestMapping(value = "test", method = {RequestMethod.GET})
public void testGet(@Valid @RequestParam("foo") String foo) {
doStuff(foo)
}
@RequestMapping(value = "test", method = {RequestMethod.POST})
public void testPost(@Valid @RequestBody MyReq req) {
doStuff(req.getFoo());
}
}

You can process the request data in different ways depending on how you receive it and call the same method to do the business logic.

Is it possible to distinguish 2 POST methods with same URL in Spring MVC?

You can use @RequestBody annotation:

@PutMapping("/users/{id}")
public void update(@PathVariable Long id, @RequestBody User user)
{
service.update(user);
}

Handle same URL in different controllers based on parameters - Spring MVC

Controller 1

@RequestMapping(value= {"/myurl"}, params = { "a" })
public ModelAndView handleMyURL()

Controller 2

@RequestMapping(value= {"/myurl"}, params = { "b" })
public ModelAndView handleMyURL()

Take a look at chapter 4 of this post for more detail

Two @GetMapping with same URL but different parameters

Something like this:

@RequestMapping(value = "/welcomeWithParam", params = "user")
public String welcome91(@RequestParam String user, Model model) {
// ...
}

@RequestMapping(value = "/welcomeWithParam", params = {"user","age"})
public ModelAndView welcome92(@RequestParam String user, @RequestParam int age, Model model) {
// ...
}

Spring MVC - How do I call the same URL with different mapping annotations

Browsers only support GET and POST as http request methods. The solution is to send your form with the POST method and inject a hidden field inside the same html form called _method with your desired method as a value, in your case here, it is just DELETE. For the case of POST, just write your form as usual.

Example :

<form:form action="${pageContext.request.contextPath}/students/${tempStudentInternship.student.username}/internships/${tempStudentInternship.internship.id}" method="POST">
<input type="hidden" name="_method" value="DELETE"/>
<input type="submit" value="Dismiss" />
</form:form>

Please, have a look at this answer for creating the spring bean and then applying the mentioned form attribute inside spring:form html forms.



Related Topics



Leave a reply



Submit