I am trying to configure a simple example for REST service using CXF + Jackson with JaxB #XmlRootElement annotation. I have already gone through various similar questions in stack overflow as well as blogs like this and have come up with the below solution:
Created a custom object mapper as follows:
public class CustomObjectMapper extends ObjectMapper{
public CustomObjectMapper() {
super();
super.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
super.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
AnnotationIntrospector primary = new JaxbAnnotationIntrospector();
AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary);
super.setAnnotationIntrospector(pair);
}
}
Passed this to Json provided's constructor in spring config
<bean id="jacksonMapper" class="com.myorg.test.CustomObjectMapper" />
<jaxrs:providers>
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider">
<constructor-arg ref="jacksonMapper" />
</bean>
</jaxrs:providers>
Added a simple class JsonBean with JAXB Root element annotation
#XmlRootElement(name = "JsonBean")
public class JsonBean {
private String val1;
private String val2;
public String getVal1() {
return val1;
}
public void setVal1(String val1) {
this.val1 = val1;
}
public String getVal2() {
return val2;
}
public void setVal2(String val2) {
this.val2 = val2;
}
}
When I pass the below json,
{
"JsonBean": {
"val1": "Hello",
"val2": "Hi"
}
}
it's throwing below exception:
WARNING: javax.ws.rs.InternalServerErrorException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "JsonBean" (Class com.cognizan
t.poc.connectedcar.JsonBean), not marked as ignorable
at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1#1f2fae4; line: 2, column: 13] (through reference chain: com.myorg.test.Json
Bean["JsonBean"])
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:243)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:99)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:203)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:158)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:243)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:163)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:755)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:219)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:669)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:457)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1075)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:384)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1009)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:368)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489)
at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:953)
Am I missing something here? Please help.
I got it right.
I missed to add unwrap for de-serialization in the custom mapper:
added the below line in CustomObjectMaller
super.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
also, modified the spring config file as:
<jaxrs:providers>
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider">
<property name="mapper" ref="jacksonMapper" />
</bean>
</jaxrs:providers>
It started working as expected..
Related
I have a Spring MVC Rest service that return JSON value, i have 8 row, here is my JSON
[
{"no":"1","date":"2015-03-30","grandtotal":699618,"diskon":699618},
{"no":"2","date":"2015-03-30","grandtotal":1867949,"diskon":1867949},
{"no":"3","date":"2015-03-27","grandtotal":2190909,"diskon":2190909},
{"no":"4","date":"2015-03-26","grandtotal":8616120,"diskon":8616120},
{"no":"5","date":"2015-03-26","grandtotal":1095455,"diskon":1095455},
{"no":"6","date":"2015-03-24","grandtotal":938961,"diskon":938961},
{"no":"7","date":"2015-03-24","grandtotal":5603848,"diskon":5603848},
{"no":"8","date":"2015-03-20","grandtotal":3735899,"diskon":3735899}
]
What i trying.. Here is my controller. SpringRestController.java
Jackson Way :
#RequestMapping(value = "/view", method = RequestMethod.GET)
public String initCreationForm(Map<String, Object> model) {
String url = "http://localhost:8080/SpringServiceJson/view/";
RestTemplate restTemplate = new RestTemplate();
TotalDiscList totaldisc = restTemplate.getForObject(url, TotalDisc.class);
model.put("DiscValue",totaldisc);
return "salesorders/totalDisc";
}
Gson way :
public String initCreationForm(Map<String, Object> model) {
String url = "http://localhost:8080/SpringServiceJson/view/";
Gson gson = new Gson();
Collection<TotalDisc> totaldisc = gson.fromJson(url, PiutangListJson.class);
model.put("DiscValue",totaldisc);
return "salesorders/totalDisc";
}
what i missed here? it always give me this error
"Could not extract response: no suitable HttpMessageConverter found for response type [class [Lorg.springframework.samples.my.model.TotalDiscList;] and content type [application/json]"
Here is my object TotalDiscList.java
public class TotalDiscList {
private String no;
#DateTimeFormat(pattern="dd-MM-yyyy")
private Date date;
private long grandtotal;
private long diskon;
//getter setter skipped
}
i should return List<TotalDiscList> totaldisc = restTemplate.getForObject(url, List<TotalDisc>.class); right?
how i do that properly?
If you have a servlet-context.xml, you can add the message-convertor there, like below :
<beans:bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters" ref="jsonMessageConverter" />
</beans:bean>
<beans:bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
I am doing the same and it works.
Edit
You need to provide a Message converter to your RestTemplate
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
And then try using an array, such as:
TotalDiscList[] totaldisc = restTemplate
.getForObject(url, TotalDiscList[].class);
you can configure a json message converter:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
and then, you can just annotate your method:
#RequestMapping(value = "/view", method = RequestMethod.POST)
public TotalDiscList[] createDiscList(
#RequestBody TotalDiscList[] object) {
}
which would cause the message converter to try to convert to the given class.
If I use the following configuration then jackson converter works (mvc declaration is last)
<!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
<context:component-scan base-package="com.base" />
<mvc:annotation-driven />
If I use this configuration in dispatcher.xml then validation works but conversion does not. (mvc declaration first)
<context:component-scan base-package="com.base" />
<mvc:annotation-driven />
<!-- Configure to plugin JSON as request and response in method handler -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter"/>
</list>
</property>
</bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
Any help greatly appreciated.
Spring version 4.0.6
I chose the part where validation is working and added this in the code base.
#RequestMapping(value = "url", method = RequestMethod.GET)
protected void getLocationAsJson(#PathVariable("id") Integer id,
#RequestParam("cid") Integer cid, HttpServletResponse response) {
MappingJacksonHttpMessageConverter jsonConverter =
new MappingJacksonHttpMessageConverter();
Location loc= new Location(id);
MediaType jsonMimeType = MediaType.APPLICATION_JSON;
if (jsonConverter.canWrite(requestedLocation.getClass(), jsonMimeType)) {
try {
jsonConverter.write(requestedLocation, jsonMimeType,
new ServletServerHttpResponse(response));
} catch (IOException m_Ioe) {
// TODO: announce this exception somehow
} catch (HttpMessageNotWritableException p_Nwe) {
// TODO: announce this exception somehow
}
}
}
Now the validation works as well as JSON returning.
The method is not returning anything.
RequestMappingHandlerAdapter's xml configuration is bit complicated. The problem with this configuration is, it removes spring default configuration for converters. It is better to use coding version of this configuration. Spring default configuration will be intact this way. Here is sample configurations.
Suggested solution, posted on numerous blogs. But not working in my case.
https://dzone.com/articles/customizing
http://www.java-allandsundry.com/2014/09/customizing-httpmessageconverters-with.html
#Configuration
public class MessageConvertorConfiguration extends WebMvcConfigurationSupport {
#Bean
public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
Custom360DateFormat dateFormat = new Custom360DateFormat();
dateFormat.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
dateFormat.setDateTimeFormat(new SimpleDateFormat("MM/dd/yyyy hh:mm a"));
objectMapper.setDateFormat(dateFormat);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(customJackson2HttpMessageConverter());
super.addDefaultHttpMessageConverters(converters);
}
}
Working Solution
#Configuration
public class MessageConvertorConfiguration {
private MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
Custom360DateFormat dateFormat = new Custom360DateFormat();
dateFormat.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
dateFormat.setDateTimeFormat(new SimpleDateFormat("MM/dd/yyyy hh:mm a"));
objectMapper.setDateFormat(dateFormat);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
#Autowired
public void updateJacksonConvertor(RequestMappingHandlerAdapter handlerAdapter) {
//remove default jakson convertor
Iterator<HttpMessageConverter<?>> convertorsIterator = handlerAdapter.getMessageConverters().iterator();
while (convertorsIterator.hasNext()) {
HttpMessageConverter converter = convertorsIterator.next();
if(converter instanceof AbstractJackson2HttpMessageConverter) {
convertorsIterator.remove();
}
}
handlerAdapter.getMessageConverters().add(customJackson2HttpMessageConverter());
}
}
I am working on a Java EE project, and I had deployed my Rest Webservices using Jackson . the services works fine but one problem I have is that I don't get the root name :
here's an example of :
[{"idRegion":1,"intituleRegion":"Sous Massa Draa"},
{"idRegion":2,"intituleRegion":"Chawya W rdigha"},{"idRegion":3,"intituleRegion":"rabat sale zemmour zaer"},
{"idRegion":4,"intituleRegion":"grand casablanca "},
{"idRegion":5,"intituleRegion":"marrakech tansift lhawz"},{"idRegion":6,"intituleRegion":"essaouira"},
{"idRegion":7,"intituleRegion":"fes boulemane "}]
knowing that I had define the : #XmlRootElement(name="region")
JAXB BEAN :
package ma.propar.FireApp.Entites;
#Entity
#Table (name = "REGION")
#XmlAccessorType(XmlAccessType.NONE)
#XmlRootElement(name="region")
public class Region {
#Id #GeneratedValue
#Column( name = "ID_REGION" )
#XmlElement
private int idRegion;
#Column( name = "INTITULE_REGION" )
#XmlElement
private String intituleRegion;
#OneToMany ( targetEntity = Zone.class, cascade=CascadeType.ALL ,mappedBy="region")
private List<Zone> zones;
#WebMethod(exclude=true)
public List<Zone> getZones() {
return zones;
}
#WebMethod(exclude=true)
public void setZones(List<Zone> zones) {
this.zones = zones;
}
public Region() {
}
public int getIdRegion() {
return idRegion;
}
public void setIdRegion(int idRegion) {
this.idRegion = idRegion;
}
public String getIntituleRegion() {
return intituleRegion;
}
public void setIntituleRegion(String intituleRegion) {
this.intituleRegion = intituleRegion;
}
/*#WebMethod(exclude=true)
public List<Zone> getZones() {
return zones;
}
#WebMethod(exclude=true)
public void setZones(List<Zone> zones) {
this.zones = zones;
}*/
}
Jackson with jax-rs :
<jaxrs:server id="regionservices" address="/regionservices">
<jaxrs:serviceBeans>
<ref bean="regionMetier" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider" />
</jaxrs:providers>
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
</jaxrs:extensionMappings>
</jaxrs:server>
any body can help ?
By default, only core Jackson annotations are used for configuring data binding aspects. To enable JAXB annotation support, you need to:
Include jackson-xc jar, which contains org.codehaus.jackson.xc.JaxbAnnotationIntrospector (Jackson 1.x)
or, com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector (Jackson 2.x)
Register this annotation introspector
You can find more details here
Jackson alternative to #XmlRootElement annotation is #JsonRootName.
Annotation similar to bind.annotation.XmlRootElement, used to indicate name to use for root-level wrapping, if wrapping is enabled.
Here is the thing... i'm using jtable (jquery) to show some user data. This component needs a json with two fields: Result and Records. In my controller i have a method to return the json:
#RequestMapping(method=RequestMethod.POST, value="/getUsersInJson")
public #ResponseBody String getUsersInJsonHandler(){
ElementsInList<User> users = new ElementsInList<User>();
users.setItems(userService.getUsers());
return users;
}
The class ElementsInList contains two fields: result and records. Result is a string to get the success message and records is a parametrized list which contains in this case a list of users. I get this JSON:
"{"result":"OK","records":[{"username":"john",
But i need this:
"{"Result":"OK","Records":[{"username":"john",...
This is my Mapping:
<!-- Json converter bean -->
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
</list>
</property>
</bean>
How can i do it? I've checked some posts but have old versions.
I'm using Spring 3, Spring MVC and jQuery.
I solved it by using JsonProperty annotation. You can give the name that jackson will use to build the json field. Here there is an example with jtable (jquery):
public class ElementsInList<T> {
#JsonProperty("Result")
private String result = "OK";
#JsonProperty("Records")
private List<T> records;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public List<T> getRecords() {
return records;
}
public void setRecords(List<T> records) {
this.records = records;
}
}
The result json is this: {"Result":"OK","Records":[{"roleName":"admin"...
But there is more about this annotations. Check the api for more info: http://fasterxml.github.io/jackson-annotations/javadoc/2.1.0/com/fasterxml/jackson/annotation/package-summary.html
I don't like Jackson.
I want to use ajax but with Google Gson.
So I'm trying to figure out how to implement my own HttpMessageConverter to use it with #ResponseBody annotation.
Can someone take a time to show me the way I should go? What configurations should I turn on?
Also I'm wondering if I can do this and still use <mvc:annotation-driven />?
Thanks in advance.
I've already asked it in Spring Community Foruns about 3 days ago with no answer so I'm asking here to see if I get a better chance.
Spring Community Forums link to my question
I've also made an exhaustive search on the web and found something interesting on this subject but it seems they're thinking to put it in Spring 3.1 and I'm still using spring 3.0.5:
Jira's Spring Improvement ask
Well... now I'm trying to debug Spring code to find out myself how to do this, but I'm having some problems like I've said here:
Spring Framework Build Error
If there is another way to do this and I'm missing it, please let me know.
Well... it was so hard to find the answer and I had to follow so many clues to incomplete information that I think it will be good to post the complete answer here. So it will be easier for the next one searching for this.
First I had to implement the custom HttpMessageConverter:
package net.iogui.web.spring.converter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
private Gson gson = new Gson();
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
public GsonHttpMessageConverter(){
super(new MediaType("application", "json", DEFAULT_CHARSET));
}
#Override
protected Object readInternal(Class<? extends Object> clazz,
HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
try{
return gson.fromJson(convertStreamToString(inputMessage.getBody()), clazz);
}catch(JsonSyntaxException e){
throw new HttpMessageNotReadableException("Could not read JSON: " + e.getMessage(), e);
}
}
#Override
protected boolean supports(Class<?> clazz) {
return true;
}
#Override
protected void writeInternal(Object t,
HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
//TODO: adapt this to be able to receive a list of json objects too
String json = gson.toJson(t);
outputMessage.getBody().write(json.getBytes());
}
//TODO: move this to a more appropriated utils class
public String convertStreamToString(InputStream is) throws IOException {
/*
* To convert the InputStream to String we use the Reader.read(char[]
* buffer) method. We iterate until the Reader return -1 which means
* there's no more data to read. We use the StringWriter class to
* produce the string.
*/
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
return writer.toString();
} else {
return "";
}
}
}
Then I had to strip off the annnotaion-driven tag and configure all by my own hands on the spring-mvc configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
<!-- Configures the #Controller programming model -->
<!-- To use just with a JSR-303 provider in the classpath
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="net.iogui.web.spring.util.CommonWebBindingInitializer" />
</property>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter" />
<bean class="net.iogui.web.spring.converter.GsonHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
<!-- bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" /-->
</list>
</property>
</bean>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<context:component-scan base-package="net.iogui.teste.web.controller"/>
<!-- Forwards requests to the "/" resource to the "login" view -->
<mvc:view-controller path="/" view-name="home"/>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
See that, to make the Formater and Validator to work, we have to build a custom webBindingInitializer too:
package net.iogui.web.spring.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
public class CommonWebBindingInitializer implements WebBindingInitializer {
#Autowired(required=false)
private Validator validator;
#Autowired
private ConversionService conversionService;
#Override
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.setValidator(validator);
binder.setConversionService(conversionService);
}
}
An Interesting thing to see is that In order to make the configuration work without the annotaion-driven tag, we have to manually configure a AnnotationMethodHandlerAdapter and a DefaultAnnotationHandlerMapping. And in order to make the AnnotationMethodHandlerAdapter capable of handling formatting and validation, we had to configure a validator, a conversionService and to build a custom webBindingInitializer.
I hope all this helps someone else besides me.
On my desperate search, this #Bozho post was extremely util. I am also grateful to #GaryF couse his answer took me to the #Bozho post.
To you that are trying to do this in Spring 3.1, see #Robby Pond answer.. A lot easier, isn't it?
You need to create a GsonMessageConverter that extends AbstractHttpMessageConverter and use the mvc-message-converters tag to register your message converter. That tag will let your converter take precedence over the Jackson one.
If you want to add a message converter without messing with xml here is a simple example
#Autowired
private RequestMappingHandlerAdapter adapter;
#PostConstruct
public void initStuff() {
List<HttpMessageConverter<?>> messageConverters = adapter.getMessageConverters();
BufferedImageHttpMessageConverter imageConverter = new BufferedImageHttpMessageConverter();;
messageConverters.add(0,imageConverter);
}
I had situation where usage of Jackson would require me to alter other group's (in the same company) code. Didn't like that. So I chose to use Gson and register TypeAdapters as needed.
Hooked up a converter and wrote a few integration tests using spring-test (used to be spring-mvc-test). No matter what variation I tried (using mvc:annotation-driven OR manual definition of the bean). None of them worked. Any combination of these always used the Jackson Converter which kept on failing.
Answer> Turns out that MockMvcBuilders' standaloneSetup method "hard" coded the message converters to default versions and ignored all my changes above. Here is what worked:
#Autowired
private RequestMappingHandlerAdapter adapter;
public void someOperation() {
StandaloneMockMvcBuilder smmb = MockMvcBuilders.standaloneSetup(controllerToTest);
List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
HttpMessageConverter<?> ary[] = new HttpMessageConverter[converters.size()];
smmb.setMessageConverters(conveters.toArray(ary));
mockMvc = smmb.build();
.
.
}
Hope this helps someone, in the end I used annotation-driven and re-purposing android's converter
Notice that GsonHttpMessageConverter was added recently to Spring (4.1)
Robby Pond is basically correct, but note that his suggestion to use the mvc:message-converters tag requires that you use 3.1. Since 3.1 is currently only a milestone release (M1), I'd suggest registering your converter this way after creating it:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="someMessageConverter"/>
<ref bean="someOtherMessageConverter"/>
</util:list>
</property>
</bean>
Or as mentioned in Jira's Spring Improvement ask, write a BeanPostProcessor that adds your HttpMessageConvertor to the AnnotationMethodHandlerAdapter
You can do this by writing the WebConfig file as a Java File. Extend your config file with WebMvcConfigurerAdapter and override extendMessageConverters method to add your intented Message Convertor. This method will retain the default converters added by Spring and will add your convertor at the end. Apparently you have full control with the list and you can add where ever you want in the list.
#Configuration
#EnableWebMvc
#ComponentScan(basePackageClasses={WebConfig.class})
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new GsonHttpMessageConverter());
}
}
package net.iogui.web.spring.converter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
private Gson gson = new Gson();
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
public GsonHttpMessageConverter(){
super(new MediaType("application", "json", DEFAULT_CHARSET));
}
#Override
protected Object readInternal(Class<? extends Object> clazz,
HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
try{
return gson.fromJson(convertStreamToString(inputMessage.getBody()), clazz);
}catch(JsonSyntaxException e){
throw new HttpMessageNotReadableException("Could not read JSON: " + e.getMessage(), e);
}
}
#Override
protected boolean supports(Class<?> clazz) {
return true;
}
#Override
protected void writeInternal(Object t,
HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
//TODO: adapt this to be able to receive a list of json objects too
String json = gson.toJson(t);
outputMessage.getBody().write(json.getBytes());
}
//TODO: move this to a more appropriated utils class
public String convertStreamToString(InputStream is) throws IOException {
/*
* To convert the InputStream to String we use the Reader.read(char[]
* buffer) method. We iterate until the Reader return -1 which means
* there's no more data to read. We use the StringWriter class to
* produce the string.
*/
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
return writer.toString();
} else {
return "";
}
}