私は Java を初めて使用し、netbeans IDE を使用して安らかな Web サービスを開発しようとしています。MYSQL データベースがあります。このサービスを開発するために netbeans チュートリアルを参照しました。行ベースを返すエンティティ クラスにメソッドを追加しました。会社名が渡されました。オプションのテストrestfulサービスを試してみましたが、左側にサービスのリストが表示されましたが、サービス(エンティティクラスに手動で追加したメソッド)をテストしようとすると、HTTP 404エラーが発生します:リクエスト リソースが見つかりません。どこに問題があるのかわかりません。Glassfish サーバーを使用しています。この点で誰でも私を助けることができます.advanceに感謝します. 以下は私のコードです:
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 + " ]";
}
}