点击控件没接收到返回的Json对象,Tomcat报错。
原因:
因为返回信息为json对象,controller方法所在的入口类上,添加的注解为:@Controller
而@Controller是不适合返回json内容的
方法一:不影响其它方法的使用的情况下,将@Controller注解改为@RestController
方法二:在需要请求的返回json对象的方法上添加@ResponseBody注解
项目中该方法所在类的其它方法有的返回到指定页面,所以不能用@RestController直接替换@Controller,所以采用方法二解决了问题。
@Controller与@RestController区别如下
@RestController is a stereotype annotation that combines @ResponseBody and @Controller.
意思是:
@RestController注解相当于@ResponseBody + @Controller合在一起的作用。
1)如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
例如:本来应该到success.jsp页面的,则其显示success.
2)如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
3)如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。