0

I have some entity:

public class Entity {
    String name;
    String age;
}

and entity's fields come to me as HTTP params:

"name=alex&age=30"

How can i set auto converting HTTP params to my entity's fields with @RequestBody annotation?

I did it properly with JSON in request body, but I cannot do it with normal HTTP params.

What message converter should I use?

coolsv
  • 268
  • 3
  • 17
  • Possible duplicate.There are dozens of examples here.Please check : https://stackoverflow.com/questions/11291933/requestbody-and-responsebody-annotations-in-spring – Tsakiroglou Fotis Feb 06 '19 at 09:30
  • @TsakiroglouFotis they all about binding request body to entity, and not about binding HTTP params to entity – coolsv Feb 06 '19 at 09:31
  • So you need them in the headers and not the body? – Tsakiroglou Fotis Feb 06 '19 at 09:33
  • check this link May be it's help full for you. http://websystique.com/spring-boot/spring-boot-rest-api-example/ – Ng Sharma Feb 06 '19 at 09:34
  • That doesn't look like HTTP params, are you sure they come like that? Usually a request body looks more like "name=alex&age=30" (unless it's JSON of course). – jvdmr Feb 06 '19 at 09:36
  • @jvdmr ye you are correct. I need to bind something like "name=alex&age=30" to my entity. – coolsv Feb 06 '19 at 09:39
  • 1
    @TsakiroglouFotis i was wrong a bit, params come to me in body in format "name=alex&age=30" – coolsv Feb 06 '19 at 09:42
  • @coolsv so you need something equal to @JsonProperty("first-name") ? – Tsakiroglou Fotis Feb 06 '19 at 09:47
  • This question: https://stackoverflow.com/questions/11291933/requestbody-and-responsebody-annotations-in-spring may be helpful to you. – JAlexey Feb 06 '19 at 09:47

1 Answers1

0

Adding consumes = "application/x-www-form-urlencoded" to the parameters of your @RequestMapping (or @PostMapping, ...) annotation should fix it.

jvdmr
  • 685
  • 5
  • 12