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

故事扮演 提交于 2019-12-11 03:16:52

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/26514414/restlet-how-can-i-retrieve-dto-with-setting-custom-mediatype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!