当前位置: 首页 > news >正文

统一返回JsonResult踩坑

定义了一个统一返回类,但是没有给@Data 导致没有get/set方法,请求一直报错

public class JsonResult<T> {private int code;private String message;private T data;public JsonResult() {}public JsonResult(int code, String message, T data) {this.code = code;this.message = message;this.data = data;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public T getData() {return data;}public void setData(T data) {this.data = data;}public static <T> JsonResult<T> success(T data) {return new JsonResult<>(200, "Success", data);}
}

在使用时,JsonResult 没有 get/set 方法时,Spring MVC 在序列化时无法将对象正确转换为 JSON,因此会被视为 视图名称,导致循环视图渲染的问题。

Completed initialization in 2 ms
GET "/api/getUser", parameters={}
Mapped to kayou.eim.controller.BasicController#users()
Using 'application/octet-stream', given [*/*] and supported [*/*]
Using @ExceptionHandler kayou.eim.controller.global.GlobalExceptionHandler#handleException(Except
Internal server errororg.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representationResolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
Completed 406 NOT_ACCEPTABLE
"ERROR" dispatch for GET "/api/error", parameters={}
Mapped to kayou.eim.controller.global.CustomErrorController#error(HttpServletRequest)
Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, appli
Writing [{timestamp=Wed May 07 18:09:17 CST 2025, status=406, error=Not Acceptable, path=/api/getUser}]
Exiting from "ERROR" dispatch, status 406

响应

{"timestamp": 1746612557176,"status": 406,"error": "Not Acceptable","path": "/api/getUser"
}

报错不够清晰准确,导致排查了一圈

http://www.xdnf.cn/news/332299.html

相关文章:

  • 今日行情明日机会——20250507
  • 前端三大件--HTML
  • svn服务器迁移
  • BK精密电源操作软件 9130BA系列和手侧user manual
  • 文生图(Text-to-Image)的发展
  • 人工智能如何进行课堂管理?
  • 分布式爬虫去重:Python + Redis实现高效URL去重
  • 多账号管理、反追踪与自动化测试:我的浏览器实战笔记
  • string--OJ4
  • LeetCode106_从中序与后序遍历序列构造二叉树
  • 迈向AI辅助数据分析代码生成的透明性与知识共享
  • #黑马点评#(三)缓存穿透/雪崩/击穿
  • hadoop中的序列化和反序列化(1)
  • MySQL的information_schema在SQL注入中的关键作用与防御策略
  • 由浅入深谈Python书写规范
  • 【MySQL】-- 联合查询
  • Linux:进程控制1
  • 如何利用 QuickAPI 生成 PostgreSQL 样本测试数据:全面解析与实用指南
  • vue-qr生成的二维码增加下载功能
  • 【云备份】客户端开发
  • 百胜企业管理咨询:助力企业快速获得ecovadis认证
  • SecureCRT SFTP命令详解与实战
  • S32K3 HSE模块安装
  • 屏蔽力 | 在复杂世界中从内耗到成长的转变之道
  • STM32开发printf函数支持
  • LeetCode:二叉树的最大深度
  • React Native主题切换、字号调整:不用styled-components也能玩出花
  • 查询nvidia边缘设备的软硬件版本jetson_release
  • 【软件设计师:程序语言】4.程序语言基础知识
  • Unity-Socket通信实例详解