I found 10 or 20 similar question and I tried many of the answers but none of them seems to work for me..
Here is the angular service code:
app.factory('service',['$http','$q', function($http,$q){
return {
get: function(m){
return $http.post('path/getJSON',m).then(
function(r){
console.log(r.data);
return r.data;
}
);
},
getY: function(m){
return $http({
method : 'POST',
url : 'path/getJSON',
headers: {'Content-Type': 'application/json'},
data : JSON.stringify(m)
}).then(
function(r){
console.log(r.data);
return r.data;
}
);
},
getZ: function(m){
return $http.post('path/getJSON',JSON.stringify(m)).then(
function(r){
console.log(r.data);
return r.data;
}
);
}
};
}]);
I tried to call any of these 3 methods in my controller but the result is the same: 415. I also tried with and without the JSON.stringify() function on any of these
The data (m) is built like this:
m = {param : value, param2 : value2 etc}
Here is the controller method in Java which should handle this POST:
@RequestMapping(value="/getJSON", consumes = {"application/json;charset=UTF-8"},
method=RequestMethod.POST)
@ResponseStatus(value=HttpStatus.OK)
public ResponseEntity<List<SmtElse>>getFlights(@RequestBody CustomJSONObj jsonObj){
HttpHeaders h= new HttpHeaders();
h.add("Access-Control-Allow-Origin", "*");
List<SmtElse> list= new ArrayList<SmtElse>();
System.out.println(jsonObj);
return new ResponseEntity<List<SmtElse>>(list,h, HttpStatus.OK);
}
CustomJSONObj has only String attributes, getters, setters and constructors. I also tried adding a couple attributes in @RequestMapping or in other form.
Sorry for possible duplicate. I know there are big chances, but I couldn't find any answer for my problem.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.4</version>
</dependency>