ssm:接口返回JSONObject,HTTP Status 406

来源:互联网 发布:单例模式 java代码 编辑:程序博客网 时间:2024/06/05 03:50

错误: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not fin

   

 原来的配置文件:

<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.cn.smarthome.controller" />

<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value> 
<!--  <value>text/plain;charset=utf-8</value>
                <value>text/html;charset=utf-8</value>
                <value>text/json;charset=utf-8</value> 
                 <value>application/json;charset=utf-8</value>  -->
</list>
</property>
</bean>

<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean 
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters"> 
<list>
<ref bean="mappingJacksonHttpMessageConverter" /><!-- JSON转换器 -->
</list>
</property>
</bean>

<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

   

<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"  
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <!-- 默认编码 -->
        <property name="defaultEncoding" value="utf-8" />  
        <!-- 文件大小最大值 -->
        <property name="maxUploadSize" value="10485760000" />  
        <!-- 内存中的最大值 -->
        <property name="maxInMemorySize" value="40960" />  
    </bean> 


结论:  添加<mvc:annotation-driven/>  ,现在可以去掉AnnotationMethodHandlerAdapter 这个bean,因为<mvc:annotation-driven/>  会自动配置。
             如果出现中文乱码, 添加编码格式。需要添加的配置如下:

<mvc:annotation-driven> 
<mvc:message-converters>   
<bean class="org.springframework.http.converter.StringHttpMessageConverter">   
<property name="supportedMediaTypes">   
<list>   
<value>text/plain;charset=UTF-8</value>   
<value>text/html;charset=UTF-8</value>   
</list>   
</property>   
</bean>    
</mvc:message-converters>   
</mvc:annotation-driven> 

原创粉丝点击