Java 使用ajax将数据发布到Jax-RS
我是Jax-RS的新手,从ajax使用web服务时遇到了一些问题。我的get请求工作正常,现在我试图将一些数据发布到Oracle 11g数据库,我的ajax调用返回的只是错误,这使得调试变得困难 这是我的ajax调用Java 使用ajax将数据发布到Jax-RS,java,ajax,web-services,persistence,jax-rs,Java,Ajax,Web Services,Persistence,Jax Rs,我是Jax-RS的新手,从ajax使用web服务时遇到了一些问题。我的get请求工作正常,现在我试图将一些数据发布到Oracle 11g数据库,我的ajax调用返回的只是错误,这使得调试变得困难 这是我的ajax调用 function save(img) // base64 encoded string { $.ajax({ type: "POST", url: "http://192.168.42.179:8082/PotholeWebservice/webresou
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;
}
可能您还没有启动并运行web服务,请使用以下方法:
@GET
public String echo() {
return "My service is working! > " + new Date().toString();
}
使用web浏览器只是为了确保您可以使用Get请求访问它
如果您没有收到回音,那么服务很有可能尚未发布。也许您没有ApplicationRestConfig,让我给您举个例子:
/**
* Initialize the rest resources, here you should add ALL the REST classes.
*/
@ApplicationPath("/rest")
public class ApplicationRestConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
return new HashSet<>(Arrays.asList(MyServiceRest.class, OtherRestService.class));
}
}
/**
*初始化rest资源,在这里您应该添加所有rest类。
*/
@应用程序路径(“/rest”)
公共类ApplicationRestConfig扩展应用程序{
@凌驾
public Set您的方法参数paramImg
标记为要注入的@pathpram(“paramImg”)
是空字符串,因为您没有在@Path(“post”)
中定义相应的模板(即@Path(“post\{paramImg}”
),这可能是一个问题
如果希望在资源方法中注入请求实体,请省略注释,JAX-RS将确保注入实体
启用(查看如何查看此选项)要查看服务器端发生了什么。请发布日志。我的服务已启动,正在运行get请求。发布失败。我想这可能与我的类构造函数以及我使用的ID序列有关。你应该使用@Stateless(或@Stateful)创建实体,你有吗?…你的超类看起来怎么样?我昨晚确实解决了这个问题,现在我只需要检查我的构造函数是否正确,因为我使用的是生成的ID字段。