【问题标题】:Restlet using custom media type使用自定义媒体类型的 Restlet
【发布时间】:2015-08-22 16:11:38
【问题描述】:

在 Restlet 2.3 (SE) 中,我尝试使用媒体类型来控制版本。我目前的尝试涉及在我的入站路由中注册新的扩展:

@Override
public Restlet createInboundRoot() {

        ...
        getTunnelService().setExtensionsTunnel(true);

        getMetadataService().addExtension("vnd.myapp.v1", MediaType.valueOf("application/vnd.myapp.v1+json"));
        getMetadataService().addExtension("vnd.myapp.v2", MediaType.valueOf("application/vnd.myapp.v2+json"));

        ...
}

然后我的资源接口设置如下:

public interface UsersResource {

    @Options
    void getCorsSupport();

    @Get("vnd.myapp.v1")
    Collection<User> representV1() throws Exception;

    // Should be the default if */* is specified
    @Get("json | vnd.myapp.v2")
    Collection<User> representV2() throws Exception;

}

然后我尝试按如下方式指定媒体类型:

http://localhost:8080/api/users?media=vnd.myapp.v1

这个想法是,如果有人将媒体类型指定为vnd.myapp.v1,他们将得到representV1() (JSON),如果他们将媒体类型指定为vnd.myapp.v2,他们将得到representV2() (JSON),并且(可选)如果他们什么都不问他们得到representV2()。通过上述设置,无论有什么要求,我总是会回复representV2()

【问题讨论】:

    标签: java json restlet restlet-2.0 media-type


    【解决方案1】:

    删除注释中的空格字符会更好,例如:

    @Get("json|vnd.myapp.v2")
    

    我输入了一个问题来解决这个问题。 https://github.com/restlet/restlet-framework-java/issues/1099

    最好的问候,蒂埃里·布瓦洛

    【讨论】:

    • 问题已修复。该修复程序将在 2.3.3 中提供。
    【解决方案2】:

    这是我在测试时得到的:

    • Accept: application/vnd.myapp.v1+json -> representV1 被调用
    • Accept: application/vnd.myapp.v2+json -> representV2 被调用
    • Accept: application/application/json -> representV1 被调用
    • Accept: */* -> representV1 被调用

    似乎json | vnd.myapp.v2 表达式无法正常工作。解决方法是将representV2拆分为jsonvnd.myapp.v2两种方法。

    当没有指定接受头时,似乎Restlet调用了它找到的第一个带有注释Get的方法。

    可以帮助您的是启用跟踪以查看不同方法的分数:

    public class RestletLauncher {
        public static void main(String[] args) {
            Engine.getInstance().setLogLevel(Level.FINEST);
            launchApplication();
        }
    }
    

    你会看到这样的痕迹:

    Score of annotation "MethodAnnotationInfo [javaMethod: public org.restlet.representation.Representation test.MyServerResource1.testGetVnd1(), javaClass: class test.MyServerResource1, restletMethod: GET, input: vnd.myapp.v1, value: vnd.myapp.v1, output: vnd.myapp.v1, query: null]"= 0.5
    Total score of variant "[application/vnd.myapp.v1+json]"= 0.04191667
    Score of annotation "MethodAnnotationInfo [javaMethod: public org.restlet.representation.Representation test.MyServerResource1.testGetJson(), javaClass: class test.MyServerResource1, restletMethod: GET, input: json, value: json, output: json, query: null]"= 0.5
    Total score of variant "[application/json]"= 0.04191667
    Score of annotation "MethodAnnotationInfo [javaMethod: public org.restlet.representation.Representation test.MyServerResource1.testGetVnd2(), javaClass: class test.MyServerResource1, restletMethod: GET, input: vnd.myapp.v2, value: vnd.myapp.v2, output: vnd.myapp.v2, query: null]"= 0.5
    Total score of variant "[application/vnd.myapp.v2+json]"= 0.04191667
    

    希望对你有帮助, 蒂埃里

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 2013-02-01
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 2014-07-01
      相关资源
      最近更新 更多