我对 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.
有关此问题的任何资源、见解或信息都将非常有帮助。