Java RESTful Webservice中的ConstraintViolationException

时间:2013-11-05 08:53:22

标签: web-services rest java-ee glassfish ejb

我对JavaEE和Web服务相对较新,但是,我使用netbeans来生成我的客户端和Web服务资源。我有一个资源“CustomerData”表示一个mysql数据库表和一个值“rewardsPoints”表示该表中的列,但是,由于ConstraintViolationException,我无法更新该值,具体来说:

javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'preUpdate'. Please refer to embedded ConstraintViolations for details.

我不熟悉'preUpdate'回调事件,是否需要覆盖?我似乎无法弄清楚为什么会抛出这个异常,但正如我所说,我对Web服务编程非常陌生。这是我的课程:

@Stateless
@Path("customers")
public class CustomerDataFacadeREST extends AbstractFacade<CustomerData> {
    @PersistenceContext(unitName = "CustomerPortalPU")
    private EntityManager em;
    public CustomerDataFacadeREST() {
        super(CustomerData.class);
    }

    @PUT
    @Path("{id}")
    @Consumes({"application/xml", "application/json"})
    public void edit(@PathParam("id") Integer id, CustomerData entity) {
        super.edit(entity);
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public CustomerData find(@PathParam("id") Integer id) {
        return super.find(id);
    }
    @GET
    @Path("addPoints/{id}/{amount}")
    @Produces({"text/plain"})
    public String addPoints(@PathParam("id") Integer id, @PathParam("amount") int amount) {
        CustomerData customer = find(id);
    customer.getRewardsPoints(customer.getRewardsPoints() + amount);
    em.persist(customer);
    edit(customer);
    return customer.getRewardsPoints();
    }
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
}

CustomerData实体类:

@Entity
@Table(name = "tbl_customer_data")
@XmlRootElement
public class CustomerData implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Column(name = "rewards_points")
    private int rewardsPoints;
    public CustomerData(Integer id, int rewardsPoints) {
        this.id = id;
        this.rewardsPoints = rewardsPoints;
    }   
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public int getRewardsPoints() {
        return rewardsPoints;
    }
    public void setRewardsPoints(int rewardsPoints) {
        this.rewardsPoints = rewardsPoints;
    }
}

当我尝试访问URI时:

http://localhost:8080/CustomerPortal/ws/customers/addPoints/1/5 

向id为1的用户添加5分我得到HTTP 500错误,并在glassfish日志中显示

[2013-11-05T03:28:11.733-0500] [glassfish 4.0] [WARNING] [ejb.system_exception] [javax.enterprise.system.container.ejb.com.sun.ejb.containers] [tid: _ThreadID=21 _ThreadName=http-listener-1(3)] [timeMillis: 1383640091733] [levelValue: 900] [[
  EJB5184:A system exception occurred during an invocation on EJB CustomerDataFacadeREST, method: public java.lang.String com.webservice.entities.CustomerDataFacadeREST.addPoints(java.lang.Integer,int)]]

[2013-11-05T03:28:11.741-0500] [glassfish 4.0] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=21 _ThreadName=http-listener-1(3)] [timeMillis: 1383640091741] [levelValue: 900] [[
  StandardWrapperValve[com.webservice.entities.ApplicationConfig]: Servlet.service() for servlet com.webservice.entities.ApplicationConfig threw exception
javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'preUpdate'. Please refer to embedded ConstraintViolations for details.

有关此问题的任何资源,见解或信息都将非常有用。

1 个答案:

答案 0 :(得分:0)

该例外与Web服务几乎没有关系:它与Bean Validation有关。

在这种情况下,由于验证在方法addPoints内失败(查看堆栈跟踪),唯一可以导致它的行是在持久化或编辑类型为CustomerData的实体时。您在该类中唯一的约束是rewardsPoints不应为null。所以,这就是异常的原因。

但有些事情在addPoints方法中无效:

  • 您应该检查find()方法是否返回空对象。
  • customer.getRewardsPoints(customer.getRewardsPoints() + amount)从不设置属性(它是否编译?)
  • 如果实体已存在,则
  • EntityManager.persist()抛出异常。如果您只想编辑(更新)实体,则可能需要删除该行。

注意:我不确定您发布的代码是否真的正在编译并导致该异常。这可能是由另一个版本引起的。