我试图在控制器中的方法中使用HashMap返回一个json
//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);
}
当我在PostMan中使用此方法时,我收到此错误
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)
这就是我设置应用程序的方式
@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();
}
}
错误说,它无法将列表转换为httpmessges,所以我需要任何转换器或类似的东西吗?我错过了什么?
答案 0 :(得分:1)
对于遇到类似问题的人:
如果您返回一个没有 Getter 和 Setter 方法的类的对象,就会发生这种情况。
例如: 如果你有以下设置
public class Employee {
private int code;
private String name;
}
并以某种方法使用它:
@GetMapping("")
public ResponseEntity<Employee> getEmployee(@RequestParam("id") Integer id) {
Employee employee;
/* CODE. . .
*/
return new ResponseEntity<>(employee, HttpStatus.OK);
}
您可能会收到一条错误消息
<块引用>org.springframework.http.converter.HttpMessageNotWritableException:没有找到返回值类型的转换器:类 com.dummy.springWeb.ExceptionHandler.Employee
要解决,只需在employee类中添加getter和setter就可以解决问题!
答案 1 :(得分:0)
方法之上的用户@ResponseBody
注释。
@RequestMapping(value = "/user",method = RequestMethod.GET)
@ResponseBody
确保用户实体拥有getter和setter。
答案 2 :(得分:0)
你可以简单地在你的班级上面返回这样的Map和用户@RestController注释:
@RequestMapping(value = "/user",method = RequestMethod.GET)
public 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);
message.putAll(json);
return message;
}
答案 3 :(得分:0)
我认为你需要自定义httpmessageconvertor。
添加依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
如果这不起作用,那么尝试在配置文件中配置MappingJackson到HttpMessage Convertor的自定义配置。
答案 4 :(得分:-1)
在Controller类级别提供 @RestController ,在方法级别提供 @ResponseBody 。 确保所有与杰克逊相关的罐子都在类路径中。
将jackson相关依赖项添加到您的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);
}
杰克逊的pom依赖示例。
<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>