When I post a JSON string to my spring controller, I get a 400 status error. My method takes in a @RequestBody parameter and when that is removed the method works.
Here is the controller:
@RequestMapping(value = "/events/idp", method = RequestMethod.POST)
public String receiveIDPEvents(@RequestBody Event event) throws JAXBException, JMSException, NamingException {
System.out.println(event);
return "Success!";
}
Here is the JSON being posted:
angular.module('hello', [])
.controller('navigation', function($rootScope, $scope, $http, $location) {
$scope.Event= '{"cause": "test3", "location": "test"}';
$http({
method: 'POST',
url: '/events/idp',
data: $scope.Event,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data) {$scope.name = data;}).error(function(data) {$scope.name = data;});
});
Here is the Event class:
public class Event {
private String cause;
/**
* @return the cause
*/
public String getCause() {
return cause;
}
/**
* @param cause the cause to set
*/
public void setCause(String cause) {
this.cause = cause;
}
/**
* @return the location
*/
public String getLocation() {
return location;
}
/**
* @param location the location to set
*/
public void setLocation(String location) {
this.location = location;
}
private String location;
public Event(String cause, String location) {
this.cause = cause;
this.location = location;
}
public Event() {
}
}
Another thing to note is that when I try to change the content type header to "application/json" I get a 415 unsupported media type error. I've tried a couple dozen solutions, but nothing resolves it.
Also, i've used the below code and the Event object is created perfectly:
ObjectMapper mapper=new ObjectMapper();
String json="{\"cause\": \"test3\", \"location\": \"test\"}";
Event event2 = mapper.readValue(json, Event.class);
Aucun commentaire:
Enregistrer un commentaire