0

我对 JavaEE 和 Web 服务还比较陌生,但是,我使用 netbeans 来生成我的客户端和 Web 服务资源。我有一个代表 mysql 数据库表的资源“CustomerData”和一个代表该表中列的值“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.

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

4

1 回答 1

0

该异常与 Web 服务几乎没有关系:它与Bean Validation相关。

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

但是有些东西在方法中不起作用addPoints

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

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

于 2013-11-05T09:24:23.457 回答