Comments (13)
Hello,
Can you please make sure that provided result is valid JSON.
from jsonapi-converter.
@jasminb I have taken from http://jsonapi.org/examples/#error-objects-source-usage
and done small modification in relationship part.
from jsonapi-converter.
Hello @pradeepmallika,
Library does not allow direct access for relationship links in related objects, it allows for resolving relationships by implementing and registering relationship resolver.
This is the interface that needs to be implemented:
public interface RelationshipResolver {
byte [] resolve(String relationshipURL);
}
Purpose of the resolver is to execute fetch using the relationship URL and than deserialise the response (it works transparently).
Now if you don't want this to happen automatically but still need to know the relationship URL, you can do the following:
public class MyCustomRelationshipResolver implements RelationshipResolver {
private String relationshipURL;
public byte [] resolve(String relationshipURL) {
this.relationshipURL = relationshipURL;
return null;
}
public String getRelationshipURL() {
return relationshipURL;
}
}
Now, when you create your resource converter, do the following:
ResourceConverter converter = new ResourceConverter(...);
MyCustomRelationshipResolver customResolver = new MyCustomRelationshipResolver();
converter.setTypeResolver(customResolver, yourRelationshipTypeClass);
Now, after you execute a request, you will be able to get the relationship URL from the resolver by:
String url = customResolver.getRelationshipURL();
Please have in mind that this is a hack
solution, proper solution would be to implement actual relationship resolver and use it to dereference linked relationships.
There is another more elaborate option that will be more clean but is more complicated:
Lets imagine you have following relationship type:
@Type("relationship-type")
public class RelationshipType {
@Id
private String id;
@com.github.jasminb.jsonapi.annotations.Links
private Links links;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Links getLinks() {
return links;
}
public void setLinks(Links links) {
this.links = links;
}
}
You could write following MyCustomRelationshipResolver
:
public class MyCustomRelationshipResolver implements RelationshipResolver {
private ResourceConverter converter;
public MyCustomRelationshipResolver(ResourceConverter converter) {
this.converter = converter;
}
public byte [] resolve(String relationshipURL) {
RelationshipType type = new RelationshipType();
type.setId(UUID.randomUUID().toString());
Link self = new Link(relationshipURL);
Map<String, Link> linkMap = new HashMap<>();
linkMap.put("self", self)
type.setLinks(new Links(linkMap));
try {
return this.converter.writeDocument(new JSONAPIDocument<>(type));
} catch (DocumentSerializationException e) {
throw new RuntimeException(e);
}
}
}
If implemented like this, you will have your relationship object available with its Links
attribute populated in the root object.
from jsonapi-converter.
@jasminb Thanks for your detailed response ,I am using Retrofit , whether your solution can be used with Retrofit ?
from jsonapi-converter.
Sure it can, you need to create an instance of ResourceConverter
to use it with retrofit, when you create it, you need to register type resolver with it.
from jsonapi-converter.
@jasminb your email-id pls ?
from jsonapi-converter.
Hey @pradeepmallika, I would prefer to keep the discussion on this thread. If you need to share something you are not comfortable with sharing, you can change it in a way it does not expose any of your specifics and share it.
from jsonapi-converter.
@jasminb We are using JSONAPIConverterFactory as a Library(I am not using source code,I am using only jar), in JSONAPIConverterFactory where i can create instance of ResourceConverter ? an working example will be help full.
from jsonapi-converter.
Hey @pradeepmallika, if you check https://github.com/jasminb/jsonapi-converter/blob/develop/src/main/java/com/github/jasminb/jsonapi/retrofit/JSONAPIConverterFactory.java, you can see that there are two different constructors that take ResourceConverter
as arguments.
from jsonapi-converter.
@jasminb I tired many ways , it is not working,an simple example with below given json will be useful , thanks in advance.
{
"data": {
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!",
"body": "The shortest article. Ever.",
"created": "2015-05-22T14:56:29.000Z",
"updated": "2015-05-22T14:56:28.000Z"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
}
},
"included":
{
"type": "people",
"id": "42",
"attributes": {
"name": "John",
"age": 80,
"gender": "male"
}
}
}
from jsonapi-converter.
Hey @pradeepmallika, in this example the included
section is not relevant since there is no type/id information in author
relationship object. I can provide detailed example on how to get the relationship links if thats what you need.
from jsonapi-converter.
Hey @pradeepmallika, do you still need this?
from jsonapi-converter.
@jasminb No ,thank you 👍
from jsonapi-converter.
Related Issues (20)
- @Id obfuscated by proguard even after excluding the annotation HOT 2
- POST Example with RestTemplate HOT 2
- How to configure ObjectMapper? HOT 2
- @Type serialization HOT 2
- Document collection always throw error while using readCollections HOT 1
- Object with relations itself HOT 1
- @Relationship is not being deserialized HOT 2
- Creating a resource on the server - required id field HOT 2
- Does the library support Javas webclient and the reactor implementation? HOT 2
- "jsonapi": { "version": "1.0" } Parser HOT 2
- Get value above "attributes" level HOT 2
- Relationship attributes dont serialized HOT 3
- The library does not support JSON Specification "8.3 Inclusion of Related Resources" HOT 13
- Class doesn't have a Type annotation error, even though the class does have one HOT 3
- Proguard issues with jsonapi-converter when Android project to targets java 17 HOT 2
- InvalidJsonApiResourceException: Resource must contain at least one of 'data', 'error' or 'meta' nodes
- Handling local identifier ("lid") when creating resource HOT 4
- Backward compatibility HOT 2
- Pagination is not supported with decode fein in Java Spring boot HOT 2
- CVE-2023-0833 5.5 Generation of Error Message Containing Sensitive Information vulnerability with Medium severity found HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonapi-converter.