Giter Site home page Giter Site logo

I would like to get the relationships- authors-links-self, donno how to get it , Pls help me to get the self link about jsonapi-converter HOT 13 CLOSED

jasminb avatar jasminb commented on August 16, 2024
I would like to get the relationships- authors-links-self, donno how to get it , Pls help me to get the self link

from jsonapi-converter.

Comments (13)

jasminb avatar jasminb commented on August 16, 2024

Hello,

Can you please make sure that provided result is valid JSON.

from jsonapi-converter.

pradeepmallika avatar pradeepmallika commented on August 16, 2024

@jasminb I have taken from http://jsonapi.org/examples/#error-objects-source-usage

and done small modification in relationship part.

from jsonapi-converter.

jasminb avatar jasminb commented on August 16, 2024

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.

pradeepmallika avatar pradeepmallika commented on August 16, 2024

@jasminb Thanks for your detailed response ,I am using Retrofit , whether your solution can be used with Retrofit ?

from jsonapi-converter.

jasminb avatar jasminb commented on August 16, 2024

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.

pradeepmallika avatar pradeepmallika commented on August 16, 2024

@jasminb your email-id pls ?

from jsonapi-converter.

jasminb avatar jasminb commented on August 16, 2024

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.

pradeepmallika avatar pradeepmallika commented on August 16, 2024

@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.

jasminb avatar jasminb commented on August 16, 2024

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.

pradeepmallika avatar pradeepmallika commented on August 16, 2024

@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.

jasminb avatar jasminb commented on August 16, 2024

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.

jasminb avatar jasminb commented on August 16, 2024

Hey @pradeepmallika, do you still need this?

from jsonapi-converter.

pradeepmallika avatar pradeepmallika commented on August 16, 2024

@jasminb No ,thank you 👍

from jsonapi-converter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.