gpt4 book ai didi

spring - 使用 spring mvc3 @ResponseBody 有 415 Unsupported Media Type 为什么?

转载 作者:行者123 更新时间:2023-12-02 11:43:43 24 4
gpt4 key购买 nike

spring xml:

  <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="atom" value="application/atom+xml" />
<entry key="html" value="text/html" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>

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

<mvc:annotation-driven />

<context:annotation-config />

<context:component-scan base-package="org.lxh"></context:component-scan>

代码是:

 @RequestMapping(value = "/2wt/test.do",method=RequestMethod.POST)
public @ResponseBody
Map<String, ? extends Object> create(
@RequestBody WtStandartype wtStandartype) {
System.out.println(wtStandartype.getId() + "--------");
return Collections.singletonMap("id", wtStandartype.getId());

}

javascript 是:

   jQuery.ajax({
'type': 'POST',
'url': '/2wt/test.do',//'WtStandardTypeList.do?addto=updb',
'contentType': 'application/json',
'data': JSON.stringify(jsonuserinfo),
'dataType': 'json',
'error':function(){
alert(222);
},
'success': function(){
alert(33333);
}
});

web.xml 是:

  <?xml version="1.0"?>
<web-app>
<display-name>jbpm</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/applicationContext.xml
</param-value>
<description>Spring config file locations</description>
</context-param>


<listener>
<listener-class>com.gzgi.framework.context.StartupListener</listener-class>
</listener>

<listener>
<listener-class>com.gzgi.framework.startup.ShutdownListener</listener-class>
</listener>

<listener>
<listener-class>com.gzgi.framework.context.UserOnlineListener</listener-class>
</listener>

<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>

<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter>
<filter-name>compressionFilter</filter-name>
<filter-class>com.gzgi.framework.web.filter.GZIPFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>compressionFilter</filter-name>
<url-pattern>*.css</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>compressionFilter</filter-name>
<url-pattern>*.js</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>compressionFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>com.gzgi.framework.web.SpringDispatcherServlet</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>resteasy</servlet-name>
<servlet-class>com.gzgi.framework.web.SpringDispatcherServlet</servlet-class>
<load-on-startup>6</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>resteasy</servlet-name>
<url-pattern>/rs/*</url-pattern>
</servlet-mapping>


<session-config>
<session-timeout>3600</session-timeout>
</session-config>


</web-app>

我ajax提交来自但firefox View 是415不支持的媒体类型

lib有jackson-mapper-asl-1.5.6.jar jackson-core-asl-1.5.6.jar spring版本是3.03

为什么提交不成功?

最佳答案

尝试放置显式的 Accept header :application/json

mvc-showcase示例 Spring 应用程序有一个消息转换器示例,它使用 JQuery 读取 json 和 xml 到屏幕上进行 Ajax 请求,您可以尝试使用该示例来建模您的代码。

关于spring - 使用 spring mvc3 @ResponseBody 有 415 Unsupported Media Type 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4234740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com