Announcement Announcement Module
Collapse
No announcement yet.
Trouble using Jackson 2 with Spring 3.2 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trouble using Jackson 2 with Spring 3.2

    I am having trouble spring use the ObjectMapper in jackson 2.

    How do i make Spring always use the jackson2 ObjectMapper by default.


    In my pom I have
    Code:
    		
                    <dependency>
    			<groupId>com.fasterxml.jackson.core</groupId>
    			<artifactId>jackson-databind</artifactId>
    			<version>2.1.4</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-webmvc</artifactId>
    			<version>3.3.2.RELEASE</version>
    		</dependency>
    No other json library is included in my dependency tree.


    My Controller looks like this
    The two methods should be equivalent

    1) "query" method takes the output object and explicitly writes it to string using the jackson2 ObjectMapper. This works fine.
    2) "query2" returns the output object as it is and I want spring to pick up the jackson2 objectmapper in classpath. I expected this to work out of the box and the same as 1. But, I get the following errors.
    Code:
    	
    @RequestMapping(value = "/query", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody String query(@RequestBody String request) throws JsonProcessingException {
    
    	BasicDBObject output = new QueryProcessor(dl,request).processRequest();
    
    	com.fasterxml.jackson.databind.ObjectMapper om = 
                                  new com.fasterxml.jackson.databind.ObjectMapper();
    	return om.writeValueAsString(output);
    		
    }
    	
    @RequestMapping(value = "/query2", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody BasicDBObject query2(@RequestBody String request) {
    		
    	BasicDBObject output = new QueryProcessor(dl,request).processRequest();
    	return output;
    		
    }

    While query method works fine, query2 throws the following errors.


    Code:
    11:35:09.814 [http-bio-8080-exec-1] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [public com.mongodb.BasicDBObject origami.qengine.springy.Request.query2(java.util.Locale,org.springframework.ui.Model,java.lang.String)]: java.lang.RuntimeException: json can't serialize type : class origami.qengine.Channels$Channel
    11:35:09.823 [http-bio-8080-exec-1] DEBUG o.s.web.servlet.DispatcherServlet - Could not complete request
    java.lang.RuntimeException: json can't serialize type : class origami.qengine.Channels$Channel
    	at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:77) ~[mongo-java-driver-2.10.1.jar:na]
    	at com.mongodb.util.JSONSerializers$MapSerializer.serialize(JSONSerializers.java:307) ~[mongo-java-driver-2.10.1.jar:na]
    	at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79) ~[mongo-java-driver-2.10.1.jar:na]
    	at com.mongodb.util.JSONSerializers$MapSerializer.serialize(JSONSerializers.java:307) ~[mongo-java-driver-2.10.1.jar:na]
    	at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79) ~[mongo-java-driver-2.10.1.jar:na]
    	at com.mongodb.util.JSON.serialize(JSON.java:56) ~[mongo-java-driver-2.10.1.jar:na]
    	at com.mongodb.util.JSON.serialize(JSON.java:41) ~[mongo-java-driver-2.10.1.jar:na]
    	at com.mongodb.BasicDBObject.toString(BasicDBObject.java:83) ~[mongo-java-driver-2.10.1.jar:na]
    	at java.lang.String.valueOf(String.java:2826) ~[na:1.6.0_43]
    	at java.lang.StringBuilder.append(StringBuilder.java:115) ~[na:1.6.0_43]
    	at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:150) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    	at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:90) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    	at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:189) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    	at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:69) ~[spring-web-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) ~[spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) [spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838) [spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) [servlet-api.jar:na]
    	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) [spring-webmvc-3.2.2.RELEASE.jar:3.2.2.RELEASE]
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) [servlet-api.jar:na]
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.37]
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.37]
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [catalina.jar:7.0.37]
    	at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:123) [catalina.jar:7.0.37]
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java) [catalina.jar:7.0.37]
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [catalina.jar:7.0.37]
    	at org.apache.catalina.core.StandardHostValve.__invoke(StandardHostValve.java:171) [catalina.jar:7.0.37]
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java) [catalina.jar:7.0.37]
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) [catalina.jar:7.0.37]
    	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936) [catalina.jar:7.0.37]
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.37]
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) [catalina.jar:7.0.37]
    	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) [tomcat-coyote.jar:7.0.37]
    	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) [tomcat-coyote.jar:7.0.37]
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) [tomcat-coyote.jar:7.0.37]
    	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) [na:1.6.0_43]
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) [na:1.6.0_43]
    	at java.lang.Thread.run(Thread.java:680) [na:1.6.0_43]

    My servlet-context looks like this
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:beans="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    	
    	<!-- Enables the Spring MVC @Controller programming model -->
    	<annotation-driven />
            <context:component-scan base-package="origami.qengine.springy" />
    
    	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    	<resources mapping="/resources/**" location="/resources/" />
    
    	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<beans:property name="prefix" value="/WEB-INF/views/" />
    		<beans:property name="suffix" value=".jsp" />
    	</beans:bean>
    	
    	
    	
    	
    	
    </beans:beans>
    Last edited by smartnut007; Mar 22nd, 2013, 10:31 PM.

  • #2
    Hello I'm using java config for my project. You can override configureMessageConverters and to set your custom object mapper . See code below:

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = {"org.mongo"}, excludeFilters = { @Filter(Configuration.class) })
    public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public InternalResourceViewResolver resolver(){
    InternalResourceViewResolver res = new InternalResourceViewResolver();
    res.setSuffix(".jsp");
    res.setPrefix("/WEB-INF/views/");
    return res;

    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/").addResourceLocations("/resourses/**");
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new LoggerInterceptor()).addPathPatterns("/**");
    super.addInterceptors(registry);
    }
    @Override
    public void configureMessageConverters(List<HttpMessageConvert er<?>> converters) {
    MappingJacksonHttpMessageConverter mappingJacksonHttpMessageConverter = new MappingJacksonHttpMessageConverter();
    mappingJacksonHttpMessageConverter.setPrefixJson(f alse);
    mappingJacksonHttpMessageConverter.setPrettyPrint( true);
    mappingJacksonHttpMessageConverter.setObjectMapper (customObjectMapper);//here set your custom object mapper
    converters.add(mappingJacksonHttpMessageConverter) ;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHand lerConfigurer configurer) {
    configurer.enable();
    }


    }

    Comment

    Working...
    X