Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Lyo » How to post JSON with the Lyo RestClient?(Failing to send JSON instead of RDF over the Lyo client.)
How to post JSON with the Lyo RestClient? [message #1798637] Wed, 21 November 2018 13:41 Go to next message
Ralph Schoon is currently offline Ralph SchoonFriend
Messages: 9
Registered: November 2018
Junior Member
Hi, I am trying to send JSON instead of RDF/XML.


JsonObject changeSet = Json.createObjectBuilder().add("name", "Import-Type-System-CS-" + TimeStampUtil.getTimestamp()).add("configurationId", target.getAbout().toString()).build();

ClientResponse response = client.createResource(changesetFactotryURL, changeSet, "application/json");


Unfortunately I get the following error:

A javax.ws.rs.ext.MessageBodyWriter implementation was not found for the class org.glassfish.json.JsonObjectBuilderImpl$JsonObjectImpl type and application/json; charset=UTF-8 media type. Verify that all entity providers are correctly registered. Add a custom javax.ws.rs.ext.MessageBodyWriter provider to handle the type and media type if a JAX-RS entity provider does not currently exist.


I am trying to figure out how to solve the issue. I recognize I have

<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.2</version>
</dependency>

in my pom and I am using the libraries to create the JSON data.
I added a different JSON library and the error message change to that.

I have found several hints in the internet, that I was unfortunately not really able to understand.

Are there any examples around sending JSON using the OSLC Client?
Any idea what I could do?
Re: How to post JSON with the Lyo RestClient? [message #1798708 is a reply to message #1798637] Fri, 23 November 2018 05:35 Go to previous messageGo to next message
Jad El-khoury is currently offline Jad El-khouryFriend
Messages: 61
Registered: August 2015
Member
Hi
Have a look at the sample code https://github.com/eclipse/lyo.client/blob/master/oslc-java-samples/src/main/java/org/eclipse/lyo/client/oslc/samples/GenericCMSample.java
In particular, "SCENARIO C: ChangeRequest creation and update" around line 120 seems to be what you are trying to do.
You notice here, that createResource is taking an instance of a ChangeRequest. ChangeRequest is a Java class that is a specialisation of AbstractResource. ChangeRequest has also annotations on its getter methods that tell the system how it is meant to marhsal the object into an rdf-representation (rdf+xml, json, turtle).
The Client class is meant to take in an instance of a class that specialises the abstract class AbstractResource. (Don't ask me why it accepts Object, and not AbstractResource though).

So, instead of passing it json, you should create such instances, and leave the json creation to the client.
Re: How to post JSON with the Lyo RestClient? [message #1798729 is a reply to message #1798637] Fri, 23 November 2018 10:43 Go to previous messageGo to next message
Andrii BerezovskyiFriend
Messages: 24
Registered: May 2016
Junior Member
Hello Ralph,

May I ask what is your case for using JSON? To follow-up on that, do you know that OSLC v2 servers only support a quite specific kind of JSON and only v3 servers MAY support JSON-LD?

Now onto your next question how to use Lyo Client to send JSON. In any case, to generate conformant data we use special helpers. This means you cannot use a JsonObjectBuilder from Java EE but instead have to follow Jad's example and pass CT_JSON instead of CT_RDF.

Offtopic: it escapes me why do we have two OSLCConstants classes with identical names both in core and client libs.

Does this help you with your question, Ralph?

Finally, we are trying to consolidate all OSLC-related discussion on https://forum.open-services.net/ and you are welcome to post new questions there.


Best regards,
Andrew
Eclipse Lyo project lead & OSLC Core TC voting member
Re: How to post JSON with the Lyo RestClient? [message #1798741 is a reply to message #1798708] Fri, 23 November 2018 12:56 Go to previous messageGo to next message
Ralph Schoon is currently offline Ralph SchoonFriend
Messages: 9
Registered: November 2018
Junior Member
Hi Jad,

I have used the annotations for RDF and understand them now. I was not sure how to translate that to JSON. Maybe just the naive way and enter the data in a similar way. I will try that, thanks!

I have created the JSON object and I am passing it as string at the moment. That definitely also works.

Thanks!

Ralph

[Updated on: Fri, 23 November 2018 12:57]

Report message to a moderator

Re: How to post JSON with the Lyo RestClient? [message #1798746 is a reply to message #1798741] Fri, 23 November 2018 13:21 Go to previous message
Ralph Schoon is currently offline Ralph SchoonFriend
Messages: 9
Registered: November 2018
Junior Member
Hi Andrii,

the simple truth is, that this is an internal API I was able to find looking at the web browser messages. I have captured the format it uses. I have confirmed with development that there is no OSLC compatible API for this activity.
Since I am using the OSLC4J client, I want to use that for this API as well, if possible. I have found a way and will try to look into a more compatible way with using annotations maybe. At the moment I am passing the JSON as string and read the JSON as inputfile back from the response.
I think, and I think this is going to be common, I am 'abusing' OSLC4J for all my REST calls and not only OSLC. As long as there are plenty of APIS that are not OSLC compliant I have to use, it appears to be a valid approach, if I can make it work.

This mode of working also requires sometimes to sneak in custom headers. I am using an HttpRequestInterceptor to 'massage' the headers and to add custom headers where I can not pass the header directly e.g. in create calls.
Even for some of the RDF data that comes back the resolution does not work (missing type) and I have to parse the RDF model.

I am not complaining, I am glad it works for me so far. I am just learning. I will try to publish what I learn in my blog https://rsjazz.wordpress.com/ or in our articles on Jazz.net. Maybe that helps people to get started.

The answers are definitely helping. The constants are a bit confusing, because there are so many duplicates. 8)

Thanks again!

Ralph

[Updated on: Fri, 23 November 2018 13:22]

Report message to a moderator

Previous Topic:RQM Categories
Next Topic:Lyo 2.4.0 release
Goto Forum:
  


Current Time: Fri Apr 26 17:58:55 GMT 2024

Powered by FUDForum. Page generated in 0.04018 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top