1.场景说明
由于后端架构采用spring boot + thymeleaf,当前端vue调用后端controller层接口时,得到的是整个html文本。实际上后端只需要传送字符串给前端,后端若直接返回字符串又会报错,直接返回字符串写法:
@CrossOrigin
@RequestMapping("/queryAllUsers")
public String queryAllUsers(Model model){
//1.调用业务方法
List<User> list= userservice.listUser();
return JSONUtil.toJsonStr(list);
}
此写法会报如下错:org.thymeleaf.exceptions.TemplateInputException: Error resolving template "[{"userName":"111","userId":1,"createTim[...]","loginPass":"sdfgsdf"}]", template might not exist or might not be accessible by any of the configured Template Resolvers
2.解决方案
后端采用thymeleaf标准接口写法保证不报错,但返回给前端的是整个html网页内容,代码如下:
@CrossOrigin
@RequestMapping("/queryAllUsers")
public String queryAllUsers(Model model){
//1.调用业务方法