我是JAVA的新手,我正在尝试使用netbeans IDE开发restful web服务,我有MYSQL数据库。我已经参考了netbeans教程来开发这个服务。我已经向实体类添加了一个方法返回传递了基于公司名称的行。我尝试了测试restful服务的选项,它给了我左侧的服务列表,但是当我尝试测试服务时(我在实体类中手动添加的方法)它给了我一个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 + " ]";
}
}
答案 0 :(得分:0)
首先,看看您的基本网址是什么。为此,您可以尝试单击netbeans中的run(在项目中双击)。然后添加到基本网址“/com.entities.mytable”,如资源中的注释路径所示。
答案 1 :(得分:0)
问题可能是您没有为您的服务定义基本网址。您需要告诉JAX-RS(Jersey是GlassFish中的实现),它必须截取哪个url模式作为您的端点(基本URL)。
实现此目的的一种方法是使用可以添加到项目中任何包的Application Class(您也可以在web.xml中定义必要的)。 See this answer for a simple Application Class