Java 我需要一个关于如何使用RESTful from entity类创建的函数create的示例
我需要一个关于如何使用RESTful from entity类创建的函数create的示例 我的实体类别(国家): 一揽子实体Java 我需要一个关于如何使用RESTful from entity类创建的函数create的示例,java,web-services,rest,Java,Web Services,Rest,我需要一个关于如何使用RESTful from entity类创建的函数create的示例 我的实体类别(国家): 一揽子实体 import java.io.Serializable; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Name
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;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
*
* @author oracle
*/
@Entity
@Table(name = "COUNTRIES")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Countries.findAll", query = "SELECT c FROM Countries c"),
@NamedQuery(name = "Countries.findByCountryId", query = "SELECT c FROM Countries c WHERE c.countryId = :countryId"),
@NamedQuery(name = "Countries.findByCountryName", query = "SELECT c FROM Countries c WHERE c.countryName = :countryName")})
public class Countries implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 2)
@Column(name = "COUNTRY_ID")
private String countryId;
@Size(max = 40)
@Column(name = "COUNTRY_NAME")
private String countryName;
static SessionFactory sessionFactory ;
public Countries() {
}
public Countries(String countryId) {
this.countryId = countryId;
}
public String getCountryId() {
return countryId;
}
public void setCountryId(String countryId) {
this.countryId = countryId;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
@Override
public int hashCode() {
int hash = 0;
hash += (countryId != null ? countryId.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 Countries)) {
return false;
}
Countries other = (Countries) object;
if ((this.countryId == null && other.countryId != null) || (this.countryId != null && !this.countryId.equals(other.countryId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "entities.Countries[ countryId=" + countryId + " ]";
}
My Countries Facaderest包含创建函数:
@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create(Countries entity) {
super.create(entity);
}
客户端代码来自android应用程序:
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://192.168.0.104:8080/wsDatabase/webresources/entities.countries/create");
String text = null;
try {
HttpResponse response = httpClient.execute(httpPost, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
} catch (Exception e) {
return e.getLocalizedMessage();
}
return text;
}
顺便说一句:客户端代码正在其他web服务上运行和测试,但我需要让它在“创建web服务”上运行。尝试使用HttpPost更改您的HttpGet,因为您的rest服务回答了POST请求。我可以知道为什么我得到了“-1”吗?我想-1是因为这个问题归结为一个问题“请为我写一些代码”,一个更好的问题可能会被表述为“这是我尝试使用此代码中的RESTful函数。我原以为它能做X,但它却做了Y。请帮我理解为什么。“所以我们应该在写任何问题之前先上英语课。我们有很多非英语母语人士提出的问题;拙劣的语法很快就被清理干净了。不赞成的是要求其他用户编写代码;这不是StackOverflow的目的。一个好的问题试图解决问题,然后问一些具体的问题,关于在尝试中可能出现的错误。我是sry JFPicard我复制了这个与HttpGet一起工作的代码。。。我现在要解决我的问题。。。