如果idea中使用Controller,定义方法中不加ResponseBody会报错
javax.servlet.ServletException: Circular view path [select1]: would dispatch back to the current handler URL [/select1] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
@Controller
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/select")
public @ResponseBody List<User> selectUser(){
List<User> userList = userMapper.queryUserlist();
for (User user:userList
) {
System.out.println(user);
}
return userList;
}
@RequestMapping("/select1")
public List<User> selectUser1(){
List<User> list = userMapper.queryUserlist();
return list;
}
}
访问http://localhost:8080/select1会报错。
1) 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
2) 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。@RestController注解,相当于@Controller+@ResponseBody两个注解的结合