本文介绍了有没有办法让Restlet Swagger扩展限制它显示的API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以让Restlet Swagger扩展限制它显示的API?



API中有一些不应该被分解的路径(只是还没有) ,确保至少只有特定的 org.restlet.routing.Router 会被暴露的策略是什么?因为我们在应用程序中有许多 Router s。

解决方案

开箱即可。要做到这一点的更简单的方法是国际海事组织为补丁扩展 org.restlet.extension.swagger




  • 更新类 SwaggerSpecificationRestlet 的方法 getSwagger 以允许更新内省定义过滤,如下所述:

      public代表性getSwagger(){
    定义definition = getDefinition();
    //用于过滤instropected结构的方法
    filterDefinition(definition);
    Swagger swagger = Swagger2Translator.getSwagger(definition);
    (...)
    }

    保护无效filterDefinition(定义定义){
    }
  • 在您的应用程序中,通过重写方法 getSwaggerSpecificationRestlet 来配置此类:

      public SwaggerSpecificationRestlet getSwaggerSpecificationRestlet(
    上下文上下文){
    SwaggerSpecificationRestlet结果
    = new SwaggerSpecificationRestlet(this){
    protected void filterDefinition(Definition definition){
    (...)
    }
    };
    result.setApiInboundRoot(this);
    返回结果;
    }




filterDefinition

  private定义filterDefinition(定义定义){ 
列表<资源> resourcesToRemove = new ArrayList<>();
Contract contract = definition.getContract();
for(Resource resource:contract.getResources()){
resourcesToRemove.add(resource);


(资源资源:resourcesToRemove){
contract.getResources()。remove(resource);
}

返回定义;
}

希望它能帮助您,
Thierry


Is there a way to make Restlet Swagger extension limit the API it shows?

There are paths in the API we have that should not be explosed (just yet), what is the strategy to make sure that at least only specific org.restlet.routing.Router will be exposed? As we have many Routers in the Application.

解决方案

This isn't possible out of the box. The easier way to do that is IMO to patch the extension org.restlet.extension.swagger:

  • Update the method getSwagger of the class SwaggerSpecificationRestlet to allow to update introspected definition filtering, as described below:

    public Representation getSwagger() {
        Definition definition = getDefinition();
        // The method to add for filtering the instropected structure
        filterDefinition(definition);
        Swagger swagger = Swagger2Translator.getSwagger(definition);
        (...)
    }
    
    protected void filterDefinition(Definition definition) {
    }
    

  • In your application, configure this class by overriding the method getSwaggerSpecificationRestlet:

    public SwaggerSpecificationRestlet getSwaggerSpecificationRestlet(
                     Context context) {
        SwaggerSpecificationRestlet result
                   = new SwaggerSpecificationRestlet(this) {
            protected void filterDefinition(Definition definition) {
                (...)
            }
        };
        result.setApiInboundRoot(this);
        return result;
    }
    

Here is a sample of the method filterDefinition:

private Definition filterDefinition(Definition definition) {
    List<Resource> resourcesToRemove = new ArrayList<>();
    Contract contract = definition.getContract();
    for (Resource resource : contract.getResources()) {
        resourcesToRemove.add(resource);
    }

    for (Resource resource : resourcesToRemove) {
        contract.getResources().remove(resource);
    }

    return definition;
}

Hope it helps you,Thierry

这篇关于有没有办法让Restlet Swagger扩展限制它显示的API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 03:31