Reputation: 51
Im working with java spring mvc and angular js.
I created a rest service:
@RestController
public class UserApiController {
@Autowired
private UserService userService;
@RequestMapping(value = "/users/createUser", method = RequestMethod.POST)
public @ResponseBody void addUser(@RequestBody UserRequestDTO newUser) {
userService.addUser(newUser);
}
And my angular controller like this:
var newUser = { surname : "orozco", name: "daniel", password: "pepe", email:"[email protected]" };
$http.post(getCompletePath("users/createUser"), JSON.stringify(newUser))
.success(function () {
alert("ok");
}).error(function () {
});
My UserRequestDTO
public class UserRequestDTO {
private String email;
private String password;
private String name;
private String surname;
+getters and setters
It return the following error: 415 (Unsupported Media Type).
If I send a string o no parameters, it works. So, the problem is in the parameters
Upvotes: 2
Views: 1494
Reputation: 51
I forget to include org.codehaus.jackson in pom.xml. It fixed the issue
Upvotes: 1
Reputation: 3073
This could be because of the Content-Type header, try to include it explicitly as follows,
var req = {
method: 'POST',
url: getCompletePath("users/createUser"),
headers: {
'Content-Type': 'application/json'
},
data: {surname : "orozco", name: "daniel", password: "pepe", email:"[email protected]" },
}
$http(req).success(function(){...}).error(function(){...});
Upvotes: 0
Reputation: 5441
I don't know AngularJS but try to add Content-Type in your AngularJS code. It should look something like this (according the spec https://docs.angularjs.org/api/ng/service/$http) :
var newUser = {
surname : "orozco",
name: "daniel",
password: "pepe",
email:"[email protected]" };
var req = {
method: 'POST',
url: getCompletePath("users/createUser"),
headers: {'Content-Type': 'application/json'},
data: newUser};
$http(req)
.success(function () {alert("ok");})
.error(function () {});
Upvotes: 0