如何使用JAVA处理REST API中的内部服务器错误? [英] How to handle Internal Server Error in REST API using JAVA?

查看:56
本文介绍了如何使用JAVA处理REST API中的内部服务器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个类来处理服务调用期间的运行时异常引发,并且我的代码是

I have create a class to handle runtime exception throws during service call and my code is

@Override
public Response toResponse(WebApplicationException exception) {
    Response response=exception.getResponse();
    if(response.getStatus()==400)
        return Response.status(Response.Status.BAD_REQUEST).entity("Invalid data format. Please enter the data in xml format").build();
    else if(response.getStatus()==403)
        return Response.status(Response.Status.FORBIDDEN).entity("You are not authorize to access this resource").build();
    else if(response.getStatus()==500)
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Internal server error has occured.Please check wheather you have passed valid data or not").build();
    else if(response.getStatus()==404)
        return Response.status(Response.Status.NOT_FOUND).entity("Invalid url. Please enter a valid url").build();
    else if(response.getStatus()==415)
        return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity("Invalid media type. Please select the correct media type").build();
    else if(response.getStatus()==503)
        return Response.status(Response.Status.SERVICE_UNAVAILABLE).entity("Server is not available. Please try after some time").build();
    else
        return Response.status(Response.Status.OK).entity("").build();
}

我的问题是,它处理服务调用期间除内部服务器错误以外的所有其他异常引发.请帮我在REST的运行期间如何处理内部服务器错误.

my problem is that it handles all the other exception throws during service calls except INTERNAL SERVER ERROR. Please help me how to handle INTERNAL SERVER ERROR during run time in REST.

预先感谢

推荐答案

我不知道您使用的是哪种Web服务器,但是只要它符合 JEE规范,您就可以在您的 web.xml :

I don't know what webserver you are using, but as long as it adheres to JEE specs you could simply handle it in your web.xml:

<错误页面>
   <错误代码> 500</错误代码>
   < location>/path/to/myErrorPage.html</location>
</error-page>

<error-page>
    <error-code>500</error-code>
    <location>/path/to/myErrorPage.html</location>
</error-page>

这篇关于如何使用JAVA处理REST API中的内部服务器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆