Home > Software design >  How to map form param key with hyphen into Java object field?
How to map form param key with hyphen into Java object field?

Time:04-14

I have a POST request called to my app with Content-Type: application/x-www-form-urlencoded.

The Form Data Payload is: [email protected]&my-token=dBq26TVjg5jy8Q4kUYyZ2DQ

I need to map these 2 params into a Java Object:

public class MyRequest{
    
    private String username;

    private String myToken;

    //getters setters
}

my controller is:

@RequestMapping(value = "user/request", method = RequestMethod.POST)
    public String request(@ModelAttribute(value = "myRequest") MyRequest myRequest, 
Model model, RedirectAttributes redirectAttributes)

I see username is populating but myToken is not.

I can't use @JsonProperty("my-token") because it is not a json, what else can be done to get the token?

CodePudding user response:

instead of assigning the value to an object store the value first in a string objects and then store it into the object.

public String request(@RequestParam("username") String username,@RequestParam("my-token") String myToken, 
Model model, RedirectAttributes redirectAttributes)
  • Related