HTTP status 406 -Not Acceptable Spring mvc

≯℡__Kan透↙ 提交于 2021-01-29 08:30:07

问题


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.

This is a controller code

@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;
    }

This is angular js code

<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>

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.


回答1:


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"


来源:https://stackoverflow.com/questions/53513644/http-status-406-not-acceptable-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!