前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]

The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]

作者头像
全栈程序员站长
发布2022-07-31 16:29:59
3.3K0
发布2022-07-31 16:29:59
举报

大家好,又见面了,我是你们的朋友全栈君。

一个异常引起的乌龙,HTTPStatus500问题的一种场景及解决办法

一、前言

这是我在编写服务器响应判断用户数据时遇到的问题,这只是 The server encountered an internal error that prevented it from fulfilling this request 问题的一种情况,具体错误如下图所示:

The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]

二、问题描述

我在设置某一属性 grade 时,设置为 int<11> 。在测试数据时,表单提交数据超出原设定范围,所引起的异常。

2-1 问题解决的方法

对异常捕获,仅仅捕获了 SQLException,导致其他异常出现时,被抛出。

代码语言:javascript
复制
try{
    // ...
} catch(SQLException e) {
    e.printStackTrace();
}

捕获异常 Exception 即可

代码语言:javascript
复制
try{
    // ...
} catch(Exception e) {
    e.printStackTrace();
}

2-2 问题出现与解决

在输入信息栏输入正确的信息,会给出正确的提示

The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]

处理后正确的响应结果如右图所示

The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]

三、问题解决

3-1 解决思路一

其实问题的解决方法就是规范化开发,对填写信息的文本域填写的内容动态的监控,定义可输入的内容为数字,大小写英文,不允许输入特殊字符等控制。比如注册用户时填写用户 ID 时可通过 Ajax 动态获取后台数据,验证该 ID 是否已存在,若存在在注册页面则提示该 ID 已被注册

3-2 解决思路二

此处我使用的是这个思路来避免该问题。当输入信息错误时,给出了不是我所写的错误处理办法,(我的错误处理办法是,给出提示:系统繁忙,稍后操作!)输入超出原定范围的数据。此处通过捕获抛出的异常进行处理,跳转到一个操作失败页面

The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]

点击确定后出现的错误如下:

The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]

回去检查文件代码,多次修改,发现该问题的解决办法:

修改前的代码:

The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]

修改后的代码:(红线已标出)

The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]

保存修改,重新运行程序,

输入错误的数据,给出了我所设定的处理方法:(下图所示)

The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]
The server encountered an internal error that prevented it from fulfilling this request的一种解决办法[通俗易懂]

当出现异常时,认真检查代码的正确性,或许就是一个字符,单词的大小写或拼写错误,都够你调试一下午的。养成良好的代码风格,也是必须的。

四、其他异常补充

4.1 空指针异常(NullPointerException 先看一下)

代码语言:javascript
复制
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 实体类对象转换异常

此转换对象不能直接强制转换为被转换对象。

代码语言:javascript
复制
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数据传参异常,不能将非数字转换为数字

代码语言:javascript
复制
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 执行写操作。

代码语言:javascript
复制
HTTP Status 500 - java.lang.IllegalStateException:Cannot forward a response that is already committd

4.5 前后台交互数据类型不匹配

后台开发查询用户接口,访问接口

http://localhost:8082/dboot/eouser/queryEOUser,请求JSON数据为

代码语言:javascript
复制
{  "treasureData": "{\"userName\":\"78\",\"userId\":\"2147483648\",\"userPassword\":\"930915\"}"}

后台响应结果

代码语言:javascript
复制
{
    "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之间的整型数据,重新请求接口,后台正常处理请求并返回结果。

代码语言:javascript
复制
{
    "eoUserList": [
        {
            "userId": 1,
            "userName": "eolinker",
            "userNickName": "eolinker",
            "userPassword": "c0bc7b2052c950c1541692eab1284937"
        }
    ],
    "errorMsg": "查询用户成功!",
    "success": true
}

4.6 Mybatis解析实体属性错误

此处异常为类型异常;异常信息如下

代码语言:javascript
复制
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导致解析数据报错,服务器无法处理请求。

代码语言:javascript
复制
<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数据存在格式错误,嵌套异常,语法错误。

代码语言:javascript
复制
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值嵌套的数据没有完全用{}封装。

代码语言:javascript
复制
修改前:
"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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、前言
  • 二、问题描述
    • 2-1 问题解决的方法
      • 2-2 问题出现与解决
      • 三、问题解决
        • 3-1 解决思路一
          • 3-2 解决思路二
          • 四、其他异常补充
            • 4.1 空指针异常(NullPointerException 先看一下)
              • 4.2 实体类对象转换异常
                • 4.3 JSON参数转换异常
                  • 4.4 服务器响应already committed异常
                    • 4.5 前后台交互数据类型不匹配
                      • 4.6 Mybatis解析实体属性错误
                        • 4.7 请求数据类型语法错误
                        • 五、其他说明
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档