barbara
barbara

Reputation: 3201

Restlet: How can I retrieve DTO with setting custom MediaType?

How can I send GET request for entity with custom MediaType?

For example I want to retrieve MyUserDTO and set MediaType to application/user+yml.

For now I have two separated actions. I can retrieve entity:

resource.get(MyUserDTO.class);

and can retrieve string:

resource.get(new MediaType("application", "user+yml");

But how to combine them? Or maybe there is some trick to configure Restlet to teach him how to work with custom MediaTypes.

Upvotes: 2

Views: 161

Answers (1)

Thierry Templier
Thierry Templier

Reputation: 202346

In fact, you have the right approach but you don't use the right constructor of the class MediaType (new MediaType(name, description)).

To make your code work, you need to change it to this:

resource.get(new MediaType("application/user+yml"));

On the server side, you will get this:

@Get
public Representation getSomething() {
    System.out.println(">> media types = " +
    getRequest().getClientInfo().getAcceptedMediaTypes());
    // Display this: media types = [application/user+yml:1.0]
    (...)
}

You can leverage the extension support of Restlet by adding a value within the annotation Get. In your case, you need to add a custom extension as described below:

public class MyApplication extends Application {
    public MyApplication() {
        getMetadataService().addExtension(
            "myextension", new MediaType("application/user+yml"));
        (...)
    }

    @Override
    public Restlet createInboundRoot() {
        (...)
    }
}

Now you can use the extension within your server resource:

@Get("myextension")
public Representation getSomething() {
    (...)
}

This method will be used with the expected media type is application/user+yml.

Hope it helps you, Thierry

Upvotes: 1

Related Questions