Controller返回数据到前端的时候出现了异常,工程:Spring Boot + Mybatis,Controller代码:
@Controller
public class UserController {
@Autowired
private UserMapper userService;
@RequestMapping("/users")
public List<User> getAllUsers() {
return userService.getAllUsers();
}
}
返回的是一个User列表,但是抛了ServletException异常,大致的意思是没有指定视图结果,让你检查一下你的视图配置,在springmvc中我们是使用viewResolver,通过在controller中return的前缀来决定跳转到相应的视图。在Spring Boot中也可以通过这样的方式,但是现在我想要返回的是一段Json数据,不需要视图来接收。
解决办法:
1.把@Controller注解换成@RestController
2.在方法上添加注解@ResponseBody
在这里为什么使用@RestController,和@Controller有什么区别呢?不如看一下源码:
@Controller源码:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
*/
String value() default "";
}
@RestController源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
* @since 4.0.1
*/
String value() default "";
}
会发现在@RestController的注解中多了一个@ResponseBody注解,这就是区别所在,也就是说@RestController包含了@Controller控制器的功能,也包含了直接响应数据的功能。
3.如果想要用视图去展示,应该要设置好视图展示页面,比如说用一个模板语言来接收返回的数据(thymeleaf或者freemarker等),也可以用jsp接收,但是SpringBoot官方是不推荐用jsp的,而是建议使用thymeleaf作为模板语言,这里我以thymeleaf为例。
在maven工程pom中引入thymeleaf依赖:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<thymeleaf.version> 3.0.2.RELEASE </thymeleaf.version>
<thymeleaf-layout-dialect.version> 2.1.1 </thymeleaf-layout-dialect.version>
<tomcat.version>7.0.69</tomcat.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath />
</parent>
.... // 中间省略
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在application.properties中配置:
spring.datasource.url= jdbc:mysql://localhost:3306/test
spring.datasource.username= root
spring.datasource.password= root
spring.datasource.driverClassName= com.mysql.jdbc.Driver
spring.thymeleaf.mode = HTML5
# mybatis
mybatis.config-locations=classpath*:mybatis-config.xml
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
mybatis.type-aliases-package=com.fisher.domain
server.session-timeout = 3600
server.port= 8080
定义一个user.html,路径/src/main/resources/template, 用来展示数据:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<body>
<div>
<h3>用户列表</h3>
<div>
<table border = "1">
<tr>
<td>ID</td>
<td>Name</td>
<td>Mark</td>
</tr>
<tr th:each="user : ${users}" >
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.mark}"></td>
</tr>
</table>>
</div>
</div>
</body>
</html>
这里的Controller需要修改,改为:
@Controller
public class UserController {
@Autowired
private UserMapper userService;
@RequestMapping("/users")
public String getAllUsers(Model model) {
List<User> users = userService.getAllUsers();
model.addAttribute("users", users);
return "user";
}
}
因为这里返回的是模板的路径,自动跳转至user.html页面。启动Spring Boot工程后,在浏览器中访问localhost:8080/users,即可看到用户列表