1

I'm trying to get my query params in a DTO like in this question but my DTO has always null value.

Is there anything wrong in my code ? I made it as simple as possible.

Queries:

GET http://localhost:8080/api/test?a=azaz => null

POST http://localhost:8080/api/test with {"a":"azaz"} => "azaz"

Controller with a GET and a POST:

@RestController
@RequestMapping(path = {"/api"}, produces = APPLICATION_JSON_VALUE)
public class MyController {

    // GET: dto NOT populated from query params "?a=azaz"
    @RequestMapping(method = GET, path = "test")
    public @ResponseBody String test(TestDto testDto){
        return testDto.toString(); // null
    }

    // POST: dto WELL populated from body json {"a"="azaz"}
    @RequestMapping(method = POST, path = "test")
    public @ResponseBody String postTest(@RequestBody TestDto testDto){
        return testDto.toString(); // "azaz"
    }

}

DTO:

public class TestDto {
    public String a;

    @Override
    public String toString() {
        return a;
    }
}

Thanks !

Full Spring boot sample to illustrate it

Community
  • 1
  • 1
Pleymor
  • 2,611
  • 1
  • 32
  • 44

3 Answers3

1

The problem is that you are missing setter for the field.

 public void setA(String a) {
    this.a = a;
}

should fix it.

Janar
  • 2,623
  • 1
  • 22
  • 32
  • created a branch to see the fix : https://github.com/pleymor/test-request-params-to-dto/tree/fix-request-params – Pleymor Sep 21 '16 at 08:46
0

I'm assuming that you have done required configuration like having Jackson mapper in the class path, consume json attribute, getter and setter in DTO classes etc.

One thing missed here is, in RequestMapping use value attribute instead of path attribute as shown below

    @RequestMapping(method = POST, value= "/test", consumes="application/json")
    public @ResponseBody String postTest(@RequestBody TestDto testDto){
        return testDto.toString(); 
    }

And, make sure that you set content-type="application/json" while sending the request

Ravindra Devadiga
  • 692
  • 1
  • 6
  • 14
  • This method (POST) works perfectly. It's the other (GET) which doesn't populate `testDto` – Pleymor Sep 19 '16 at 07:35
  • In GET request you cannot pass the json value. You have to use either PathParameter or RequestParameter – Ravindra Devadiga Sep 19 '16 at 07:50
  • I don't intend to pass any json value in my GET request, but I try to do like here : http://stackoverflow.com/questions/16942193/spring-mvc-complex-object-as-get-requestparam – Pleymor Sep 19 '16 at 07:58
  • I was able to pass the values in the URL as you said. Only change I have is in **@RequestMapping** I have used **value** element to set the URL but you have used **path** element. Could you confirm which request mapping being used? – Ravindra Devadiga Sep 20 '16 at 07:49
  • I'm using `org.springframework.web.bind.annotation.RequestMapping`. I just added a repo in github to test the behavior :) – Pleymor Sep 21 '16 at 08:39
0

I think what you are trying to do is not possible. To access the query Parameter you have to use @RequestParam("a"). Then you just get the String. To get your object this way you have to pass json as Parameter. a={"a":"azaz"}

Kind regards

David Ibl
  • 901
  • 5
  • 13
  • According to this question it seems possible : http://stackoverflow.com/questions/16942193/spring-mvc-complex-object-as-get-requestparam – Pleymor Sep 19 '16 at 07:57