Spring MVC + AngularJS 415 Unsupported Media Type
The question is very simple and I know that many other questions have been answered, but none worked for me. Using Spring MVC and AngularJS I get UnSupported Media Type for AngularJS 415!
I tried to set the angular header to application / json
I tried with @Consumes
server side annotation
I tried with consumes ="application/json"
I am tired of consumes ="application/application/json;charset=UTF-8'
I tried with consumes ={"application/json","application/xml"}
I also tried to set the property.
I tried to explicitly set the listener content type on the client to exactly match the one on the server, but NOTHING WORKS! here are related questions that haven't helped anyone!
1
2
3
Here is my controller
/**
* Created by adelin.ghanayem@gmail.com
*/
@Controller
@RequestMapping(value = "/administration/places")
public class PlacesController {
private PlacesService service;
@Autowired
public PlacesController(PlacesService service) {
this.service = service;
}
@RequestMapping(method = RequestMethod.POST,consumes = {"application/json;charset=UTF-8"})
public String newPlace(@RequestBody Place places) {
String id = service.addNewPlace(places);
return "/administration/places/" + id;
}
@RequestMapping(value = "/{id}")
public Place getById(@PathVariable String id) {
return new Place();
}
}
And my AngularJS controller
function NewPlacesController(scope, http) {
scope.place = {};
scope.add = function () {
http.post(URLS.addNewPlace, scope.place,{'Content-Type': 'application/json'}).success(function (value) {
console.log("got it !");
}).error(function (value) {
console.log("CUR!");
});
}
}
NewPlacesController['$inject'] = ['$scope', '$http'];
source to share
Try:
@RequestMapping(method = RequestMethod.POST,consumes = {"application/json;charset=UTF-8"}, produces={"application/json;charset=UTF-8"})
public String newPlace(@RequestBody Place places) {
String id = service.addNewPlace(places);
return "/administration/places/" + id;
}
And make sure you include the Jackson Databind library.
source to share