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)
web.xmlCode:<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>
ControllerCode:<!-- 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>
CodeQuery.javaCode:@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; }
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?


Reply With Quote