2013-01-11 23 views
0

我想使用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

+0

我還沒有答案,它讓我頭痛...... – isy

回答

0

事實上我的代理被修改的內容我的請求。

我在Jetty(後端)上部署了我的GWT應用程序,解決了它,它只支持一臺服務器,所以我沒有SOP問題。 不再需要使用一些代理,但每次我在客戶端更改某些內容時,我必須重新部署它,這是時間的浪費。

0

取消註釋@Consumes(「應用程序/ XML」)來處理作爲XML而不是默認的應用程序/ X WWW的窗體-urlencoded

+0

當我取消@Consumes的評論時,它會告訴:「不能消費內容類型」。我認爲錯誤是由我的代理(用於避免同源策略)造成的。我如何避免沒有代理的SOP? – isy