Java EE 7 Restful Webservice POST方法需要ID类型Autoincrement

时间:2013-08-21 14:09:33

标签: web-services rest java-ee java-ee-7

我使用this Tutorial使用Netbean 7.3.1创建简单的Java EE 7 Web服务。

它运行成功,我正在测试使用GET,PUT,POST,DELETE。除了POST之外,这三个都正常工作。

我发布了带有ID的json数据,工作正常。

{"id":"2","address1":"pe3","address2":"address line1","address3":"Address line3","city":"City Name","town":"Town Name","country":"uk","postcode":"123123"}

但是因为我的ID是自动递增的,所以当我在没有id的情况下在json之后POST时,它会失败并出现以下错误

{"address1":"pe3","address2":"address line1","address3":"Address line3","city":"City Name","town":"Town Name","country":"uk","postcode":"123123"}

错误是

HTTP Status 400 - Bad Request

type Status report

messageBad Request

descriptionThe request sent by the client was syntactically incorrect.
GlassFish Server Open Source Edition 4.0

任何人都可以解释为什么我需要在发送POST时发送数据库中自动增量类型的ID?有什么工作吗?

实体类中的代码是

.......
        @Id
        @Basic(optional = false)
        @NotNull
        @Column(name = "id")
        private Integer id;

        @Size(max = 45)
        @Column(name = "address1")
        private String address1;

        @Size(max = 45)
        @Column(name = "address2")
........

和AddressFacadeREST.java类看起来像这样。

package entities.service;

import entities.Address;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;


@Stateless
@Path("entities.address")
public class AddressFacadeREST extends AbstractFacade<Address> {
    @PersistenceContext(unitName = "CustomerDBPU")
    private EntityManager em;

    public AddressFacadeREST() {
        super(Address.class);
    }

    @POST
    @Override
    @Consumes({"application/xml", "application/json"})
    public void create(Address entity) {
        super.create(entity);
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Address entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Address find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Address> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Address> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

}

1 个答案:

答案 0 :(得分:1)

错误是由jax-rs服务无法将提供的json转换为实体类的实例而生成的。根据NetBeans如何创建实体类和服务外观,您的实体类只有一个非空构造函数,并且它需要id作为输入参数。此外,@NotNull类中有@Id注释。如果是这样的话:

  • 创建一个构造函数,该构造函数接受除@Id属性之外的所有实体属性。这样,服务就能将传入的json转换为实体类的实例。
  • 删除@NotNull注释,以避免JavaBeans Validator在持久保存新实体时引发错误。