欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 统一返回JsonResult踩坑

统一返回JsonResult踩坑

2025/5/9 21:09:26 来源:https://blog.csdn.net/FLGBgo/article/details/147771538  浏览:    关键词:统一返回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"
}

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词