2013-05-14 89 views
1

我使用this tutorrial和我創建此 表(ID,用戶名)打印; 現在我想使用RESTful Web服務我怎樣才能使用put方法插入數據到我的MySQL數據庫使用寧靜web服務

enter image description here

我應該把在內容文本框中輸入要做到這一點插入數據呢?

這是我的油漆類

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package entities; 

import java.io.Serializable; 
import javax.persistence.Basic; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.NamedQueries; 
import javax.persistence.NamedQuery; 
import javax.persistence.Table; 
import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Size; 
import javax.xml.bind.annotation.XmlRootElement; 

/** 
* 
* @author subhi 
*/ 
@Entity 
@Table(name = "prints") 

@XmlRootElement(名稱= 「打印」) @NamedQueries({ @NamedQuery(名稱= 「Prints.findAll」,查詢=「選擇P檔FROM打印p 「), @NamedQuery(name =」Prints.findById「,query =」SELECT p FROM Prints p WHERE p.id =:id「), @NamedQuery(name =」Prints.findByUsername「,query =」SELECT p FROM Prints p WHERE p.username =:username「)}) public class Prints implements Serializable {private static final long serialVersionUID = 1L; @Id @Basic(可選= false) @NotNull @Column(name =「id」) private Integer id; @Basic(可選= false) @NotNull @Size(min = 1,max = 30) @Column(name =「username」) private String username;

public Prints() { 
    } 

    public Prints(Integer id) { 
     this.id = id; 
    } 

    public Prints(Integer id, String username) { 
     this.id = id; 
     this.username = username; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (id != null ? id.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 Prints)) { 
      return false; 
     } 
     Prints other = (Prints) object; 
     if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "entities.Prints[ id=" + id + " ]"; 
    } 

} 

我試圖把使用方法,但我得到這個錯誤 enter image description here

,爲什麼我沒有POST方法在組合框中?

控制器代碼的Java EE 6

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package service; 

import entities.Prints; 
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; 
import org.netbeans.saas.RestResponse; 
import org.netbeans.saas.google.GoogleMapService; 

/** 
* 
* @author subhi 
*/ 
@Stateless 
@Path("entities.prints") 
public class PrintsFacadeREST extends AbstractFacade<Prints> { 

    @PersistenceContext(unitName = "test3PU") 
    private EntityManager em; 

    public PrintsFacadeREST() { 
     super(Prints.class); 
    } 

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

    @PUT 
    @Override 
    @Consumes({"application/xml", "application/json"}) 
    public void edit(Prints 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 Prints find(@PathParam("id") Integer id) { 
     return super.find(id); 
    } 

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

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

    @GET 
    @Produces("text/html") 
    public String getGoogleMap() { 
// Drag and drop the getGoogleMap operation here 

     try { 
      String address = "16 Network Circle, Menlo Park"; 
      java.lang.Integer zoom = 15; 
      String iframe = "false"; 

      RestResponse result = GoogleMapService.getGoogleMap(address, zoom, iframe); 
      return result.getDataAsString(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

     return ""; 
    } 
} 

回答

0

把請求主體部分there.If你要更新一些實體,那麼你應該通過與數據的DTO應該是updated.I沒有經過你link.this部分可以在JSON或XML。(如下圖所示)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<prints> 
<id>1</id> 
<username>test</username> 
</prints> 

你必須註釋你的DTO可以說printsDTO如下

@XmlRootElement(name = "prints") 
public class PrintsDTO { 
private int id; 
private String username; 
// have getters and setteres for above 
} 
+0

你可以給我一個例子嗎? – 2013-05-15 06:50:48

+0

@MohammedSubhiSheikhQuroush我編輯答案加入示例 – 2013-05-15 07:39:17

+0

我編輯了問題 – 2013-05-15 08:23:23

0

我使用的代碼將數據插入到使用RESTful客戶端應用程序我的RESTful Web服務作爲跟隨

NewJerseyClient client = new NewJerseyClient(); 
     Prints p = new Prints(); 
     p.setId(235); 
     p.setUsername("subhi"); 
     client.create_XML(p); 

,這足以讓我的情況