2

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 Commented Jun 7, 2016 at 10:28
  • 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 Commented Jun 7, 2016 at 10:32
  • then you are not fixing the json properly from your client / jsp Commented Jun 7, 2016 at 12:40

3 Answers 3

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
0

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

Comments

-1

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;
}

6 Comments

I need to know how do I do this with DTO object?
The url is working for me. :(. Try it again, please.
How this part "@ModelAttribute("pet") Pet pet" solves my problem?? Can you please elaborate?
First you need to choose which approach do you need (or want to use) param or model. When you use something like this localhost:8080/sample/get?firstName=mvg , you are passing data as parameters. So you need to use the @RequestParam annotation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.