Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use spring mvc3 @ResponseBody had 415 Unsupported Media Type why?

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>

and code is:

 @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 is:

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

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

I ajax submit the from but firefox view is 415 Unsupported Media Type

lib had jackson-mapper-asl-1.5.6.jar jackson-core-asl-1.5.6.jar spring version is 3.03

Why submit not success?

like image 457
EdwardLau Avatar asked Nov 20 '10 20:11

EdwardLau


1 Answers

Try putting an explicit Accept Header of : application/json.

The mvc-showcase sample spring application has a message converters example which reads json and xml to the screen with JQuery for Ajax request, you can try and use that example to model your code.

like image 167
Biju Kunjummen Avatar answered Nov 15 '22 09:11

Biju Kunjummen