0

I created a Jersey client program to consume a REST web service that return XML.

Client client = Client.create();
WebResource webResource = client.resource("http://xyz:abc/myRestservice/getval");

I use the webResource get method to store the return value in a string variable:

String s = webResource.get(String.class);

I get no errors. But variable "s" shows null as output:

System.out.println("- "+s);

Output:

- 
Process exited with exit code 0.

When I test the same web service locally (using the JDeveloper IDE without a client program), it returns value.


update:

I found that the variable "s" was showing null because of an exception (explained below) in the web service program.

The web service program uses an opaque (OracleTypes.OPAQUE) variable to store the XMLTYPE value retrieved from a stored function in ORACLE database. The opaque variable is then assigned to a new XMLType using a cast. This somehow works while testing in JDeveloper IDE internal weblogic server. But it doesn't work when I deploy this web service in a remote weblogic server and try to consume using a client program. I get an exception - "oracle.sql.OPAQUE cannot be cast to oracle.xdb.XMLType".

Most probably due to some missing Jar in the remote weblogic server I guess, but not sure which jar.

Karut
  • 1
  • 1
  • 2

1 Answers1

1

You are almost there. What you should really be doing is get a ClientResponse.class and not String.class.

Here's a sample:

ClientResponse response = webResource.get(ClientResponse.class);
String responseString = response.getEntity(String.class);

Once you start using this jersey rest client you will know a bit more that per REST api contract, you will also have to set the "requestType" and the "responseAccept" as a part of the webResource. Without that, you might end up having some other weird errors as well.

Here's another sample:

ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_XML).post(ClientResponse.class);
String responseStringXML = response.getEntity(String.class);

where:

the request type (data format that the REST API expects in the body) is: "application/json" and the response type (data format that the REST API returns) is: "application/xml"

Note that I used POST(.post method) instead of get because in normal GET request, a body is not expected (and thus no request Type is needed). The POST example was just for reference. For GET, You will still need a response type though, something along the lines of:

ClientResponse response = webResource.accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
String responseStringXML = response.getEntity(String.class);

I hope that helps !

shahshi15
  • 2,772
  • 2
  • 20
  • 24