本文主要是介绍统一返回JsonResult踩坑的记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《统一返回JsonResult踩坑的记录》:本文主要介绍统一返回JsonResult踩坑的记录,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教...
统一返回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 vopythonid 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方法时
SCJKqqopring MVC 在序列化时无法将对象正确转换为 JSON,因此会被视为 视图名称,导致循环视China编程图渲染的问题。
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 error org.springframework.web.HttpMediaTypeNotjsAcceptableException: Could not find acceptable representation Resolved [ojsrg.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" }
报错不够清晰准确,导致排查了一圈。
总结
这篇关于统一返回JsonResult踩坑的记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!