Issue when mapping GET request parameters with DTO object in Spring Boot Rest
05:12 07 Jun 2016

I have created a Spring Boot with Spring REST application.

This is my controller code.

@RestController
public class SampleController {

  @RequestMapping(value = "/sample/get", method = RequestMethod.GET, produces = "application/json")
  @ResponseBody
  public Response getResponse(SampleDTO dto) {
    Response response = new Response();

    response.setResponseMsg("Hello "+dto.getFirstName());

    return response;
  }
}

This is my SampleDTO

public class SampleDTO {

  @JsonProperty("firstname")
  private String firstName;

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
}

and this is my Response object

public class Response {

  private String responseMsg;

  public String getResponseMsg() {
    return responseMsg;
  }

  public void setResponseMsg(String responseMsg) {
    this.responseMsg = responseMsg;
  }
}

When I try to access service this way

http://localhost:8080/sample/get?firstName=mvg

I am getting this expected output

{"responseMsg":"Hello mvg"}

When I try to access service this way

http://localhost:8080/sample/get?firstname=mvg

I am getting this output

{"responseMsg":"Hello null"}

My question is how do I map 'firstname' in request parameter with 'firstName' of DTO?

Thanks in advance

java spring spring-restcontroller spring-rest