Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
758 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 Answer

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>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...