[英]Angularjs Java Rest Service 415 Unsupported Media Type
Im working with java spring mvc and angular js. 我正在使用java spring mvc和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:"dani@dani.com" };
$http.post(getCompletePath("users/createUser"), JSON.stringify(newUser))
.success(function () {
alert("ok");
}).error(function () {
});
My UserRequestDTO 我的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). 它返回以下错误:415(不支持的媒体类型)。
If I send a string o no parameters, it works. 如果我发送的字符串不带任何参数,它将起作用。 So, the problem is in the parameters
所以,问题出在参数
I don't know AngularJS but try to add Content-Type in your AngularJS code. 我不知道AngularJS,但尝试在您的AngularJS代码中添加Content-Type。 It should look something like this (according the spec https://docs.angularjs.org/api/ng/service/ $http) :
它应该看起来像这样(根据规范https://docs.angularjs.org/api/ng/service/ $ http):
var newUser = {
surname : "orozco",
name: "daniel",
password: "pepe",
email:"dani@dani.com" };
var req = {
method: 'POST',
url: getCompletePath("users/createUser"),
headers: {'Content-Type': 'application/json'},
data: newUser};
$http(req)
.success(function () {alert("ok");})
.error(function () {});
This could be because of the Content-Type header, try to include it explicitly as follows, 这可能是由于Content-Type标头所致,请尝试按如下所示明确包含它,
var req = {
method: 'POST',
url: getCompletePath("users/createUser"),
headers: {
'Content-Type': 'application/json'
},
data: {surname : "orozco", name: "daniel", password: "pepe", email:"dani@dani.com" },
}
$http(req).success(function(){...}).error(function(){...});
I forget to include org.codehaus.jackson in pom.xml. 我忘记将org.codehaus.jackson包含在pom.xml中。 It fixed the issue
它解决了这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.