0

我是 JAVA 新手,我正在尝试使用 netbeans IDE 开发 restful web 服务,我有 MYSQL 数据库。我参考了 netbeans 教程来开发这个服务。我已经向实体类添加了一个方法,它返回基于行公司名称通过。我尝试了选项 test restful services,它给了我左侧的服务列表,但是当我尝试测试服务(我在实体类中手动添加的方法)时,它给了我一个 HTTP 404 错误:未找到请求资源。我不明白问题出在哪里。我使用的是 Glassfish 服务器。任何人都可以在这方面帮助我。谢谢。以下是我的代码:

1.AbstractFacade.java

public abstract class AbstractFacade<T> {
    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<T> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }
}

2.MyTableFacadeREST.java

@Stateless
@Path("com.entities.mytable")
public class MyTableFacadeREST extends AbstractFacade<MyTable> {
    @PersistenceContext(unitName = "WSDataBasePU")
    private EntityManager em;

    public MyTableFacadeREST() {
        super(MyTable.class);
    }

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

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

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

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

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

    @GET
    @Path("{from}/{to}")
    @Produces({"application/json", "application/xml"})
    public List<MyTable> 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());
    }

//added method to fetch rows corresponding to company passed
   @GET
    @Path("companyname/{companyname}")
    @Produces({"application/json", "application/xml"})
    public List<MyTable> findByCompanyname(@PathParam("companyname") String name){
       return (List<MyTable>)(MyTable)em.createNamedQuery("MyTable.findByCompanyname").setParameter("companyname",name).getResultList();
    }
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

}

3.MyTable.java

@Entity
@Table(name = "MyTable")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "MyTable.findAll", query = "SELECT m FROM MyTable m"),
    @NamedQuery(name = "MyTable.findByCompanyid", query = "SELECT m FROM MyTable m WHERE m.companyid = :companyid"),
    @NamedQuery(name = "MyTable.findByCompanyname", query = "SELECT m FROM MyTable m WHERE m.companyname = :companyname"),
    @NamedQuery(name = "MyTable.findByCompanyasset", query = "SELECT m FROM MyTable m WHERE m.companyasset = :companyasset"),
    @NamedQuery(name = "MyTable.findByAssetname", query = "SELECT m FROM MyTable m WHERE m.assetname = :assetname"),
    @NamedQuery(name = "MyTable.findByAssetdescription", query = "SELECT m FROM MyTable m WHERE m.assetdescription = :assetdescription")})
public class MyTable implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "Company_id")
    private Integer companyid;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "Company_name")
    private String companyname;
    @Size(max = 255)
    @Column(name = "Company_asset")
    private String companyasset;
    @Size(max = 255)
    @Column(name = "Asset_name")
    private String assetname;
    @Size(max = 255)
    @Column(name = "Asset_description")
    private String assetdescription;

    public MyTable() {
    }

    public MyTable(Integer companyid) {
        this.companyid = companyid;
    }

    public MyTable(Integer companyid, String companyname) {
        this.companyid = companyid;
        this.companyname = companyname;
    }

    public Integer getCompanyid() {
        return companyid;
    }

    public void setCompanyid(Integer companyid) {
        this.companyid = companyid;
    }

    public String getCompanyname() {
        return companyname;
    }

    public void setCompanyname(String companyname) {
        this.companyname = companyname;
    }

    public String getCompanyasset() {
        return companyasset;
    }

    public void setCompanyasset(String companyasset) {
        this.companyasset = companyasset;
    }

    public String getAssetname() {
        return assetname;
    }

    public void setAssetname(String assetname) {
        this.assetname = assetname;
    }

    public String getAssetdescription() {
        return assetdescription;
    }

    public void setAssetdescription(String assetdescription) {
        this.assetdescription = assetdescription;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (companyid != null ? companyid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof MyTable)) {
            return false;
        }
        MyTable other = (MyTable) object;
        if ((this.companyid == null && other.companyid != null) || (this.companyid != null && !this.companyid.equals(other.companyid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.entities.MyTable[ companyid=" + companyid + " ]";
    }

}
4

2 回答 2

0

问题可能是您没有为您的服务定义基本 url。您需要告诉 JAX-RS(Jersey 是 GlassFish 中的实现)它必须拦截哪个 url 模式作为您的端点(基本 url)。

实现此目的的一种方法是使用可以添加到项目中任何包的应用程序类(您也可以在 web.xml 中定义必要的)。请参阅此答案以获取简单的 Application Class

于 2014-06-04T16:13:48.207 回答
0

首先,看看你的基本网址是什么。为此,您可以尝试在 netbeans 中单击运行(在项目中双击)。然后添加到基本 url “/com.entities.mytable”,如资源中的注释路径所示。

于 2012-12-07T16:03:39.137 回答