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

mvg
  • 1,574
  • 4
  • 37
  • 63
  • in your `SampleController` in your method add this annotation @RequestBody on the `SampleDTO` param – AntJavaDev Jun 07 '16 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 – mvg Jun 07 '16 at 10:32
  • then you are not fixing the json properly from your client / jsp – AntJavaDev Jun 07 '16 at 12:40

3 Answers3

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.

kiran
  • 71
  • 3
  • 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 Jun 07 '16 at 10:11
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

Community
  • 1
  • 1
Amin Arab
  • 530
  • 4
  • 19
-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;
}
José Ricardo Pla
  • 1,043
  • 10
  • 16
  • I need to know how do I do this with DTO object? – mvg Jun 07 '16 at 09:47
  • Map it with @Modelattribute - http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-method-args – José Ricardo Pla Jun 07 '16 at 09:48
  • The url is working for me. :(. Try it again, please. – José Ricardo Pla Jun 07 '16 at 09:52
  • How this part "@ModelAttribute("pet") Pet pet" solves my problem?? Can you please elaborate? – mvg Jun 07 '16 at 09:57
  • 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 parameters. So you need to use the @RequestParam annotation. – José Ricardo Pla Jun 07 '16 at 10:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114008/discussion-between-mvg-and-joseripla). – mvg Jun 07 '16 at 10:03