简体   繁体   English

使用 JAX-RS 和 swagger-core 的 swagger-ui 中的服务 URL 错误

[英]Wrong service URL in swagger-ui using JAX-RS and swagger-core

I need to use openapi 3 wit swagger ui to document a jaxrs jersey service, using swagger-core v3.我需要使用 openapi 3 wit swagger ui 来记录 jaxrs jersey 服务,使用 swagger-core v3。 I prefer use the full code approach without use config files.我更喜欢使用完整的代码方法而不使用配置文件。 I'm going to use Wildfly to deploy service.我将使用 Wildfly 来部署服务。

Here the code to generate openapi.json at following address: http://localhost:12079/suma-auth-ms/rest/openapi.json这里是在以下地址生成 openapi.json 的代码:http://localhost:12079/suma-auth-ms/rest/openapi.json

@ApplicationPath("/rest")
public class AuthRestConfig extends Application {

  public AuthRestConfig(@Context ServletConfig servletConfig) {
    super();

    System.out.println();
    OpenAPI oas = new OpenAPI();
    Info info = new Info()
        .title("Suma Automaton OpenAPI")
        .description("This is a sample server Petstore server.  You can find out more about Swagger "
            + "at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).  For this sample, "
            + "you can use the api key `special-key` to test the authorization filters.")
        .termsOfService("http://swagger.io/terms/")
        .contact(new Contact()
            .email("emasil@email.com"));

    oas.info(info);
    SwaggerConfiguration oasConfig = new SwaggerConfiguration()
        .openAPI(oas)
        .prettyPrint(true)
        .resourcePackages(Stream.of("io.swagger.sample.resource").collect(Collectors.toSet()));

    try {
      new JaxrsOpenApiContextBuilder<>()
          .servletConfig(servletConfig)
          .application(this)
          .openApiConfiguration(oasConfig)
          .buildContext(true);
    } catch (OpenApiConfigurationException e) {
      throw new RuntimeException(e.getMessage(), e);
    }

  }
}

and here you can see openapi.json file at mentioned address:在这里你可以在提到的地址看到 openapi.json 文件:

{
    "openapi": "3.0.1",
    "paths": {
        "/rest/authentication/authenticateUser": {
            "post": {
                "operationId": "authenticateUser",
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/Credentials"
                            }
                        }
                    }
                },
                "responses": {
                    "default": {
                        "description": "default response",
                        "content": {
                            "application/json": {}
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "Credentials": {
                "type": "object",
                "properties": {
                    "username": {
                        "type": "string"
                    },
                    "password": {
                        "type": "string"
                    }
                }
            }
        }
    }
}

As you can see in this file there's no references to suma-auth-ms, service response correctly at http://localhost:12079/suma-auth-ms/rest/authentication/authenticateUser正如你在这个文件中看到的,没有对 suma-auth-ms 的引用,服务响应在 http://localhost:12079/suma-auth-ms/rest/authentication/authenticateUser

When I'm going to take a look to the swagger ui, andr try to execute some tests, I've this result:当我要查看 swagger ui 并尝试执行一些测试时,我得到了以下结果: 在此处输入图片说明

Error shown is due to lack of suma-auth-ms in the address used by curl to call the service.显示的错误是由于 curl 用于调用服务的地址中缺少 suma-auth-ms。 Address should be http://localhost:12079/suma-auth-ms/rest/authentication/authenticateUser and not http://localhost:12079/rest/authentication/authenticateUser地址应该是 http://localhost:12079/suma-auth-ms/rest/authentication/authenticateUser 而不是 http://localhost:12079/rest/authentication/authenticateUser

Is there a way to fix?有办法修复吗?

You need to use io.swagger.v3.oas.models.servers.Server and add it into your oas object.您需要使用io.swagger.v3.oas.models.servers.Server并将其添加到您的 oas 对象中。

oas.info(info);
String url = "/suma-auth-ms/"
List<Server> servers = new ArrayList<Server>();
Server server = new Server();
server.setUrl(url);
servers.add(server);
oas.setServers(servers)

After making this change you should see servers listed in your openapi.json and you will be able to hit request with suma-auth-ms added进行此更改后,您应该会看到 openapi.json 中列出的服务器,并且您将能够通过添加suma-auth-ms来点击请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM