Hi SO.
I have a weird problem that I've been struggling with all day long.
I have my Angular JS module (App), and a controller. In that controller, I inject a factory, which handles my $http.post method. (Which i call "doSearch"). So far so good.
On the back-end I have a spring-mvc rest api class. It looks like this:
@Controller
@RequestMapping("/api/filesearch")
public class FileSearchAPI {
@RequestMapping(value = "/search", method = RequestMethod.POST)
@ResponseBody
public ResultDTO doSearch(@RequestBody NewPerfectSearchDTO searchDTO) {
System.out.println(searchDTO.getIdentifier());
SolrService solrService = new SolrService();
//return solrService.query(searchDTO);
return new ResultDTO();
}
}
And this is the function that sends some JSON, that will be mapped to a JAVA POJO/DTO:
doSearch: function(searchParams) {
$http.post("/np/api/filesearch/search", JSON.stringify({identifier: "apa", inputRows: []})).success(function(response) {
console.log(response);
return response;
}).error(function(data, status, headers, config) {
console.log(status, headers, config);
});
}
Take note of the payload (Yes, it's static for this example):
JSON.stringify({identifier: "apa", inputRows: []})
This maps perfectly fine in my DTO. The fields are correct, and variable inputRows
is an empty, initialized array.
HOWEVER when I look at the payload (Using firebug or the chrome inspector) , sent by the client, to the API,
it looks like this: {"identifier":"apa","inputRows":"[]"}
Did you notice the ""
around my JSON array?
{"identifier":"apa","inputRows":"[]"}
^ ^
| |
Yeah... That's the problem I'm having.
When I send that request, the server simply replies with a HTTP 400
, malformed syntax (Tomcat/Spring error page), the request doesn't even invoke the APIController on the back-end.
So, what I've tried so far (without success):
- I have tried without using
JSON.stringify()
on the payload, with the same result - I have tried specifying the headers in my APIController (No use, it doesn't even execute that code before throwing the error)
- I have tried using a
Java.util.List
instead of an array in the DTO
So I'm pretty much stuck here. Any help would be appreciated.