swagger-maven-plugin自动生成swagge.json(Jersey项目)

本文介绍了如何在Jersey项目中,通过swagger-maven-plugin插件来自动化生成swagger.json文件。首先,引入swagger-jersey2-jaxrs依赖,接着配置swagger-maven-plugin插件,包括指定API源、基本信息、输出目录等。然后,在控制器上添加必要的Swagger注解以定义API。最后,执行mvn compile生成swagger.json。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,加入依赖

<!-- Swagger -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-jersey2-jaxrs</artifactId>
            <version>1.5.0</version>
        </dependency>


2,加入插件

<!--增加swagger-plugin-->
            <plugin>
                <groupId>com.github.kongchen</groupId>
                <artifactId>swagger-maven-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <apiSources>
                        <apiSource>
                            <springmvc>false</springmvc>
                            <locations>com.gionee.geip.web</locations>
                            <schemes>http,https</schemes>
                            <host>xxxx</host>
                            <basePath>/api</basePath>
                            <info>
                                <title>Swagger Maven Plugin Sample</title>
                                <version>v1</version>
                                <description>
                                    This is a sample.
                                </description>
                                <termsOfService>
                                    http://www.github.com/kongchen/swagger-maven-plugin
                                </termsOfService>
                                <contact>
                                    <email>xxx</email>
                                    <name>xxx</name>
                                    <url>xxx</url>
                                </contact>
                                <license>
                                    <url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
                                    <name>Apache 2.0</name>
                                </license>
                            </info>
                            <securityDefinitions>
                            </securityDefinitions>
                            <swaggerDirectory>${basedir}/src/main/generated</swaggerDirectory>
                        </apiSource>
                    </apiSources>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>io.swagger</groupId>
                        <artifactId>swagger-hibernate-validations</artifactId>
                        <version>1.5.6</version>
                    </dependency>
                </dependencies>
            </plugin>


3,加入注释

@Controller
@Path("/geip")//必须
@Api(value = "/geip", description = "Operations about WebController")//必须
public class WebController {

    @Autowired
    private GeipManager geipManager;

    @GET
    @Path("/test")
    @ApiOperation(value = "test",
            notes = "test"
    )//必须
    public String test() {
        return "test";
    }

}


4,执行mvn compile

Swagger Maven Plugin是一个用于生成Swagger接口文档的Maven插件。它可以帮助开发人员在构建项目自动生成Swagger规范的JSON或YAML文件,以便于API文档的管理和使用。 使用Swagger Maven Plugin生成接口文档swagger.jsonswagger.yaml的步骤如下: 1.项目的pom.xml文件中添加Swagger Maven Plugin的依赖配置: ```xml <build> <plugins> <plugin> <groupId>com.github.kongchen</groupId> <artifactId>swagger-maven-plugin</artifactId> <version>3.1.8</version> <configuration> <!-- 配置Swagger文档的基本信息 --> <apiSources> <apiSource> <springmvc>true</springmvc> <locations>com.example.controller</locations> <basePath>/api</basePath> <info> <title>API文档</title> <version>1.0.0</version> <description>API接口文档</description> <termsOfServiceUrl>http://example.com/terms-of-service</termsOfServiceUrl> <contact> <email>contact@example.com</email> </contact> <license> <name>Apache 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.html</url> </license> </info> </apiSource> </apiSources> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 2.项目根目录下执行以下命令生成Swagger接口文档: ``` mvn compile swagger:generate ``` 3. 执行完上述命令后,Swagger Maven Plugin会根据配置的信息扫描项目中的接口,并生成Swagger规范的JSON或YAML文件。生成的文件默认保存在项目的target目录下的swagger目录中。 生成的Swagger接口文档可以通过访问http://localhost:8080/api/swagger-ui.html(假设项目部署在本地的8080端口)来查看和测试API接口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值