Search code examples
javajerseyrestlet

restlet syntax for jersey version 1 request list of objects


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);

Solution

  • 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