我是新泽西州的Web服务的新成员。我创建了一个Web服务代码如下将XML字符串发送到Jersery中的Web服务
@Path("/remotedb")
public class RemoteDb {
@GET
@Path("/save/{xmlData}")
@Produces(MediaType.TEXT_XML)
public String saveData(@PathParam("xmlData") String xml) {
return xml;
}
}
我在客户端的代码现在
public class WebServiceClient {
public static void callWebService() {
String xml = "<data>" +
"<table><test_id>t4</test_id><dateprix>2013-06-06 22:50:40.252</dateprix><nomtest>NOMTEST</nomtest><prixtest>12.70</prixtest><webposted>N</webposted><posteddate>2013-06-06 21:51:42.252</posteddate></table>" +
"</data>";
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
System.out.println(service.path("restful").path("remotedb").path("save").path(xml).accept(MediaType.TEXT_XML).get(String.class));
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8080/WebServiceModule").build();
}
}
当我打电话的web服务我有以下异常
Exception in thread "main" com.sun.jersey.api.client.UniformInterfaceException: GET http://localhost:8080/WebServiceModule/restful/remotedb/save/%3Cdata%3E%3Ctable%3E%3Ctest_id%3Et4%3C/test_id%3E%3Cdateprix%3E2013-06-06%2022:50:40.252%3C/dateprix%3E%3Cnomtest%3ENOMTEST%3C/nomtest%3E%3Cprixtest%3E12.70%3C/prixtest%3E%3Cwebposted%3EN%3C/webposted%3E%3Cposteddate%3E2013-06-06%2021:51:42.252%3C/posteddate%3E%3C/table%3E%3C/data%3E returned a response status of 404 Not Found
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:507)
at com.main.WebServiceClient.callWebService(WebServiceClient.java:25)
at com.main.Test.main(Test.java:7)
我怀疑新泽西州可能会越来越被XML数据中的所有/所困惑。它将URLEncode特殊字符(例如< and >),但它不知道/是XML的一部分,而不是URL的一部分。你可以使用URLEncoder并自己编码XML字符串吗?另外,您确定要将该数据发送到URL而不是实体主体中吗?这看起来更像是POST数据而不是url路径。 – Pace
如何使用URLEncoder? –
http://stackoverflow.com/questions/10786042/java-url-encoding – Pace