我是Jax-RS的新成員,並且在使用ajax的Web服務時遇到了一些問題。我的get請求正在運行,現在我試圖發佈一些數據以保存到Oracle 11g數據庫,我的ajax調用正在返回錯誤,這使得它很難調試。使用ajax向Jax-RS發佈數據
這裏是我的Ajax調用
function save(img) // base64 encoded string
{
$.ajax({
type: "POST",
url: "http://192.168.42.179:8082/PotholeWebservice/webresources/entities.pothole/post",
data: img,
dataType: "json",
success: function(data)
{
alert("saved to database");
},
error: function(textStatus, errorThrown)
{
alert("error in saving to database: " + textStatus + " " + errorThrown);
}
});
}
這裏是我的JAX-RS POST方法
@POST
@Path("post")
@Consumes({"application/xml", "application/json"})
public void create(@PathParam("paramImg") String paramImg) {
Date date = new Date();
BASE64Decoder decoder = new BASE64Decoder(); // decode base64 image to byte[]
byte[] decodedBytes = null;
try {
decodedBytes = decoder.decodeBuffer(paramImg);
} catch (IOException ex) {
Logger.getLogger(PotholeFacadeREST.class.getName()).log(Level.SEVERE, null, ex);
}
Pothole entity = new Pothole(decodedBytes, date);
super.create(entity);
}
超類
// super.create
public void create(T entity) {
getEntityManager().persist(entity);
}
我的課
public class Pothole implements Serializable {
private static final long serialVersionUID = 1L;
@Basic(optional = false)
@NotNull
@Lob
@Column(name = "IMAGE")
private byte[] image;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Id
@GeneratedValue(generator = "ID_Seq")
@SequenceGenerator(name="IDSeq",sequenceName="ID_SEQ", allocationSize=1)
@Basic(optional = false)
@NotNull
@Column(name = "ID")
private BigDecimal id;
@Basic(optional = false)
@NotNull
@Column(name = "PDATE")
@Temporal(TemporalType.TIMESTAMP)
private Date pdate;
public Pothole() {
}
public Pothole(BigDecimal id) {
this.id = id;
}
public Pothole(byte[] image, Date pdate) {
this.image = image;
this.pdate = pdate;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public Date getPdate() {
return pdate;
}
public void setPdate(Date pdate) {
this.pdate = pdate;
}
昨晚我做了這個工作,現在我只需要檢查我的構造函數是否正確,因爲我正在使用生成的ID字段。 –