統(tǒng)一返回JsonResult踩坑的記錄
統(tǒng)一返回JsonResult踩坑
定義了一個統(tǒng)一返回類
但是沒有給@Data 導(dǎo)致沒有g(shù)et/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沒有g(shù)et/set方法時
Spring MVC 在序列化時無法將對象正確轉(zhuǎn)換為 JSON,因此會被視為 視圖名稱,導(dǎo)致循環(huán)視圖渲染的問題。
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.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
Resolved [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響應(yīng)
{
"timestamp": 1746612557176,
"status": 406,
"error": "Not Acceptable",
"path": "/api/getUser"
}報錯不夠清晰準(zhǔn)確,導(dǎo)致排查了一圈。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解關(guān)于SpringBoot的外部化配置使用記錄
這篇文章主要介紹了詳解關(guān)于SpringBoot的外部化配置使用記錄,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
SpringBoot Admin2.0 集成Arthas的實現(xiàn)步驟
這篇文章主要介紹了SpringBoot Admin2.0 集成Arthas的實現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下2021-04-04
SpringBoot使用Nacos進行application.yml配置管理詳解
Nacos是阿里巴巴開源的一個微服務(wù)配置管理和服務(wù)發(fā)現(xiàn)的解決方案,下面我們來看看在SpringBoot中如何使用Nacos進行application.yml配置管理吧2025-03-03
如何解決EasyExcel導(dǎo)出文件LocalDateTime報錯問題
這篇文章主要介紹了如何解決EasyExcel導(dǎo)出文件LocalDateTime報錯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
使用Spring Validation實現(xiàn)數(shù)據(jù)校驗的代碼詳解
在現(xiàn)代Web應(yīng)用開發(fā)中,數(shù)據(jù)校驗是不可忽視的重要環(huán)節(jié),Spring提供了強大的數(shù)據(jù)校驗框架——Spring Validation,可以有效提升數(shù)據(jù)輸入的安全性與應(yīng)用的穩(wěn)定性,本文將介紹如何使用Spring Validation進行數(shù)據(jù)校驗,幫助您深入理解和靈活應(yīng)用這一技術(shù)2024-11-11

