[英]Unsupported Media Type error in browser console when sending json using angularjs to spring controller
Here is my controller function 这是我的控制器功能
@RequestMapping(value = "/logInChecker", method = RequestMethod.POST, consumes = {"application/json"})
public @ResponseBody String logInCheckerFn(@RequestBody UserLogData userLogData){
Integer userAuthFlag = goAnalyserModel.checkUserAuth(userLogData);
return userAuthFlag.toString();
}
My Bean class 我的豆类
public class UserLogData { 公共类UserLogData {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
} }
My html file with angularjs function 我的带有angularjs函数的html文件
<!DOCTYPE html>
<html lang="en" ng-app="nameAppIndexPage">
<head>
<meta charset="utf-8">
<title>Go Analyser - Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- Le styles -->
<link href="assets/css/bootstrap.css" rel="stylesheet">
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
</style>
<link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
</head>
<body ng-controller="nameController">
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">Go Analyser</a>
</div>
</div>
</div>
<div class="container">
<!-- Example row of columns -->
<div class="row">
<div class=" margin_alignment"></div>
<div class="span4"></div>
<div class="span4">
<form class="form-signin">
<label for="exampleInputEmail1">Email address</label>
<input type="text" class="input-block-level" placeholder="Email address" ng-model="userName">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="input-block-level" placeholder="Password" ng-model="password">
<!-- <center><button class="btn btn-large" type="submit">Login</button></center>-->
<button type="submit" ng-click='checkLogin()'>login</button>
</form>
</div>
<div class="span4"></div>
</div>
<div class="hr_space"></div>
<footer>
</footer>
</div>
<script src="assets/js/jquery.js"></script>
<script src="assets/js/angular.js"></script>
<script>
var myApp = angular.module('nameAppIndexPage',[]);
myApp.controller('nameController',function($http,$scope){
$scope.checkLogin = function(){
alert("inside checklogin()");
var userName = $scope.userName;
var password = $scope.password;
var dataToSend = {
"userName" : userName,
"password" : password
};
console.log(dataToSend);
alert("after data to send");
$http.post('logInChecker',dataToSend).success(function(data){
if(data == 1){
alert("inside loginSuccess");
}else{
alert("username and password mismatch");
//write function to show incorrect password
}
}).error(function(data){
alert("error in post" + JSON.stringify({data: data}));
});
}
});
</script>
</body>
</html>
I keep getting unsupported media error in browser console. 我在浏览器控制台中不断收到不受支持的媒体错误。 The angularjs function is not getting the request through to the spring controller.
angularjs函数未将请求传递到spring控制器。 But every thing seems to be fine.
但是一切似乎都很好。
This message happens either when the request cannot be converted to a java object, or when a java object cannot be converted to response. 当无法将请求转换为Java对象或无法将Java对象转换为响应时,将发生此消息。
As you've explained, you have the former case. 正如您所解释的,您有前一种情况。 Few things that you should check:
您应该检查的几件事:
check that you have the <mvc:annotation-driven />
in your servlet configuration 检查servlet配置中是否具有
<mvc:annotation-driven />
check that you have jackson dependencies on your classpath, for spring 4.x you should have the jackson 2.x version, for spring 3.x you should use jackson 1.9 检查您的类路径上是否具有杰克逊依赖项,对于spring 4.x,您应该具有jackson 2.x版本,对于spring 3.x,您应该使用jackson 1.9
to ensure that your response is also properly converted you should, besides having the proper dependencies, ensure that 为了确保您的回复也得到了正确的转换,除了具有适当的依赖关系之外,您还应确保
application/json
or that the RequestMapping annotation of your handler method has the attribute produces = {"application/json"}
application/json
发送给Accept头,或者您的处理程序方法的RequestMapping批注的属性为produces = {"application/json"}
Add @JsonProperty("**") (**Name of the property) to your bean class. 将@JsonProperty(“ **”)(**属性名称)添加到您的bean类中。 Check in the browser's developer tool type of JSON post messages.
检入浏览器的JSON帖子消息的开发人员工具类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.