DTO has only null with GET request params, but not POST #RequestBody - spring

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

The problem is that you are missing setter for the field.
public void setA(String a) {
this.a = a;
}
should fix it.

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

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

Related

Spring MVC GetMapping does not take effect

I'm trying Spring MVC,I tried #RequestMapping and #GetMampping annotation. There is something wired that #GetMapping doest not take effect.
Below is the code:
#Controller
#RequestMapping(path="/service")
public class ServiceEntry {
#RequestMapping(path="/hello")
#ResponseBody
public String mainPage(){
return "Hello,Information";
}
#RequestMapping(path="/hello2", method = RequestMethod.GET)
#ResponseBody
public String page2(){
return "hello2!";
}
#GetMapping(path="/test1")
#ResponseBody
public String page_test1(){
return "Get method 1";
}
#GetMapping(path="/test2")
#ResponseBody
public String page_test2(){
return "Get method 2";
}
}
While I visit /service/hello or /service/hello2 it works, but /service/test1 or /service/test2 does not work, please help me, thanks!
In RequestMapping you can use parameter path but not in
GetMapping. You can do like this: GetMapping("/test1") or
GetMapping(value="/test1")
Use value instead path. EG: #GetMapping(value="/test2")

Spring REST: DTO as parameter - values are not urldecoded

I have a REST controller with a RequestMapping that looks like this:
#RequestMapping(method = RequestMethod.GET)
public List<MyDTO> search(SearchParameters searchParameters) {
// ...
}
and call it like that: /data/search?name=some%20value&....
searchParameters is populated, but the values are not being urldecoded. So instead of setting searchParameter's attribute name to "some value" it is "some%20value". How can I instruct Spring to urldecode these values?
One possible solution is to use a Map and have their names stored statically in a class, like :
#RequestMapping(method = RequestMethod.GET)
public List<MyDTO> search(#RequestParam Map<String,String> parameters) {
String name = parameters.get(SearchParameters.NAME);
// ...
}
or use the Map to build the Object SearchParameters:
#RequestMapping(method = RequestMethod.GET)
public List<MyDTO> search(#RequestParam Map<String,String> parameters) {
SeachParameters searchParameters = new SearchParameters(parameters);
// ...
}
Okay, the problem was not actually the decoding, but the encoding. It was encoded in a test case, although it wouldn't have needed to be. So the URL looking like /data/search?name=some%20value should really have been /data/search?name=some value with actual spaces.

Spring MVC parse web url Object

I have a GET request in the format below
http://www.example.com/companies?filters=%7B%22q%22%3A%22aaa%22%7D
After decode it is
filters={"q":"aaa"}
I have created an Object named Filters as below
public class Filters {
private String q;
//getter setter....
}
and in my controller
#RequestMapping(method = RequestMethod.GET)
public List<CompanyDTO> getCompanies(Filters filters) {
filters.getQ();
//do things
}
However, the filters.getQ() is null.
Am I doing something incorrect here?
You need to associate the request parameter to the method argument. Add #RequestParam to your method i.e.
#RequestMapping(method = RequestMethod.GET)
public List<CompanyDTO> getCompanies(#RequestParam(value="filters") Filters filters) {
filters.getQ();
//do things
}
Instead of #RequestParam, use #RequestBody
Instead of String filters=%7B%22q%22%3A%22aaa%22%7D, pass JSON object as parameter http://www.example.com/companies?filters={"q":"aaa"}

Bind map and list in spring mvc

Controller:
#ResponseBody
#RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(StressTest stressTest) {
//dosomething
return "ok";
}
Bind Class:
Class StressTest {
private Map<String,List<String>> methods;
private String serviceName;
...
}
Question is:
How to write html form,my http post package :
methods['GetSome'].[0].value=ds&methods['GetSome'].[1].value=asaaaaa&serviceName=aa
But bind fail in springmvc,List in methods always empty
Posted data are not correct. Get rid of the .value part
Try to produce this:
methods['GetSome'].[0]=ds&methods['GetSome'].[1]=asaaaaa&serviceName=aa
Use methods['GetSome'][0]=ds. If use List ,use methods['GetSome'][0].someMember.

Request parameters in spring

I need to take two parameters in my spring controller.
http://mydomain.com/myapp/getDetails?Id=13&subId=431
I have controller which will return Json for this request.
#RequestMapping(value = "/getDetails", method = RequestMethod.GET,params = "id,subId", produces="application/json")
#ResponseBody
public MyBean getsubIds(#RequestParam String id, #RequestParam String subId) {
return MyBean
}
I am getting 400 for when i tried to invoke the URL. Any thoughts on this?
I was able to get it with one parameter.
Try specifying which parameter in the query string should match the parameter in the method like so:
public MyBean getsubIds(#RequestParam("id") String id, #RequestParam("subId") String subId) {
If the code is being compiled without the parameter names, Spring can have trouble figuring out which is which.
As for me it works (by calling: http://www.example.com/getDetails?id=10&subId=15):
#RequestMapping(value = "/getDetails", method = RequestMethod.GET, produces="application/json")
#ResponseBody
public MyBean getsubIds(#RequestParam("id") String id, #RequestParam("subId") String subId) {
return new MyBean();
}
P.S. Assuming you have class MyBean.

Resources