我已经创建了一个类来处理服务调用期间的运行时异常抛出,而我的代码是
@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();
}
我的问题是它在服务调用期间处理除INTERNAL SERVER ERROR之外的所有其他异常抛出。请帮助我在REST运行时如何处理INTERNAL SERVER ERROR。
提前致谢
答案 0 :(得分:0)
我不知道您使用的是哪个网络服务器,但只要它符合JEE specs
,您就可以在web.xml
中处理它:
<错误页>
<错误代码> 500℃; /出错码>
&LT;地点&gt; /path/to/myErrorPage.html< /地点&gt;
&LT; /错误页&GT;