SpingMVC返回Json对象类型报异常
使用SpringMVC返回Json对象时,报 org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type 异常
public @ResponseBody BaseResult login(String username, String password, String vercode) throws Exception { BaseResult br = new BaseResult(); Manager manager = managerService.findByUsernameAndPassword(username,password); if (manager==null){ br = new BaseResult("Error","用户名或密码错误"); } return br; }
原因是,SpinGMVC无法将对象直接转换为Json对象,因此需要添加依赖。
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.5</version> </dependency>