1
我在我的項目澤西自定義異常沒有抓住Servlet類?
public class BadRequestException extends WebApplicationException {
private static final long serialVersionUID = 1L;
private String message;
public BadRequestException(String message) {
super();
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
我創建了一個映射類還使用下面自定義異常類..
public class BadRequestExceptionMapper implements ExceptionMapper<BadRequestException> {
public Response toResponse(BadRequestException brexec) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(brexec.getResponse().getEntity()).type(MediaType.APPLICATION_JSON).build();
}
}
我通過的Servlet叫着我的服務和拋出異常通過它的一種方法,但我無法在Servlet類中捕獲它。我使用下面的代碼來捕捉異常。
try{
//Some Business logic then
service.path("restful").path("jwbservice/" + methodName + "/" + id).header("lid", lid).delete(String.class);
}
catch (BadRequestException ex) {
out.println(ex);
}
catch(Exception exe){
out.println(exe);
}
而我在我的Service類中使用這個代碼的服務方法會拋出異常。
@DELETE
@Path("/deleteLink/{id}")
@Produces(MediaType.APPLICATION_JSON)
public String deleteLink(@PathParam("id") int id, @HeaderParam("lid") String lid) throws BadRequestException {
if (id<= 0) {
throw new BadRequestException("Required Parameter: id");
}
//Some Business Logic
}
我的服務拋的BadRequestException但在Servlet的它會異常抓不BadRequestException catch塊。 任何人都可以知道我做錯了什麼。
請關注[本](http://avianey.blogspot.in/2011/12/exception-mapping-jersey.html)並嘗試 – chiru 2013-03-25 13:42:23