2014-01-20 99 views
1

我是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; 
} 

回答

0

你的方法參數標註paramImg@PathParam("paramImg")是一個空字符串,因爲您沒有在@Path("post")(即@Path("post\{paramImg}")中定義相應的模板。這可能是一個問題。

如果你想要在你的資源方法中注入請求實體,省略註釋並且JAX-RS將確保實體被注入。

打開LoggingFilter(看看如何看看這個article)看看服務器端發生了什麼。請發佈日誌。

+0

昨晚我做了這個工作,現在我只需要檢查我的構造函數是否正確,因爲我正在使用生成的ID字段。 –

0

也許你仍然不必須​​的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)); 
    } 
} 

如果連所有這些檢查後仍不能工作,檢查本教程> http://coenraets.org/blog/2011/12/restful-services-with-jquery-and-java-using-jax-rs-and-jersey/

+0

我的服務已啓動並正在運行我的get請求正常工作。它的帖子失敗了。我認爲這可能是我的構造函數對我的類和序列我用的ID。 –

+0

你應該使用@Stateless(或@Stateful)創建實體,你有嗎? ...你的超類怎麼看? – Sergio