Evg*_*aev 5 maven swagger micronaut
我想从Spring Boot 2切换到Micronaut框架。而且我在Swagger设置上苦苦挣扎。
在Spring Boot 2项目中,我具有以下依赖关系:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
和SwaggerConfig.class:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket swagger() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo getApiInfo() {
return new ApiInfo("test",
"",
"",
"",
new Contact("", "https://test.test", ""),
"",
"");
}
}
Run Code Online (Sandbox Code Playgroud)
它与Spring Boot 2应用程序一起完美启动swagger-ui。
我应该添加哪些依赖关系到Maven,以及应该创建哪些类才能为Micronaut项目获得相同的结果?
假设已经创建了应用程序,则将以下内容添加到您中:pom.xml
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger.version}</version>
<scope>compile</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
其中属性swagger.version设置为2.0.3
在maven-compiler-plugin中的注释处理器中添加以下内容
<path>
<groupId>io.micronaut.configuration</groupId>
<artifactId>micronaut-openapi</artifactId>
<version>${micronaut.version}</version>
</path>
Run Code Online (Sandbox Code Playgroud)
然后将以下内容添加到您的micronaut路由器部分。
micronaut:
router:
static-resources:
swagger:
paths: classpath:META-INF/swagger
mapping: ${application.api.swagger.path}/**
Run Code Online (Sandbox Code Playgroud)
如果使用下面的配置,这将显示在编译过程中生成的swagger / oas yml文件。当然,您可以根据自己的喜好${application.api.swagger.path}将其更改为“ be be” /api-docs/swagger或“东西”。
如文档中所述,--features=swagger-java在最初创建项目时,还可以执行以下操作以添加上述依赖关系。
如果要从应用程序本身呈现api规范,则需要添加更多代码。下面的示例可能比需要的要充实,但是出于我的目的,该应用程序充当了swagger / oas规范的中央渲染器。
首先添加一个针对您急需的控制器,然后使用对该控制器进行注释,@Hidden以确保注释处理器不会对其进行处理。
@Hidden
@Controller("/api")
public class SwaggerController {
@Inject
SwaggerConfig config;
@View("swagger/index")
@Get
public SwaggerConfig index() {
return config;
}
}
Run Code Online (Sandbox Code Playgroud)
然后添加以下配置类,该配置类从下面绑定属性
@ConfigurationProperties(SwaggerConfig.PREFIX)
public class SwaggerConfig {
public static final String PREFIX = "application.api.swagger";
private String version;
private String layout;
private boolean deepLinking;
private List<URIConfig> urls;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getLayout() {
return layout;
}
public void setLayout(String layout) {
this.layout = layout;
}
public boolean isDeepLinking() {
return deepLinking;
}
public void setDeepLinking(boolean deepLinking) {
this.deepLinking = deepLinking;
}
public List<URIConfig> getUrls() {
return urls;
}
public void setUrls(List<URIConfig> urls) {
this.urls = urls;
}
@ConfigurationProperties(URIConfig.PREFIX)
public static class URIConfig {
static final String PREFIX = "urls";
private String name;
private String url;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的config类绑定了application.yml中的以下配置,但是需要放在特定于micronaut的配置之前。
application:
api:
swagger:
path: /api-docs/swagger
version: 3.19.4
layout: StandaloneLayout
deepLinking: true
urls:
- name: ubw-rest
url: /api-docs/swagger/ubw-rest-0.1.yml
Run Code Online (Sandbox Code Playgroud)
完成后,将以下把手/胡子依赖项添加到pom中
<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
<version>4.1.0</version>
<scope>runtime</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在resources文件夹下,创建一个名为swagger的文件夹,然后创建一个包含以下内容的index.hbs文件。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger-ui</title>
<link rel="icon" type="image/png" href="https://unpkg.com/swagger-ui-dist@{{version}}/favicon-32x32.png">
<link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist@{{version}}/swagger-ui.css">
<script src="https://unpkg.com/swagger-ui-dist@{{version}}/swagger-ui-standalone-preset.js"></script>
<script src="https://unpkg.com/swagger-ui-dist@{{version}}/swagger-ui-bundle.js"></script>
</head>
<body>
<div id="swagger-ui"></div>
<script>
window.onload = function() {
var ui = SwaggerUIBundle({
urls: [{{#each urls}}
{
name: "{{name}}",
url: "{{url}}"
}{{#unless @last}},{{/unless}}{{/each}}
],
dom_id: '#swagger-ui',
deepLinking: {{deepLinking}},
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "{{layout}}"
});
window.ui = ui
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
最后,在应用程序主类中,添加@OpenApiDefinition注释,以使注释处理器能够扫描整个应用程序。
@OpenAPIDefinition(
info = @Info(
title = "swagger-server",
version = "0.1",
description = "My API",
license = @License(name = "Apache 2.0")
)
)
public class Application {
public static void main(String[] args) {
Micronaut.run(Application.class);
}
}
Run Code Online (Sandbox Code Playgroud)
关于micronaut 1.0.0中的注释处理器的一个建议是,不会公开对象上的公共字段,因此,如果要查看输入或返回值的架构,则需要使用getter / setter方法。
如果您想尝试上述示例的运行示例,我有一个位于此处https://github.com/frehov/micronaut-swagger-server的swagger服务器配置的存储库,其中包括使用以下功能发布帖子的功能Swagger呈现的url和名称对的列表。
| 归档时间: |
|
| 查看次数: |
2371 次 |
| 最近记录: |