使用springfox 集成swagger 与spring mvc
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
<pluginRepositories> <pluginRepository> <id>jcenter-snapshots</id> <name>jcenter</name> <url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url> </pluginRepository> <pluginRepository> <snapshots> <enabled>false</enabled> </snapshots> <id>jcenter-releases</id> <name>jcenter</name> <url>http://jcenter.bintray.com</url> </pluginRepository> </pluginRepositories>
<repositories><repository><snapshots><enabled>false</enabled></snapshots><id>jcenter-releases</id><name>jcenter</name><url>http://jcenter.bintray.com</url></repository><repository><id>jcenter-snapshots</id><name>jcenter</name><url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url></repository> </repositories><dependencies> <!--<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework.version}</version> <scope>provided</scope> </dependency>--> <!--<dependency> <groupId>nl.jworks.markdown_to_asciidoc</groupId> <artifactId>markdown_to_asciidoc</artifactId> <version>1.0</version> </dependency>--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.5.0</version> </dependency>
<dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version></dependency><!--api--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.5.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.6.3</version></dependency><dependency><!--項目模塊--><groupId>com.inlycat</groupId><artifactId>yixun-core</artifactId><version>1.0</version></dependency><dependency><!--項目模塊--><groupId>com.inlycat</groupId><artifactId>yixun-app</artifactId><version>1.0</version><type>war</type></dependency><dependency><groupId>io.github.swagger2markup</groupId><artifactId>swagger2markup</artifactId><version>1.0.0</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-staticdocs</artifactId><version>2.4.0</version></dependency> </dependencies><build> <plugins> <!--可以將文檔導(dǎo)出md文件,不過目前導(dǎo)出時會報錯--> <plugin> <groupId>io.github.swagger2markup</groupId> <artifactId>swagger2markup-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <swaggerInput>http://localhost:8081/api/v2/api-docs?group=external</swaggerInput> <outputDir>${project.build.directory}/asciidoc</outputDir> <config> <swagger2markup.markupLanguage>MARKDOWN</swagger2markup.markupLanguage> </config> </configuration> </plugin> </plugins> <finalName>${project.name}</finalName> </build> ``` 2. 在需要引入swagger注解的模塊中引入相應(yīng)的依賴。 ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.6.3</version> </dependency> </dependencies> ``` 3. 在api模塊中添加初始化swagger的代碼。 ``` @EnableSwagger2 public class Swagger2Spring { @Bean public Docket petApi() { return new Docket(DocumentationType.SWAGGER_2); } ``` api模塊也是一個spring mvc 項目,在spring mvc 的配置文件中加入代碼。 ``` <mvc:annotation-driven/> <!--需要掃描的controller--> <context:component-scan base-package="cn.com.yixun.**.api" use-default-filters="false" > <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- Enables swgger ui--> <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/> <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
<!-- Include a swagger configuration--> <bean name="/applicationSwaggerConfig" class="cn.com.yixun.doc.swagger.Swagger2Spring"/> 啟動項目之后打開網(wǎng)址,{項目地址}/swagger-ui.html 就可以訪問了。后記:由于swagger對項目的侵入性比較強,可以考慮把注解寫在接口上。轉(zhuǎn)載于:https://my.oschina.net/qzzsunly/blog/719983
總結(jié)
以上是生活随笔為你收集整理的使用springfox 集成swagger 与spring mvc的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql出现连接错误不识别 utf8m
- 下一篇: C++ float的内存布局