0

I am trying to convert the following jersey code to restlet.

WebResource webResource = client.resource("/getlistofobjects");
List<MyObject> thisMemberObjects = webResource
    .accept("application/json")
    .get(new GenericType<List<MyObject>>(){});

thisListOfObjects.addAll((List<MyObject>)thisMemberObjects);
rtruszk
  • 3,902
  • 13
  • 36
  • 53
rzeldes
  • 13
  • 2

1 Answers1

0

First you need to create an annotated interface, as described below:

public interface MyService {
    @Get
    List<MyObject> getObjs();
}

Then you can leverage Restlet converter from class ClientResource based on this interface with code like this:

ClientResource cr = new ClientResource("http://.../getlistofobjects");
cr.accept(MediaType.APPLICATION_JSON);
MyService myService = cr.wrap(MyService.class);
List<MyObject> objs = myService.getObjs();

Don't forget to add in your classpath the extension org.restlet.ext.jackson that will automatically convert the JSON content to Java objects.

Here a sample pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                    http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.restlet</groupId>
    <artifactId>test.restlet.client</artifactId>
    <name>${project.artifactId}</name>
    <packaging>jar</packaging>
    <version>1.0.0-snapshot</version>

    <properties>
        <java-version>1.7</java-version>
        <restlet-version>2.3.1</restlet-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.restlet.jse</groupId>
            <artifactId>org.restlet</artifactId>
            <version>${restlet-version}</version>
        </dependency>

        <dependency>
            <groupId>org.restlet.jse</groupId>
            <artifactId>org.restlet.ext.jackson</artifactId>
            <version>${restlet-version}</version>
        </dependency>

        <dependency>
            <groupId>org.restlet.jse</groupId>
            <artifactId>org.restlet.ext.httpclient</artifactId>
            <version>${restlet-version}</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>maven-restlet</id>
            <name>Public online Restlet repository</name>
            <url>http://maven.restlet.com</url>
        </repository>
    </repositories>
</project>

Edited

For the following response payload:

{
    "elitFileBasic": [
        {
            "application":"$TRACKING",
            "fileName":"FILE.TRACKING.DATA"
        },
        {
            "application":"$TRACKING$",
            "fileName":"TRACKING.EVENT.5"
        }
    ]
}

You need to have the following bean:

public class ResponseBean {
    private List<ResponseElementBean> elitFileBasic;

    public List<ResponseElementBean> getElitFileBasic() {
        return this.elitFileBasic;
    }

    public void setElitFileBasic(List<ResponseElementBean> elitFileBasic) {
        this.elitFileBasic = elitFileBasic;
    }
}

public class ResponseElementBean {
    private String application;
    private fileName;

    // Getters and setters
    (...)
}

In this case, the annotated interface would be:

public interface MyService {
    @Get
    ResponseBean getObjs();
}

You can notice that you can update response deserialization with Jackson to support a list of objects as return of the method getObjs. For such use case, you need to register a custom deserializer. This answer can give you some interesting hints: Restlet Complex Object to XML serializaton.

Hope it helps you, Thierry

Community
  • 1
  • 1
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • now getting an exception - com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1a1d6a08; line: 1, column: 1] @thierry templier – rzeldes May 06 '15 at 14:45
  • Could you provide a sample of data structure that is received? Thanks! This would help me to understand the exception... – Thierry Templier May 06 '15 at 14:53
  • {"elitFileBasic":[{"application":"$TRACKING","fileName":"FILE.TRACKING.DATA"}, {"application":"$TRACKING$","fileName":"TRACKING.EVENT.5"} ]} – rzeldes May 06 '15 at 15:33
  • prior comment is the json structure received by a restlet client from a Jersey server @thierry templier – rzeldes May 06 '15 at 16:02
  • I updated my answer with the response content you gave me in your comment... Hope it will help you – Thierry Templier May 07 '15 at 07:57
  • Thanks - the updated content solved my issue. I did have to make sure the ResponseBean and ResponseElementBean were defined "static" (found this in other overflow entries). I do have another question. Why do I see ---- text text/csv csv vs application application/json json application application/json json vs text text/csv csv isCompatible false isCompatible true ---- messages in my log? @thierry – rzeldes May 07 '15 at 14:45
  • Yes, in fact, Restlet let you the choice to work either on raw data or beans. Where do you see these hints displayed? It seems to be media types in fact. Otherwise feel free to upvote my answer or even accept it if it suits you ;-) Thanks! – Thierry Templier May 07 '15 at 14:50