【问题标题】:Json/Jackson ResponseEntity 406 Error - spring mvc 4Json/Jackson ResponseEntity 406 错误-spring mvc 4
【发布时间】:2018-01-23 20:30:52
【问题描述】:

对不起我的英语。

我试图解决这个错误,但我不知道会是什么

我是 spring 和 REST 的新手

我正在使用:

*spring mvc 4.0.1 *NetBeans 8 *休眠 *雄猫 8 *甲骨文 *Json / 杰克逊休息 *AngularJS

当我尝试使用 Json 在控制器中获取对象列表时,错误是 406 错误,当我尝试获取简单字符串时,答案是 200(确定)

我尝试在我的 dispatcher-servlet 中添加这一行 >> <mvc:annotation-driven/> 但随后返回:

org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0”的bean时出错:调用init方法失败;嵌套异常是 org.springframework.http.InvalidMediaTypeException: Invalid mime type "String": does not contain '/'

所以,接下来是没有前一行的代码和 406 错误:

调度程序-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
            <beans xmlns="http://www.springframework.org/schema/beans"
                   xmlns:context="http://www.springframework.org/schema/context"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:p="http://www.springframework.org/schema/p"
                   xmlns:aop="http://www.springframework.org/schema/aop"
                   xmlns:tx="http://www.springframework.org/schema/tx"
                   xmlns:mvc="http://www.springframework.org/schema/mvc"
                   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

                <context:component-scan base-package="controller"/>

                <context:annotation-config />

                <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
                  <property name="prefix" value="/WEB-INF/jsp/"/>
                  <property name="suffix" value=".jsp"/>
                </bean>

                <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
                    <property name="prefixJson" value="true"/>
                </bean>

PerfilesController.java

...

    @ResponseStatus(value=HttpStatus.OK)
    @RequestMapping(value= "perfiles.serv", method = RequestMethod.GET, headers="Accept=*/*", produces= {"application/xml", "application/json", "String"})    
    public @ResponseBody ResponseEntity<List<Perfiles>> getAllProfiles() {
        PerfilesService pService = new  PerfilesService();
            List<Perfiles> list = pService.getAllProfles();
            return new ResponseEntity<List<Perfiles>>(list, HttpStatus.OK);
    }

...

我用 Angular JS 调用

应用程序服务

        svservicios.service('PerfilesService',['$http', function ($http) {

        function getPerfiles(pageNumber,size) {
            pageNumber = pageNumber > 0?pageNumber - 1:0;
            return $http({
              method: 'GET',
              headers: {
               'Content-Type': 'application/json','Accept': 'application/json, */*'
             },
                url: 'perfiles.serv?page='+pageNumber+'&size='+size
            });
        }
        return {
            getPerfiles: getPerfiles
        };
    }]);

观点:

<div class="edit-contenedor">

<div class = "btnAddPerfil">

    <md-button href="crearP.html" class = "col-md-12 md-raised">Agregar Perfil</md-button>
</div>

<div>
    <div ng-controller="AdminPerfilesController">
        <div ui-grid="gridOptions" class="grid-ap" ui-grid-pagination>
        </div>
    </div>
</div>

谢谢

【问题讨论】:

    标签: java angularjs json spring-mvc jackson


    【解决方案1】:

    我在 dispatcher-servlet 中解决了这个问题:

    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter"/>
            </list>
        </property>
    </bean>
    

    感谢spring mvc not returning json content - error 406

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      相关资源
      最近更新 更多