2010-07-20 49 views
29

我讀過,我可以創建一個javax.ws.rs.ext.ExceptionMapper的實現,它將引發的應用程序異常映射到Response對象。使用異常映射器的JAX-RS

我創建了一個簡單的例子,如果手機長度大於20個字符時持續對象引發異常。我希望將異常映射到HTTP 400(錯誤請求)響應;不過,我收到一個HTTP 500(內部服務器錯誤),但下列情況除外:

java.lang.ClassCastException: com.example.exception.InvalidDataException cannot be cast to java.lang.Error 

我缺少什麼?任何意見是極大的讚賞。

異常映射器:

@Provider 
public class InvalidDataMapper implements ExceptionMapper<InvalidDataException> { 

    @Override 
    public Response toResponse(InvalidDataException arg0) { 
     return Response.status(Response.Status.BAD_REQUEST).build(); 
    } 

} 

異常類:

public class InvalidDataException extends Exception { 

    private static final long serialVersionUID = 1L; 

    public InvalidDataException(String message) { 
     super(message); 
    } 

    ... 

} 

實體類:

@Entity 
@Table(name="PERSON") 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlRootElement 
public class Person { 

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name="ID") 
    private Long id; 

    @Column(name="NAME") 
    private String name; 

    @Column(name="PHONE") 
    private String phone; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getPhone() { 
     return phone; 
    } 

    public void setPhone(String phone) { 
     this.phone = phone; 
    } 

    @PrePersist 
    public void validate() throws InvalidDataException { 
     if (phone != null) { 
      if (phone.length() > 20) { 
       throw new InvalidDataException("Phone number too long: " + phone); 
      } 
     }  
    } 
} 

服務:

@Path("persons/") 
@Produces(MediaType.APPLICATION_XML) 
@Consumes(MediaType.APPLICATION_XML) 
@Stateless 
public class PersonResource { 

    @Context 
    private UriInfo uriInfo; 

    @PersistenceContext(name="simple") 
    private EntityManager em; 

    @POST 
    public Response createPerson(JAXBElement<Person> personJaxb) { 
     Person person = personJaxb.getValue(); 
     em.persist(person); 
     em.flush(); 
     URI personUri = uriInfo.getAbsolutePathBuilder(). 
     path(person.getId().toString()).build(); 
     return Response.created(personUri).build(); 
    } 

} 

回答

31

InvalidDataException是否包裝在PersistenceException中?也許你可以不喜歡下面的內容:

@Provider 
public class PersistenceMapper implements ExceptionMapper<PersistenceException> { 

    @Override 
    public Response toResponse(PersistenceException arg0) { 
     if(arg0.getCause() instanceof InvalidDataException) { 
      return Response.status(Response.Status.BAD_REQUEST).build(); 
     } else { 
      ... 
     } 
    } 

} 
+2

謝謝布萊斯,那工作。爲什麼我必須在PersistenceException中包裝InvalidDataException?根據我的書籍之一,它表示如果註冊異常映射器,JAX-RS運行時將處理任何拋出的異常。就我而言,我爲InvalidDataException註冊了一個異常映射器... – 2010-07-28 19:30:50

+4

JPA實現將捕獲InvalidDataException並將其包裝在PersistenceException中。然後你的JAX-RS實現將會得到一個PersistenceException。這就是爲什麼你需要打開它。 – 2010-07-28 19:45:13

+0

另請參閱http://stackoverflow.com/questions/3249495/call-exceptionmapper-from-another-exceptionmapper-in-jax-rs – 2010-07-28 19:52:04

-1

交叉檢查你的web.xml,你需要註冊你的「PersistenceMapper」級還與服務一起。