我想使用 RequestBuilder 在客户端 (GWT) 上调用我的 REST 服务。我需要序列化一个复杂类型(Connexion),我选择了 Piriti。序列化似乎工作正常。然后我将复杂对象的字符串表示附加到正文的请求并发送 POST 请求。
但我有以下错误:
org.jboss.resteasy.spi.BadRequestException:找不到类型的消息正文阅读器:com.ald.projet.property.Connexion 内容类型:application/x-www-form-urlencoded
我在服务器端使用 RESTeasy,它似乎没有收到正确的内容类型。
我检查了萤火虫,我的请求的内容类型是 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)
发布内容
<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;
}
}
发生了什么事,我该怎么做才能让它发挥作用?
提前谢谢