我想使用RequestBuilder在客戶端(GWT)調用我的REST服務。我需要序列化一個複雜類型(Connexion),我選擇了Piriti。序列化似乎工作正常。 然後,我將我的複雜對象的字符串表示形式附加到body的請求併發送POST請求。POST請求(GWT:Requestbuilder)。錯誤的內容類型:application/x-www-form-urlencoded而不是application/xml
但我有以下錯誤:
org.jboss.resteasy.spi.BadRequestException:找不到類型郵件正文的讀者:內容類型的類com.ald.projet.property.Connexion:應用程序/ x-www-form-urlencoded
我在服務器端使用RESTeasy,它似乎沒有收到正確的內容類型。
我用螢火檢查,我的請求的內容類型是application/xml的...不是應用程序/ x-WWW窗體-urlencoded
Request headers Host: 127.0.0.1:8888 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0 Accept: application/xml Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3 Accept-Encoding: gzip, deflate Connection: keep-alive Referer: http://127.0.0.1:8888/Front_End.html?gwt.codesvr=127.0.0.1:9997 Content-Type: application/xml; charset=UTF-8 Content-Length: 109 Pragma: no-cache Cache-Control: no-cache Response headers Content-Type: text/html; charset=iso-8859-1 Server Jetty(6.1.x)
POST內容
<connexion>
<login>azerty</login>
<password>azerty</password>
</connexion>
客戶端
Connexion connexion = new Connexion("azerty", "azerty");
String url ="proxy.jsp?url=" + URL.encode("http://localhost:8080/rest/service/connexion");
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
builder.setHeader("Content-Type", "application/xml");
builder.setHeader("Accept", "application/xml");
//serialization with Piriti
String xml = Connexion.WRITER.toXml(connexion);
builder.setRequestData(xml);
builder.setCallback(new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
GWT.log(response.getText());
System.out.println(response.getText().trim());
}
@Override
public void onError(Request request, Throwable exception) {
}
});
try{
builder.send();
}catch(Exception e){
e.printStackTrace();
}
}
REST服務(服務器端)
@POST
@Path("/connexion")
//@Consumes("application/xml")
@Produces("application/xml")
public Response connexion(Connexion connexion){
String status = connexionDAO.isValidConnection(connexion);
return Response.ok(status).build();
}
Connexion.java客戶端
public class Connexion {
interface ConnexionReader extends XmlReader<Connexion> {}
public static final ConnexionReader XML = GWT.create(ConnexionReader.class);
public interface ConnexionWriter extends XmlWriter<Connexion> {}
public static final ConnexionWriter WRITER = GWT.create(ConnexionWriter.class);
private String login;
private String password;
public Connexion(){
}
public Connexion(String login, String password) {
super();
this.login = login;
this.password = password;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Connexion.java服務器側
@Embeddable
@XmlRootElement(name = "connexion")
public class Connexion {
private String login;
private String password;
public Connexion(){
}
public Connexion(String login, String password) {
super();
this.login = login;
this.password = password;
}
@XmlElement
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
@XmlElement
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
發生什麼事,我可以爲了使其工作做什麼?
預先THX
我還沒有答案,它讓我頭痛...... – isy