簡體   English   中英

使用自定義媒體類型的Restlet

[英]Restlet using custom media type

在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() vnd.myapp.v1 representV1() (JSON),如果他們將媒體類型指定為vnd.myapp.v2 ,則它們將得到vnd.myapp.v2 representV2() (JSON),並且(可選)他們不要求任何特定的東西,他們得到了representV2() 通過上面的設置,無論請求什么,我總是會返回representV2()

這是我測試時得到的:

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

似乎表達式json | vnd.myapp.v2 json | vnd.myapp.v2無法正常工作。 解決方法是使用jsonvnd.myapp.v2分為兩個方法。

當沒有指定accept頭時,Restlet似乎會調用第一個帶有注解Get it find的方法。

可以幫助您啟用跟蹤功能以查看不同方法的分數的方法是:

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

希望對您有幫助,蒂埃里

在注釋中刪除空格字符時,效果會更好:

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

我輸入了一個問題來解決此問題。 https://github.com/restlet/restlet-framework-java/issues/1099

此致Thierry Boileau

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM