統(tǒng)一返回JsonResult踩坑的記錄
統(tǒng)一返回JsonResult踩坑
定義了一個(gè)統(tǒng)一返回類
但是沒有給@Data 導(dǎo)致沒有g(shù)et/set方法,請(qǐng)求一直報(bào)錯(cuò)
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);
}
}
在使用時(shí),JsonResult沒有g(shù)et/set方法時(shí)
Spring MVC 在序列化時(shí)無法將對(duì)象正確轉(zhuǎn)換為 JSON,因此會(huì)被視為 視圖名稱,導(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"
}報(bào)錯(cuò)不夠清晰準(zhǔn)確,導(dǎo)致排查了一圈。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解關(guān)于SpringBoot的外部化配置使用記錄
這篇文章主要介紹了詳解關(guān)于SpringBoot的外部化配置使用記錄,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Java值得使用Lambda的8個(gè)場(chǎng)景合集
可能對(duì)不少人來說,Lambda顯得陌生又復(fù)雜,覺得Lambda會(huì)導(dǎo)致代碼可讀性下降,但畢竟2023年了,JDK都出了那么多新版本,是時(shí)候試試Lambda了2023-08-08
SpringBoot Admin2.0 集成Arthas的實(shí)現(xiàn)步驟
這篇文章主要介紹了SpringBoot Admin2.0 集成Arthas的實(shí)現(xiàn)步驟,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下2021-04-04
java獲取指定開始時(shí)間與結(jié)束時(shí)間之間的所有日期
這篇文章主要為大家詳細(xì)介紹了java獲取指定開始時(shí)間與結(jié)束時(shí)間之間的所有日期,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
SpringBoot使用Nacos進(jìn)行application.yml配置管理詳解
Nacos是阿里巴巴開源的一個(gè)微服務(wù)配置管理和服務(wù)發(fā)現(xiàn)的解決方案,下面我們來看看在SpringBoot中如何使用Nacos進(jìn)行application.yml配置管理吧2025-03-03
如何解決EasyExcel導(dǎo)出文件LocalDateTime報(bào)錯(cuò)問題
這篇文章主要介紹了如何解決EasyExcel導(dǎo)出文件LocalDateTime報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
使用Spring Validation實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的代碼詳解
在現(xiàn)代Web應(yīng)用開發(fā)中,數(shù)據(jù)校驗(yàn)是不可忽視的重要環(huán)節(jié),Spring提供了強(qiáng)大的數(shù)據(jù)校驗(yàn)框架——Spring Validation,可以有效提升數(shù)據(jù)輸入的安全性與應(yīng)用的穩(wěn)定性,本文將介紹如何使用Spring Validation進(jìn)行數(shù)據(jù)校驗(yàn),幫助您深入理解和靈活應(yīng)用這一技術(shù)2024-11-11

