I am trying to get my Controller to receive JSON, but it is failing. I am new to Spring and also new to HTML browser development. It seems I am returning JSON OK in the first few methods below, just can't receive it in the last method. I am at a total loss what to do. Here is my setup.
Here is part of my controller code:
@Controller
public class SDTM_Controller {
private SDTM_Service service;
@RequestMapping(value="/getVersions", method=RequestMethod.GET)
public @ResponseBody List<String> getVersions(){
return service.getVersions();
}
// URL: sdtm/getTemplateInfo?version=xxx
@RequestMapping(value="/getTemplateInfo", params="version", method=RequestMethod.GET)
public @ResponseBody List<DomainTemplateInfo> getTemplateInfo(@RequestParam String version){
return service.getTemplates(version);
}
@RequestMapping(value="/createDomains", method=RequestMethod.POST, consumes="application/json")
public @ResponseBody String createDomains(@RequestBody CreateDomainParams params){
// breakpoint set hit, but never reached.
}
}
The methods getVersions and getTemplateInfo work just fine, they return the data in JSON format. However the createDomains method never gets called.
In the WEB-INF directory I have a file named sdtm-servlet.xml. Here is the content. I have tried this with and without the element.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
>
<context:component-scan base-package="com.sas.hls.clc.clinicalstandards.sdtm" />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
My WEB-INF/web.xml file contains the following entry:
<servlet>
<servlet-name>sdtm</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/sdtm-servlet.xml</param-value>
</init-param>
</servlet>
I am running this in Tomcat, using Spring 3.2. My application directory's WEB-INF/lib directory contains jackson-core-asl-1.9.13.jar and jackson-mapper-asl.1.9.13.jar.
What am I missing?
Also, another developer wrote a small HTML page that is used for submitting the requests to the server. We are using Chrome. He insists that the JSON data needs to be UURL encoded, but when I look at the request info being sent in Chrome, the request body is NOT in JSON format, it seems to be translated into a format that looks like a list of HTTP request parameters. Should he be doing the encoding? What is missing on the browswer or server side that is preventing the call of my controller's method called createDomain.
Content-Type
header, which according to your controller method should beapplication/json
. – superEb