如何解决使用JAX-RS和swagger-core的swagger-ui中的服务URL错误
我需要使用swagger-core v3,使用openapi 3 wit swagger ui来记录jaxrs jersey服务。 我更喜欢使用完整的代码方法而不使用配置文件。我将使用Wildfly部署服务。
这里的代码在以下地址生成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);
}
}
}
{
"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"
}
}
}
}
}
}
如您所见,在该文件中没有对suma-auth-ms的引用,服务响应正确位于http:// localhost:12079 / suma-auth-ms / rest / authentication / authenticateUser
当我看一下swagger ui并尝试执行一些测试时,我得到了以下结果:
显示的错误是由于curl用来调用服务的地址中缺少suma-auth-ms。地址应为http:// localhost:12079 / suma-auth-ms / rest / authentication / authenticateUser,而不是http:// localhost:12079 / rest / authentication / authenticateUser
有没有办法解决?
解决方法
您需要使用 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)
进行此更改后,您应该会在openapi.json中看到服务器,您将可以通过添加 suma-auth-ms 来命中请求
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。