0
votes

I want to call my REST service on the client side (GWT) using RequestBuilder. I need to serialize a complex type (Connexion), i chose Piriti. The serialization seems to work fine. Then i attach the string representation of my complex object to the body's request and send the POST request.

But i have the following error :

org.jboss.resteasy.spi.BadRequestException: Could not find message body reader for type: class com.ald.projet.property.Connexion of content type: application/x-www-form-urlencoded

I use RESTeasy on the server-side, it seems that it doesn't receive the correct content type.

I checked with firebug, the content type of my request is application/xml ...not 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 content

<connexion>
  <login>azerty</login>
  <password>azerty</password>
</connexion>

Client-side

    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 Service (server side)

@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 client side

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 server side

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

}

what is happening and what can i do in order to make it work ?

Thx in advance

2
i still don't have the answer, it gives me headach ...isy

2 Answers

1
votes

In fact my proxy was modifying the content of my request.

I deployed my GWT application on Jetty (back-end) and that solved it, it lasts only one server so i don't have SOP issue. No need to use some proxy anymore, but every time i change something in the client side i have to re deploy it and it's a loss of time.

0
votes

Uncomment @Consumes("application/xml") to process as XML instead of default application/x-www-form-urlencoded