POST请求(GWT:Requestbuilder)。错误的内容类型:application / x-www-form-urlencoded而不是application / xml

时间:2013-01-11 09:14:44

标签: xml rest gwt jax-rs content-type

我想使用RequestBuilder在客户端(GWT)调用我的REST服务。我需要序列化复杂类型(Connexion),我选择了Piriti。序列化似乎工作正常。 然后我将我的复杂对象的字符串表示附加到正文的请求并发送POST请求。

但我有以下错误:

  

org.jboss.resteasy.spi.BadRequestException:找不到类型的消息正文阅读器:class com.ald.projet.property.Connexion of content type:application / x-www-form-urlencoded

我在服务器端使用RESTeasy,似乎没有收到正确的内容类型。

我用firebug检查过,我的请求的内容类型是application / xml ...不是application / x-www-form-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();
    }
}

休息服务(服务器端)

@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;
}   

}

发生了什么以及我能做些什么才能使其发挥作用?

提前谢谢

2 个答案:

答案 0 :(得分:1)

实际上我的代理正在修改我的请求内容。

我在Jetty(后端)部署了我的GWT应用程序并解决了它,它只持续一台服务器,所以我没有SOP问题。 不再需要使用某些代理,但每次我在客户端更改某些内容时,我必须重新部署它,这是一个时间的浪费。

答案 1 :(得分:0)

取消注释@Consumes(“application / xml”)以XML格式处理,而不是默认的application / x-www-form-urlencoded