0

My back doesn't receive parameters from a mobile app.

org.springframework.web.bind.MissingServletRequestParameterException Required String parameter 'param1' is not present

With Chrome DevTools, I can see that the mobile app call my back with these parameters :

RequestMethod : POST
Request Payload : param1=toto&param2=titi
Request Headers: Content-Type : application/json

I try to retrieve parameters like this :

public void method (@RequestParam(name = "param1") String param1, @RequestParam(name = "param2") String param2) {
...
}

(I tried with "value" instead of "name")

How can I match the payload that the front sends me, with my back ? The payload is imposed on me, it is my back that must be modified

Previously, the back was in Jersey, with @FormParam annotation. I switch the back to spring framework, with the use of RequestParam...

Lainea
  • 15
  • 5
  • RequestBody & RequestParam both are different https://stackoverflow.com/questions/3337350/learning-springs-requestbody-and-requestparam – Ryuzaki L Aug 18 '23 at 17:19
  • @RyuzakiL Thank you for your helpful answer which help me a lot ! ... or not -_- can you told me HOW I can match this payload with my back ? Without told me some annotations are different... – Lainea Aug 21 '23 at 06:56
  • Request Headers: Content-Type : application/json seems to be an error, payload doesn't contain json – Marc Stroebel Aug 21 '23 at 09:35
  • @MarcStroebel unfortunately, I don't have choice for Content-Type : it's the front code, not mine, I must compose with these content-type... – Lainea Aug 21 '23 at 12:59
  • @Lainea see my updated answer – Marc Stroebel Aug 21 '23 at 13:09

2 Answers2

0

@RequestParamis used for query params, try to catch the raw request body

@RequestMapping(value = "/", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE)
public String myMethod(@RequestBody String param) {
    
}

The frontend app seems broken... payload format param1=toto&param2=titi looks like query params, not a request body... and content type application/json also doesn't match...

Marc Stroebel
  • 2,295
  • 1
  • 12
  • 21
  • try removing the consumes param... – Marc Stroebel Aug 21 '23 at 09:54
  • sorry for my deleted answer. I tried with @RequestBody, with HttpServletRequest... The call my back receive is empty, no parameter is catch... – Lainea Aug 21 '23 at 12:44
  • Thank you for your response, I was surprised to see what I thought were inconsistencies. I'll talk to the mobile app developer... – Lainea Aug 21 '23 at 13:20
0

This post resolve my problem to receive a payload in raw with a spring back : How to access plain json body in Spring rest controller? (HttpEntity for me)

Lainea
  • 15
  • 5