Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 views
in Technique[技术] by (71.8m points)

web services - How to handle Internal Server Error in REST API using JAVA?

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();
}

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.

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...