Announcement Announcement Module
Collapse
No announcement yet.
HTTP 4115: Unsupported Media Type (application/json) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HTTP 4115: Unsupported Media Type (application/json)

    I'm using Spring MVC that takes JSON request and return JSON response. I'm running into a weird bug where the first request will result in a HTTP 415 Unsupported Media Type, but ever subsequent request works fine.

    Application context (json-context.xml)
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
    		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    		xmlns:flex="http://www.springframework.org/schema/flex"
    		xmlns:context="http://www.springframework.org/schema/context"
    		xmlns:mvc="http://www.springframework.org/schema/mvc"
    		xsi:schemaLocation="http://www.springframework.org/schema/beans 
    			 	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    			 	http://www.springframework.org/schema/context 
            		http://www.springframework.org/schema/context/spring-context-3.0.xsd
            		http://www.springframework.org/schema/mvc
            		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
        <bean id="jacksonMessageConverter" 
                class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <ref bean="jacksonMessageConverter" />
                </list>
            </property>
        </bean>         
    
    	<context:component-scan base-package=my.controller" />
    	<context:component-scan base-package="my.services" />
    	<mvc:annotation-driven />
    
    </beans>
    web.xml
    Code:
        <!-- DispatcherServlet for regular Spring MVC (for GWT client that sends and receives JSON as data) -->
      	<servlet>
    		<servlet-name>mvc</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/json-context.xml</param-value>
            </init-param>
    		<load-on-startup>0</load-on-startup>
    	</servlet>
    
    	
    	<!-- Map all gwt request to the GWT DispatcherServlet for handling  -->
    	<servlet-mapping>
    		<servlet-name>mvc</servlet-name>
    		<url-pattern>/mvc/*</url-pattern>
    	</servlet-mapping>
    Controller
    Code:
    @Controller
    public class CodeController {
    	
    	@Autowired
    	@Qualifier("mockCodeService")
    	private CodeService codeService;
    	
    	
        
        /**
         * Expects the Content-type: 'application/json'
         * Returned Content-type: 'application/json'
         * 
         */
        @RequestMapping(value = "/geCodes", method = RequestMethod.POST)
        public @ResponseBody List<Code> getCodes(@RequestBody final CodeQuery codeQuery){
        	List<Code> codes = codeService.queryForCode(codeQuery);
        	return codes;
        }
    CodeQuery.java
    Code:
    // this class holds the search criteria
    public class CodeQuery implements Query{
        private String code;
        private String label;
        private List<AoInfo> aoInfo;  // where AoInfo is an interface, 4 classes implements the AoInfo interface.
        
    }

    I use FireFox (with Rest Client plugin) and make a POST call (with Content-Type set to "application/json").
    The first call will fail - results in unsupported Media Type (application/json), resend request again ad it works.
    only the first request (after the server start will fail). What am i doing wrong?
    Last edited by tqnguyen1973; Apr 15th, 2012, 06:50 PM.

  • #2
    Could you post the complete error stack trace shown in the server? If is not available, check the log files in the server.

    Comment


    • #3
      Actually, I tracked down the cause o the HTTP 415 Unsupported Media Type. My CodeQuery class also has one additional field List<AoInfo> where AoInfo is an interface and it has multiple classes that implements the interface.
      The Jackson deserializer was not able to deserialize the list of <AoInfo>, probably cause it cannot determine the actual implementation to use...understandable. However, no exception was being thrown or logged. It seem the Jackson mapper call the method canRead(Object, Class). The lack of logging was what thrown me off and wasted so much time in tracking down the bug.

      I had to resort to using the JSONObject and JSONArray to manually deserialize the JSON string. I changed the @RequestBody Code query to HttpServetRequest req, so i can get the body content from the request.

      Comment

      Working...
      X