org.springframework.http.converter.HttpMessageNotReadableException

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • My setup is completely based on Java Configuration files. I configured my my application as follows: {code:java} @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] { CustomMongoDBRepositoryConfig.class, CustomJPARepositoryConfig.class, CustomSecurityConfiguration.class }; } .. @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext(); webCtx.register(CustomRepositoryRestMvcConfiguration.class); DispatcherServlet dispatcherServlet = new DispatcherServlet(webCtx); ServletRegistration.Dynamic appServlet = servletContext.addServlet("exporter", dispatcherServlet); appServlet.setAsyncSupported(true); appServlet.setLoadOnStartup(1); appServlet.addMapping("/*"); FilterRegistration.Dynamic corsFilter = servletContext.addFilter("corsFilter", CORSFilter.class); corsFilter.addMappingForUrlPatterns(null, false, "/*"); FilterRegistration.Dynamic filter = servletContext.addFilter("openSessionInViewFilter", OpenEntityManagerInViewFilter.class); filter.setInitParameter("singleSession", "true"); filter.addMappingForServletNames(null, true, "exporter"); FilterRegistration.Dynamic securityFilter = servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain")); securityFilter.addMappingForUrlPatterns(null, false, "/*"); super.onStartup(servletContext); } {code} The interesting thing is, everything is working fine. MVC URLs are mapped, controllers are working, Spring Security is working, SD REST URLs are mapped and working, but one thing is not working: POST/PUT of entities with rels. Having the following payload posted to /roles {code:json} { "name": "Developer", "description": "Standard role for a agent developer in the system", "rights": [ "http://localhost:8080/web/rights/186", "http://localhost:8080/web/rights/189", "http://localhost:8080/web/rights/185", "http://localhost:8080/web/rights/46" ] } {code} leads to the following exception: {code} LOGBACK:16:54:09.765 [tomcat-http--2] ERROR o.s.d.r.w.RepositoryRestExceptionHandler - Could not read JSON: (was java.lang.NullPointerException) (through reference chain: de.cloudscale.model.user.Role["rights"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: de.cloudscale.model.user.Role["rights"]) org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: (was java.lang.NullPointerException) (through reference chain: de.cloudscale.model.user.Role["rights"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: de.cloudscale.model.user.Role["rights"]) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:208) ~[AbstractJackson2HttpMessageConverter.class:4.1.1.RELEASE] at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readInternal(AbstractJackson2HttpMessageConverter.java:192) ~[AbstractJackson2HttpMessageConverter.class:4.1.1.RELEASE] at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:159) ~[AbstractHttpMessageConverter.class:4.1.1.RELEASE] at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.read(PersistentEntityResourceHandlerMethodArgumentResolver.java:185) ~[PersistentEntityResourceHandlerMethodArgumentResolver.class:na] at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.read(PersistentEntityResourceHandlerMethodArgumentResolver.java:168) ~[PersistentEntityResourceHandlerMethodArgumentResolver.class:na] at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.resolveArgument(PersistentEntityResourceHandlerMethodArgumentResolver.java:123) ~[PersistentEntityResourceHandlerMethodArgumentResolver.class:na] at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77) ~[HandlerMethodArgumentResolverComposite.class:4.1.1.RELEASE] {code} Which in turn is a result of fact that in DomainClassConverter the variable repositories is empty during the POST/PUT call. Following that the RepositoryInformations are empty and the NPE is thrown. I guess this is happening due to some context splitup during startup time. The interesting thing is, that POST/PUT a plain object without any rels works properly, I am not able to see any stuff in the debug logs showing me the second context
    via by Johannes Hiemer,
  • My setup is completely based on Java Configuration files. I configured my my application as follows: {code:java} @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] { CustomMongoDBRepositoryConfig.class, CustomJPARepositoryConfig.class, CustomSecurityConfiguration.class }; } .. @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext(); webCtx.register(CustomRepositoryRestMvcConfiguration.class); DispatcherServlet dispatcherServlet = new DispatcherServlet(webCtx); ServletRegistration.Dynamic appServlet = servletContext.addServlet("exporter", dispatcherServlet); appServlet.setAsyncSupported(true); appServlet.setLoadOnStartup(1); appServlet.addMapping("/*"); FilterRegistration.Dynamic corsFilter = servletContext.addFilter("corsFilter", CORSFilter.class); corsFilter.addMappingForUrlPatterns(null, false, "/*"); FilterRegistration.Dynamic filter = servletContext.addFilter("openSessionInViewFilter", OpenEntityManagerInViewFilter.class); filter.setInitParameter("singleSession", "true"); filter.addMappingForServletNames(null, true, "exporter"); FilterRegistration.Dynamic securityFilter = servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain")); securityFilter.addMappingForUrlPatterns(null, false, "/*"); super.onStartup(servletContext); } {code} The interesting thing is, everything is working fine. MVC URLs are mapped, controllers are working, Spring Security is working, SD REST URLs are mapped and working, but one thing is not working: POST/PUT of entities with rels. Having the following payload posted to /roles {code:json} { "name": "Developer", "description": "Standard role for a agent developer in the system", "rights": [ "http://localhost:8080/web/rights/186", "http://localhost:8080/web/rights/189", "http://localhost:8080/web/rights/185", "http://localhost:8080/web/rights/46" ] } {code} leads to the following exception: {code} LOGBACK:16:54:09.765 [tomcat-http--2] ERROR o.s.d.r.w.RepositoryRestExceptionHandler - Could not read JSON: (was java.lang.NullPointerException) (through reference chain: de.cloudscale.model.user.Role["rights"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: de.cloudscale.model.user.Role["rights"]) org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: (was java.lang.NullPointerException) (through reference chain: de.cloudscale.model.user.Role["rights"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: de.cloudscale.model.user.Role["rights"]) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:208) ~[AbstractJackson2HttpMessageConverter.class:4.1.1.RELEASE] at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readInternal(AbstractJackson2HttpMessageConverter.java:192) ~[AbstractJackson2HttpMessageConverter.class:4.1.1.RELEASE] at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:159) ~[AbstractHttpMessageConverter.class:4.1.1.RELEASE] at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.read(PersistentEntityResourceHandlerMethodArgumentResolver.java:185) ~[PersistentEntityResourceHandlerMethodArgumentResolver.class:na] at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.read(PersistentEntityResourceHandlerMethodArgumentResolver.java:168) ~[PersistentEntityResourceHandlerMethodArgumentResolver.class:na] at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.resolveArgument(PersistentEntityResourceHandlerMethodArgumentResolver.java:123) ~[PersistentEntityResourceHandlerMethodArgumentResolver.class:na] at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77) ~[HandlerMethodArgumentResolverComposite.class:4.1.1.RELEASE] {code} Which in turn is a result of fact that in DomainClassConverter the variable repositories is empty during the POST/PUT call. Following that the RepositoryInformations are empty and the NPE is thrown. I guess this is happening due to some context splitup during startup time. The interesting thing is, that POST/PUT a plain object without any rels works properly, I am not able to see any stuff in the debug logs showing me the second context
    via by Johannes Hiemer,
  • Spring Data REST with custom GSON instance
    via Stack Overflow by mcxme
    ,
  • Hi guys, I have the following JPA mapping: {code} @Entity public class Product { @ElementCollection(fetch = FetchType.LAZY) @CollectionTable(name = "product_name_lv", joinColumns = @JoinColumn(name = "product_pk")) @MapKeyColumn(name = "locale") @MapKeyJoinColumn(name = "language", referencedColumnName = "isocode") private Map<Locale, LocalizedValue> name = new HashMap<Locale, LocalizedValue>(); } {code} where the LocalizedValue is a simple POJO class: {code} @Embeddable public class LocalizedValue { @Column(name = "VALUE") private String value; } {code} So SDR works good and returns my product, like a normal json: {code} "name" : { "bg" : { "value" : "FOO" }, "en" : { "value" : "BAR" }, "de" : { "value" : "German FOO" } }, {code} So far so good I try to make a PUT request with the same json data, it yields with: {code} 2013-10-19 13:05:57,726 [qtp508751118-134] ERROR: Could not read JSON: Unrecognized field "en" (class com.xxxxxx.platform.core.model.i18n.LocalizedValue), not marked as ignorable (one known property: "value"]) at [Source: org.eclipse.jetty.server.HttpInput@63497f5b; line: 1, column: 30] (through reference chain: com.xxxxx.platform.core.model.i18n.LocalizedValue["en"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "en" (class com.xxxxxx.platform.core.model.i18n.LocalizedValue), not marked as ignorable (one known property: "value"]) at [Source: org.eclipse.jetty.server.HttpInput@63497f5b; line: 1, column: 30] (through reference chain: com.xxxxx.platform.core.model.i18n.LocalizedValue["en"]) org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "en" (class com.xxxxxx.platform.core.model.i18n.LocalizedValue), not marked as ignorable (one known property: "value"]) at [Source: org.eclipse.jetty.server.HttpInput@63497f5b; line: 1, column: 30] (through reference chain: com.xxxxxx.platform.core.model.i18n.LocalizedValue["en"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "en" (class com.xxxxxx.platform.core.model.i18n.LocalizedValue), not marked as ignorable (one known property: "value"]) at [Source: org.eclipse.jetty.server.HttpInput@63497f5b; line: 1, column: 30] (through reference chain: com.xxxxxx.platform.core.model.i18n.LocalizedValue["en"]) at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:181) at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readInternal(MappingJackson2HttpMessageConverter.java:166) at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:153) at org.springframework.data.rest.webmvc.PersistentEntityResourceHandlerMethodArgumentResolver.resolveArgument(PersistentEntityResourceHandlerMethodArgumentResolver.java:48) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:849) at javax.servlet.http.HttpServlet.service(HttpServlet.java:758) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) at javax.servlet.http.HttpServlet.service(HttpServlet.java:848) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:686) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1494) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1482) at org.apache.logging.log4j.core.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:66) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1482) at com.xxxxxx.platform.modules.rest.mvc.filter.CorsFilter.doFilterInternal(CorsFilter.java:23) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1474) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:499) 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:1086) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:428) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020) 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:370) at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:489) at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:960) at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1021) at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865) at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240) at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:668) at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543) at java.lang.Thread.run(Thread.java:722) {code} So I started digging in the code and I think the problem happens in PersistentEntityJackson2Module:197. I can see there an if-statement which checks if the property is of type map: {code} } else if (persistentProperty.isMap()) { Class<? extends Map<?, ?>> mtype = (Class<? extends Map<?, ?>>) persistentProperty.getType(); Map<Object, Object> m = (Map<Object, Object>) wrapper.getProperty(persistentProperty); if (null == m || m == Collections.EMPTY_MAP) { m = new HashMap<Object, Object>(); } if ((tok = jp.nextToken()) == JsonToken.START_OBJECT) { do { name = jp.getCurrentName(); // TODO resolve domain object from URI tok = jp.nextToken(); Object mval = jp.readValueAs(persistentProperty.getMapValueType()); //<-- this line reads as persistent property map value type. m.put(name, mval); } while ((tok = jp.nextToken()) != JsonToken.END_OBJECT); val = m; } else if (tok == JsonToken.VALUE_NULL) { val = null; } else { throw new HttpMessageNotReadableException("Cannot read a JSON " + tok + " as a Map."); } {code} So it basically gets the type of the persistent property, creates an empty map, iterates over the json, and reads the value of the original map (i.e. what will be placed after name: ) as the value of the persistent property map (LocalizedValue in my case). Which is wrong I think. It should read it as a map imho.
    via by Petar Tahchiev,
  • GitHub comment 1#238048746
    via GitHub by Pytry
    ,
  • Cannot use non optional arguments
    via GitHub by snowe2010
    ,
    • org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: (was java.lang.NullPointerException) (through reference chain: de.cloudscale.model.user.Role["rights"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: de.cloudscale.model.user.Role["rights"]) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:208)[AbstractJackson2HttpMessageConverter.class:4.1.1.RELEASE] at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readInternal(AbstractJackson2HttpMessageConverter.java:192)[AbstractJackson2HttpMessageConverter.class:4.1.1.RELEASE] at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:159)[AbstractHttpMessageConverter.class:4.1.1.RELEASE] at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.read(PersistentEntityResourceHandlerMethodArgumentResolver.java:185)[PersistentEntityResourceHandlerMethodArgumentResolver.class:na] at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.read(PersistentEntityResourceHandlerMethodArgumentResolver.java:168)[PersistentEntityResourceHandlerMethodArgumentResolver.class:na] at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.resolveArgument(PersistentEntityResourceHandlerMethodArgumentResolver.java:123)[PersistentEntityResourceHandlerMethodArgumentResolver.class:na] at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)[HandlerMethodArgumentResolverComposite.class:4.1.1.RELEASE]

    Users with the same issue

    Unknown visitor1 times, last one,
    tvrmsmith
    3 times, last one,
    Unknown visitor1 times, last one,