HTTP状态406 –不可接受 根据请求中接收到的主动协商标头字段,目标资源没有用户代理可接受的当前表示形式,并且服务器不愿意提供默认表示形式。
控制器代码
@RequestMapping(value="/welcomes", method = RequestMethod.GET, produces="application/json")
public @ResponseBody List<UserBean> welcome(@ModelAttribute UserBean userBean, HttpServletResponse response)
{
List<UserBean> usernames=new ArrayList<UserBean>();
usernames = retrievedataservice.findAllUsers(userBean);
System.out.println(usernames.size());
return usernames;
}
角度js代码
<script>
var app = angular.module('myApp', []);
app.controller('UserController', function($scope, $http, $location){
$scope.usernames=[];
var url = $location.absUrl() + "welcomes";
$http.get(url).then(function (response)
{
$scope.usernames = response.records;
},function error(response)
{
$scope.postResultMessage = "Error with status: " + response.statusText;
});
});
</script>
<table border="1" width="50%" height="50%">
<tr><th>user_name</th><th>phone</th><th>email</th></tr>
<tr data-ng-repeat="user in usernames">
<td>{{user.username}}</td>
<td>{{user.phone}}</td>
<td>{{user.email}}</td>
</tr>
</table>
如何将数据从spring控制器发送到angular js控制器?
答案 0 :(得分:1)
尝试如下更改方法签名。
@RequestMapping(value="/welcomes", method = RequestMethod.GET,produces={"application/json"})
public @ResponseBody List<UserBean> welcome(UserBean userBean, HttpServletResponse response)
请注意,我删除了@ModelAttribute
并更改了产品。
还要确保您的类路径中有杰克逊或任何其他json库。
如果没有,则在下面添加到pom.xml
<!-- add jackson to support restful API, otherwise the API will return 406 error -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>