-1

I am writing a Spring RESTful service which consumes JSON and performs some action. As the request contains a large number of parameters, I thought of mapping my request parameters to Java object using Spring's Jackson mapping.

My POJO

public class RequestInput {

 private int id;
 private String name;
 // parameters follow


 // getter and setter
}

My controller

@Controller
public class RequestController{

   @RequestMapping(method=RequestMethod.POST, value="/rest/postRequest")
   public void handleRequest(@RequestBody RequestInput input){
          // code follows
   }

}

Things work fine when the data is posted as

{"id" : 1, "name" : "ABCD"}

but when data is posted as

{"id" : 1, "first_name" : "ABCD"}

value of name in object is being returned as NULL.

Can you please help me in understanding how I can map first_name in request to name param in Java POJO

amdixon
  • 3,814
  • 8
  • 25
  • 34
Abhi
  • 13
  • 1
  • 2
  • 1
    How would Jackson know, if you wont tell it, what to do. Guessing, that "name" and "first_name" should map to "name" is a bit too hard. – Thomas Junk Apr 23 '15 at 11:32
  • @ThomasJunk solution suggested by wsl below works. JsonProperty annotation informs the framework which input key to map to which param in pojo – Abhi Apr 26 '15 at 15:39

1 Answers1

5

You can use @JsonProperty. Annotate your RequestInput class:

public class RequestInput {
    private int id;
    @JsonProperty("first_name")
    private String name;
}
beerbajay
  • 19,652
  • 6
  • 58
  • 75
wsl
  • 8,875
  • 1
  • 20
  • 24
  • If everything is ok please check it as accepted answer, will help others who have same poblem :-) – wsl Apr 27 '15 at 07:04