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