Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - Post Mapping

Is there a way that when you have a code:

@PostMapping("/test")
public boolean test(@RequestBody String data) {
    //code and stuff
}

You could send information in the post request url? for example

localhost:8080/test?username=username
like image 634
rainbowdog Avatar asked Jun 10 '26 16:06

rainbowdog


1 Answers

Yes, just use @RequestParam:

@PostMapping("/test")
public boolean test(@RequestParam String username, @RequestBody String data) {
    //code and stuff
}

In the reference documentation we can read the following:

Supported for annotated handler methods in Spring MVC and Spring WebFlux as follows:

  • In Spring MVC, "request parameters" map to query parameters, form data, and parts in multipart requests. This is because the Servlet API combines query parameters and form data into a single map called "parameters", and that includes automatic parsing of the request body.
  • In Spring WebFlux, "request parameters" map to query parameters only. To work with all 3, query, form data, and multipart data, you can use data binding to a command object annotated with ModelAttribute.
like image 64
João Dias Avatar answered Jun 12 '26 07:06

João Dias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!