@ResponseBody响应头Content-Type不是application/json

后端使用@ResponseBody返回一个对象时,前端无法获取数据。检查发现响应体的Content-Type类型是text/html;charset=utf-8。

后端代码:

@ResponseBody
@GetMapping( value = "/getall")
public Account showList() throws Exception {
    List<Account> accountList = accountService.getAll();
    Account account = accountList.get(1);
    return account;
}

前端代码:

 getAccountList(){
    //发起异步请求 获取表单数据
    axios({
    	method:"GET",
    	url:path+"getall"
    }).then(function (result) {
    	console.log(result.data);
    })
}

错误截图:
在这里插入图片描述
在这里插入图片描述

解决方法:引入Jackson依赖

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.3</version>
</dependency>

注意事项:

引入依赖后注意检查编译输出目录是否也有依赖!!!

在这里插入图片描述

参考:https://blog.csdn.net/m0_61849361/article/details/124763130

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐