title: JAX-RS using exception mappers
body: <p>I have read that I can create an implementation of <code>javax.ws.rs.ext.ExceptionMapper</code> that will map a thrown application exception to a <code>Response</code> object.  </p>
  
  <p>I've created a simple example which throws an exception if the phone length is greater than 20 characters when persisting the object. I am expecting the exception to be mapped to an HTTP 400 (Bad Request) response; however, I am receiving an HTTP 500 (Internal Server Error) with the following exception:</p>
  
  <pre><code>java.lang.ClassCastException: com.example.exception.InvalidDataException cannot be cast to java.lang.Error
  </code></pre>
  
  <p>What am I missing? Any advice is greatly appreciated.</p>
  
  <p>Exception mapper:</p>
  
  <pre><code>@Provider
  public class InvalidDataMapper implements ExceptionMapper&lt;InvalidDataException&gt; {
  
      @Override
      public Response toResponse(InvalidDataException arg0) {
          return Response.status(Response.Status.BAD_REQUEST).build();
      }
  
  }
  </code></pre>
  
  <p>Exception class:</p>
  
  <pre><code>public class InvalidDataException extends Exception {
  
      private static final long serialVersionUID = 1L;
  
      public InvalidDataException(String message) {
          super(message);
      }
  
      ...
  
  }
  </code></pre>
  
  <p>Entity class:</p>
  
  <pre><code>@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() &gt; 20) {
                  throw new InvalidDataException("Phone number too long: " + phone);
              }
          }       
      }
  }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>@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&lt;Person&gt; personJaxb) {
          Person person = personJaxb.getValue();
          em.persist(person);
          em.flush();
          URI personUri = uriInfo.getAbsolutePathBuilder().
          path(person.getId().toString()).build();
          return Response.created(personUri).build();  
      }
  
  }
  </code></pre>