• Post Reply Bookmark Topic Watch Topic
  • New Topic

Invoking Rest with Jersey Client

 
Nikhil Jain
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Experts,

I am trying to invoke Rest service through Jersey Client, that expect custom mime type or media type "application/vnd.com.oracle.oracloud.datagrid.Service+json". I get the below error


Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.sun.jersey.core.util.MultivaluedMapImpl, and MIME media type, application/vnd.com.oracle.oracloud.datagrid.Service+json, was not found
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155)
at com.sun.jersey.api.client.Client.handle(Client.java:652)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570)
at com.oracle.demo.OracleCloudControllerAPIs.OracleCloudAPIs.addCoherenceDataTierCluster(OracleCloudAPIs.java:112)
at com.oracle.demo.OracleCloudControllerAPIs.OracleCloudAPIs.main(OracleCloudAPIs.java:129)
Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class com.sun.jersey.core.util.MultivaluedMapImpl, and MIME media type, application/vnd.com.oracle.oracloud.datagrid.Service+json, was not found
at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:217)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153)
... 6 more



The code which I have is below. The below code works for the rest services which expects JSON or XML as mime type.


I am trying to programatically invoke this REST API

I have never come across custom mime type. The code works for all other mime types. Is there any other configuration which one has to make?
 
Simon Roberts
Author
Ranch Hand
Posts: 175
8
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really have to send that mime type? It seems like an excuse for "applicatin/json". Have you tried sending it as such?

If you cannot, then I think what you'll need to do is to create a class that implements MessageBodyReader and MessageBodyWriter, dive into the infrastructure and find the Provider class that's already installed that handles application/json, and proxy to that, converting the type blah-blah-oracle-blah/json into application/json and back accordingly, so that the "real" handler doesn't get it's knickers in a twist.

This:

https://jersey.java.net/documentation/latest/message-body-workers.html#d0e6814

gives an overview of the MessageBodyReader/Writer process; it's not hard really, thought.

HTH
Simon
 
Nikhil Jain
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried with application/json, but it replied with error "Not Supported Content Type", i am not even able to invoke this from other rest clients like Chrome rest client, I don't see any option of setthign this mime type.

Can someone explain me, why one would have used custom mime type?

Thanks
 
Simon Roberts
Author
Ranch Hand
Posts: 175
8
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there might be more to it that just JSON, but that would seem odd. Perhaps it's certain pre-agreed elements in the JSON. Does seem a little odd though.
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
application/vnd.com.oracle.oracloud.datagrid.Service+json specifies the json-formatted schema for the representations exchanged between the server and clients. In your code you are performing a GET, so the it should probably be specified in an Accept header, rather than a Content-Type header.

There are some curl examples here.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Posts: 2679
155
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code you are performing a GET, so the it should probably be specified in an Accept header, rather than a Content-Type header.

Agreed, you can check Jersey client examples here.
 
Nikhil Jain
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to perform post.



I still get "Content Not Supported Error". I am able to invoke this from Chrome Client (Attached Screen Shot)

I set the body as Raw & sent the json request.

 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My comment regarding Accept was for the code that you had originally posted, which was doing a GET operation. The example that you have now is doing a POST operation. For POST you do need Content-Type set to application/vnd.com.oracle.oracloud.datagrid.Service+json.

Are you trying to do a GET or POST?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil Jain, I have removed your screen shot from your post. Not only is it a poor way to show text information (please post text as text, and UseCodeTags to preserve formatting), but also it revealed your login credentials to anyone who could see the post. I'm sure that that was not your intention.
 
Nikhil Jain
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies for attaching screen shot. Just wanted to convey the configuration on my rest client. Thanks for removing it.

I am trying to do POST operation.
 
Tim Holloway
Bartender
Posts: 18412
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jersey works with plug-in converters that accept the request inputstream and convert it to javabean parameters. There are a number of options available. I've used MoXy, myself.

https://jersey.java.net/documentation/latest/media.html

application/json won't work (be accepted) without one of the JSON converters being wired in.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!