1
votes

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

3
in your SampleController in your method add this annotation @RequestBody on the SampleDTO param - AntJavaDev
I am getting this error Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.sample.dto.Response com.sample.controller.SampleController.getResponse(com.sample.dto.SampleDTO) When I am adding this @RequestBody - mvg
then you are not fixing the json properly from your client / jsp - AntJavaDev

3 Answers

0
votes

When you are setting @JsonProperty("firstname") make sure you are imported the this statement "import com.fasterxml.jackson.annotation.JsonProperty ;". One thing more if you are sending more properties and your bean class does not have that you can set the @JsonIgnoreProperties(ignoreUnknown=true) on the top of the bean name.

You are also missing the @RequestBody annotation. You should take this as (@RequestBody SampleDTO dto)in getResponse method.

0
votes

Simply create a Pojo Java Bean with fields with names that match your request parameters.

Then use this class as an argument for your request handler method (without any additional annotations)

see this

-1
votes

First you need to choose which approach do you need (or want to use) param or model. When you use something like this http://localhost:8080/sample/get?firstName=mvg , you are passing data as request parameters. So you need to use the @RequestParam annotation.

Sample using @RequestParam annotation (use is explained in the docs)

    @RequestMapping(method = RequestMethod.GET)
public Response foo(@RequestParam("firstName") String firstName) {
    Response response = new Response();
    response.setResponseMsg("Hello "+ firstName );
    return response;
}