我使用"从数据库选项生成Web服务"创建了一个宁静的Web服务。在netbeans。 我已经在wildfly中部署了这个,并注意到当GET请求工作时,POST请求不会。 这是我接受POST连接的java代码的一部分:
@POST
@Override
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void create(XtgpsActivity entity) {
super.create(entity);
}
create方法采用XtgpsActivity类型的参数。这是XtgpsActivity类:
//packages and imports list removed
@Entity
@Table(name = "xtgps_Activity")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "XtgpsActivity.findAll", query = "SELECT x FROM XtgpsActivity x"),
@NamedQuery(name = "XtgpsActivity.findById", query = "SELECT x FROM XtgpsActivity x WHERE x.id = :id")})
public class XtgpsActivity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "Id")
private Integer id;
@JoinColumn(name = "BaseItemId", referencedColumnName = "Id")
@ManyToOne
private XtgpsBaseItem baseItemId;
@OneToMany(mappedBy = "activityId")
private Collection<XtgpsNearByActivity> xtgpsNearByActivityCollection;
public XtgpsActivity() {
}
public XtgpsActivity(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public XtgpsBaseItem getBaseItemId() {
return baseItemId;
}
public void setBaseItemId(XtgpsBaseItem baseItemId) {
this.baseItemId = baseItemId;
}
@XmlTransient
public Collection<XtgpsNearByActivity> getXtgpsNearByActivityCollection() {
return xtgpsNearByActivityCollection;
}
public void setXtgpsNearByActivityCollection(Collection<XtgpsNearByActivity> xtgpsNearByActivityCollection) {
this.xtgpsNearByActivityCollection = xtgpsNearByActivityCollection;
}
@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 XtgpsActivity)) {
return false;
}
XtgpsActivity other = (XtgpsActivity) 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 "au.com.xitech.XtgpsActivity[ id=" + id + " ]";
}
}
这是超类中的create方法:
public void create(T entity) {
getEntityManager().persist(entity);
}
由于相关表只有两列id和baseitemid,而id是主键,我发送的POST JSON数据如下:
{"baseitemid":"2"}
然而,当我以这种格式发布数据时,我收到一个错误:
http://localhost:8089/xxx/api/activity/ 400 (Bad Request)
我认为这是因为我发送的JSON数据存在问题。为post请求创建JSON数组的正确方法是什么?
PS :我还在下面附上了我的ajax电话。注意我在浏览器中启用CORS后执行了此操作。
$('#submit_it').click(function(e) {
//console.log("submit button has been clicked");
e.preventDefault(); //cancel form submit
var jsObj = $post_example.serializeObject()
, ajaxObj = {};
//console.log(jsObj);
alert(JSON.stringify(jsObj));
ajaxObj = {
type: "POST",
url: "http://localhost:8089/xxx/api/activity/",
data: JSON.stringify(jsObj),
contentType:"application/json",
success: function(data) {
//console.log(data);
if(data[0].HTTP_CODE == 200) {
$('#div_ajaxResponse').text( data[0].MSG );
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("Error " + jqXHR.getAllResponseHeaders() + " " + errorThrown);
},
complete: function(XMLHttpRequest) {
//console.log( XMLHttpRequest.getAllResponseHeaders() );
},
dataType: "json" //request JSON
};
$.ajax(ajaxObj);
});
答案 0 :(得分:0)
json中传递的值被解析为数值,因此发送为(没有值的引号)
{
"baseitemid":2
}
的工作。