0

I am new to web service in jersey. I have created a web service the code is below

@Path("/remotedb")
public class RemoteDb {

@GET
@Path("/save/{xmlData}")
@Produces(MediaType.TEXT_XML)
public String saveData(@PathParam("xmlData") String xml) {
    return xml;
    }
}

I have this code at client side

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();
    }
} 

Now when i called the web service i got the following exception

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)
Waqas Ali
  • 1,642
  • 4
  • 32
  • 55
  • I suspect Jersey may be getting confused by all the /'s in the XML data. It will URLEncode special characters (e.g. < and >) but it doens't know that the / is part of the XML and not part of the URL. Can you use URLEncoder and encode the XML string yourself? Also, are you sure you want to send that data in the URL and not in the entity body? That looks more like POST data than a url path. – Pace Jun 12 '13 at 16:50
  • how to use URLEncoder?? – Waqas Ali Jun 12 '13 at 16:53
  • http://stackoverflow.com/questions/10786042/java-url-encoding – Pace Jun 12 '13 at 16:56

1 Answers1

2

Passing XML data in a path segment is very unorthodox and likely to raise all kind of issues. You should pass it as a query parameter, e.g. /WebServiceModule/restful/remotedb/save?xmlData= %3Cdata...

@GET
@Path("/save")
@Produces(MediaType.TEXT_XML)
public String saveData(@QueryParam("xmlData") String xml) {
    return xml;
    }
}

or even better if it is a write operation as the name suggests then it should be a POST /WebServiceModule/restful/remotedb/save with the xmlData passed in the request body.

@POST
@Path("/save")
@Produces(MediaType.TEXT_XML)
public String saveData(String xml) {
    return xml;
    }
}

or even better yet, if you can map your xmlData to a POJO with JAXB's @XmlRootElement annotation, then you can get jersey to parse it for you:

@POST
@Path("/save")
@Consumes(MediaType.APPLICATION_XML)
public String saveData(YourXmlDataObject obj) {
    return obj.getField();
    }
}
TheArchitect
  • 2,161
  • 1
  • 12
  • 16
  • I am calling a POST method in which one of my parameter has encoded xml in it. I am accessing it using @FormParam("formula"). But, it's value is blank. I am passing encoded xml during call. I don't know why it's coming blank in Java call. Also I used "@Consumes(MediaType.APPLICATION_FORM_URLENCODED)" annotation with my method. Any advice? – Madhusudan Dec 11 '17 at 11:40