I am trying to return a json using HashMap like this in a method in the controller
//list all users @RequestMapping(value = "/user",method = RequestMethod.GET) public ResponseEntity<Map<String,Object>> handleRequest() throws Exception{ List<User> users = userService.list(); Map<String, Object> message = new HashMap<String, Object>(); message.put("severity", "info"); message.put("user", users); message.put("summary", "Not successfully."); message.put("code", 200); Map<String, Object> json = new HashMap<String, Object>(); json.put("success", true); json.put("message", message); return new ResponseEntity<Map<String,Object>>(message,HttpStatus.CREATED); }
When I hit this method in PostMan I get this error
org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.HashMap org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:226) org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:180) org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) javax.servlet.http.HttpServlet.service(HttpServlet.java:635) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
This is how I have setuped the Application
@Configuration @ComponentScan("com.fyt.fyt") @EnableTransactionManagement public class ApplicationContextConfig { @Bean public InternalResourceViewResolver getViewResolver() { InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver(); internalResourceViewResolver.setPrefix("/WEB-INF/views/"); internalResourceViewResolver.setSuffix(".jsp"); return internalResourceViewResolver; } @Bean public DataSource getDataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/fyt"); dataSource.setUsername("root"); dataSource.setPassword(""); return dataSource; } @Bean public LocalSessionFactoryBean getSessionFactory() { LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean(); factoryBean.setDataSource(getDataSource()); factoryBean.setHibernateProperties(getHibernateProperties()); factoryBean.setAnnotatedClasses(User.class); return factoryBean; } private Properties getHibernateProperties() { Properties properties = new Properties(); properties.put("hibernate.show_sql", "true"); properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); return properties; } @Bean public HibernateTransactionManager getTransactionManager() { HibernateTransactionManager transactionManager = new HibernateTransactionManager(); transactionManager.setSessionFactory(getSessionFactory().getObject()); return transactionManager; } @Bean public UserDao userDao() { return new UserDaoImpl(); } }
The error says, it cannot convert the list to httpmessges so do I need to any any converters or something like that? What am I missing something?
Advertisement
Answer
Provide @RestController in Controller class level other wise provide @ResponseBody in method level. Make sure that all jackson related jars are there in class path.
Add jackson related dependencies in to your pom.xml
@RequestMapping(value = "/user",method = RequestMethod.GET) @ResponseBody public ResponseEntity<Map<String,Object>> handleRequest() throws Exception{ List<User> users = userService.list(); Map<String, Object> message = new HashMap<String, Object>(); message.put("severity", "info"); message.put("user", users); message.put("summary", "Not successfully."); message.put("code", 200); Map<String, Object> json = new HashMap<String, Object>(); json.put("success", true); json.put("message", message); return new ResponseEntity<Map<String,Object>>(message,HttpStatus.CREATED); }
Sample pom dependencies for jackson .
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency>