大家好,又見面了,我是你們的朋友全棧君。
一個異常引起的烏龍,HTTPStatus500問題的一種場景及解決辦法
一、前言
這是我在編寫伺服器響應判斷使用者資料時遇到的問題,這隻是 The server encountered an internal error that prevented it from fulfilling this request 問題的一種情況,具體錯誤如下圖所示:
二、問題描述
我在設定某一屬性 grade 時,設定為 int<11> 。在測試資料時,表單送出資料超出原設定範圍,所引起的異常。
2-1 問題解決的方法
對異常捕獲,僅僅捕獲了 SQLException,導緻其他異常出現時,被抛出。
try{
// ...
} catch(SQLException e) {
e.printStackTrace();
}
複制
捕獲異常 Exception 即可
try{
// ...
} catch(Exception e) {
e.printStackTrace();
}
複制
2-2 問題出現與解決
在輸入資訊欄輸入正确的資訊,會給出正确的提示
處理後正确的響應結果如右圖所示
三、問題解決
3-1 解決思路一
其實問題的解決方法就是規範化開發,對填寫資訊的文本域填寫的内容動态的監控,定義可輸入的内容為數字,大小寫英文,不允許輸入特殊字元等控制。比如注冊使用者時填寫使用者 ID 時可通過 Ajax 動态擷取背景資料,驗證該 ID 是否已存在,若存在在注冊頁面則提示該 ID 已被注冊
3-2 解決思路二
此處我使用的是這個思路來避免該問題。當輸入資訊錯誤時,給出了不是我所寫的錯誤處理辦法,(我的錯誤處理辦法是,給出提示:系統繁忙,稍後操作!)輸入超出原定範圍的資料。此處通過捕獲抛出的異常進行處理,跳轉到一個操作失敗頁面
點選确定後出現的錯誤如下:
回去檢查檔案代碼,多次修改,發現該問題的解決辦法:
修改前的代碼:
修改後的代碼:(紅線已标出)
儲存修改,重新運作程式,
輸入錯誤的資料,給出了我所設定的處理方法:(下圖所示)
當出現異常時,認真檢查代碼的正确性,或許就是一個字元,單詞的大小寫或拼寫錯誤,都夠你調試一下午的。養成良好的代碼風格,也是必須的。
四、其他異常補充
4.1 空指針異常(NullPointerException 先看一下)
HTTP Status 500 - Request processing failed; nested exception is
java.lang.NullPointerException
com.dorm.action.CounsellorAction.counsellorAdd(CounsellorAction.java:123)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
...
首先sun.* 或者org.*,都是源碼,這些資訊不是排查Bug 的重點
一般異常抛出是自己的業務代碼有漏洞, 才會觸發一系列的資訊抛出;
先找異常資訊中的Cause by ... 這是此次異常抛出的原因,
然後先看異常資訊中第一條和目前項目有關業務代碼,
看看資訊指定的類的方法某一行為什麼抛出空指針異常.
比如上面這個是自己寫的CounsellorAction.java 檔案中方法counsellorAdd 抛出的異常,
運作中發現第123 行存在為null 的對象.
複制
4.2 實體類對象轉換異常
此轉換對象不能直接強制轉換為被轉換對象。
HTTP Status 500 - Request processing failed; nested exception is java.lang.ClassCastException:
com.zduod.core.requestentity.RequestUser cannot be cast to com.zduod.core.requestentity.RequestPay
複制
4.3 JSON參數轉換異常
JSON資料傳參異常,不能将非數字轉換為數字
HTTP Status 500 - Request processing failed; nested exception is com.alibaba.fastjson.JSONException
...
root cause
java.lang.NumberFormatException
java.math.BigDecimal.<init>(BigDecimal.java:494)
java.math.BigDecimal.<init>(BigDecimal.java:383)
java.math.BigDecimal.<init>(BigDecimal.java:806)
com.alibaba.fastjson.util.TypeUtils.castToBigDecimal(TypeUtils.java:194)
...
複制
4.4 伺服器響應already committed異常
response 是服務端對用戶端請求的一個響應,其中封裝了響應頭、狀态碼、内容等;服務端在把response送出到用戶端之前,會向緩沖區内寫入響應頭和狀态碼,然後将所有内容flush,即将所有緩存輸出。這就标志着該次響應已經送出。
對于目前頁面already commitd 已經送出的response 就不能再使用response 執行寫操作。
HTTP Status 500 - java.lang.IllegalStateException:Cannot forward a response that is already committd
複制
4.5 前背景互動資料類型不比對
背景開發查詢使用者接口,通路接口
http://localhost:8082/dboot/eouser/queryEOUser,請求JSON資料為
{ "treasureData": "{\"userName\":\"78\",\"userId\":\"2147483648\",\"userPassword\":\"930915\"}"}
複制
背景響應結果
{
"timestamp": "2018-07-13T03:05:36.261+0000",
"status": 500,
"error": "Internal Server Error",
"message": "For input string: \"2147483648\"",
"path": "/dboot/eouser/queryEOUser"
}
複制
接口響應錯誤資訊為input輸入資料為String類型,背景請求體封裝實體類定義屬性userId為Integer類型。Integer.MAX_VALUE=2147483647,此處2147483648已經不能作為Integer處理。接口資料改為-2147483648 ~ 2147483647之間的整型資料,重新請求接口,背景正常處理請求并傳回結果。
{
"eoUserList": [
{
"userId": 1,
"userName": "eolinker",
"userNickName": "eolinker",
"userPassword": "c0bc7b2052c950c1541692eab1284937"
}
],
"errorMsg": "查詢使用者成功!",
"success": true
}
複制
4.6 Mybatis解析實體屬性錯誤
此處異常為類型異常;異常資訊如下
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException:
nested exception is org.apache.ibatis.reflection.ReflectionException:
There is no getter for property named 'useId' in 'class com.zduod.manage.face.entity.ZddInformation'
複制
此處是因為mybatis解析屬性錯誤,資訊為useId在實體類中無對應的setter()、getter()方法。檢查發現請求參數為userId,實體類屬性為userId,也存在對應的setter()、getter()方法。
此處配置檔案mapper.xml中錯寫userId為useId導緻解析資料報錯,伺服器無法處理請求。
<select id="getListByPage" resultMap="BaseResultMap" parameterType="com.zduod.manage.face.entity.ZddInformation">
select
<include refid="Base_Column_List" />
from zdd_information
<where>
<if test="informationId != null" >
and information_id = #{informationId,jdbcType=VARCHAR}
</if>
<if test="userId != null" >
<!--此處寫錯userId為#{useId,jdbcType=DECIMAL}-->
and user_id = #{userId,jdbcType=DECIMAL}
</if>
</where>
order by create_time desc
</select>
複制
4.7 請求資料類型文法錯誤
此處異常為JSON資料存在格式錯誤,嵌套異常,文法錯誤。
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Request processing failed; nested exception is com.alibaba.fastjson.JSONException: syntax error, expect {, actual string, pos 0
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.alibaba.fastjson.JSONException: syntax error, expect {, actual string, pos 0
複制
異常資訊是JSON資料存在文法錯誤,在 { 大括号嵌套時有問題。我這裡是前端是通過JSON資料請求後端接口,仔細檢查後JSON資料的格式存在如下問題:JSON請求資料中treasureData的value值嵌套的資料沒有完全用{}封裝。
修改前:
"treasureData": "{"userId":"2018071211901416892","userPayPassword":"100000","cashBalance":"80""
修改後:
"treasureData": "{"userId":"2018071211901416892","userPayPassword":"100000","cashBalance":"80"}"
複制
五、其他說明
500狀态碼,問題出現的情況多樣,請根據Exception資訊分析,進行debug斷點調試排查具體原因。
你可以把異常資訊貼出來,放到技術問答https://ask.csdn.net/去提個問題,會有人幫助你分析處理問題。
你可以把關鍵異常資訊貼在此文評論區,沒有異常資訊無法确定具體原因。
釋出者:全棧程式員棧長,轉載請注明出處:https://javaforall.cn/127860.html原文連結:https://javaforall.cn