HTTP状态406-不可接受的Spring MVC [英] HTTP status 406 -Not Acceptable Spring mvc
问题描述
错误是HTTP状态406 –不可接受根据请求中收到的主动协商标头字段,目标资源没有用户代理可接受的当前表示形式,并且服务器不愿意提供默认表示形式.
Error is HTTP Status 406 – Not Acceptable The target resource does not have a current representation that would be acceptable to the user agent, according to the proactive negotiation header fields received in the request, and the server is unwilling to supply a default representation.
这是一个控制器代码
@RequestMapping(value="/welcomes", method = RequestMethod.GET, headers="Accept=application/json")
public @ResponseBody List<UserBean> welcome(@ModelAttribute UserBean userBean)
{
List<UserBean> usernames = retrievedataservice.findAllUsers();
return usernames;
}
这是有角度的js代码
<body>
<div data-ng-app="myApp" data-ng-controller="UserController">
<table>
<tr><th>user name</th><th>phone</th><th>email</th></tr>
<tr data-ng-repeat="user in usernames">
<td><span data-ng-bind="user.username"></span></td>
<td><span data-ng-bind="user.phone"></span></td>
<td><span data-ng-bind="user.email"></span></td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', ['ngResource']);
app.controller('UserController', ['$scope', '$resource', function($scope,$resource){
function fetchalluser()
{
$scope.usernames=$resource('http://localhost:8080/SpringAngular/welcomes').query(function(data)
{
return data;
});
};
$scope.refresh=function(){
fetchalluser();
};
}]);
</script>
</body>
我调试以下代码,控制器方法"welcome()"返回HTTP状态406-不可接受的错误.我认为有角js和spring MVC集成问题.
I debug the following code, Controller method "welcome()" returns the error of HTTP status 406- Not Acceptable. I think angular js and spring MVC integration problem.
推荐答案
我认为您在响应中缺少内容类型标头.可以这样添加
I think you are missing content-type header in the response. that can be added like this
@RequestMapping(value="/welcomes", method = RequestMethod.GET, produces= MediaType.APPLICATION_JSON)
public @ResponseBody List<UserBean> welcome(@ModelAttribute UserBean userBean){
...
}
或者您也可以指定为字符串
or you can specify as a string also
produces="application/json"
这篇关于HTTP状态406-不可接受的Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!