美文网首页
Springfox与SpringMVC集成笔记

Springfox与SpringMVC集成笔记

作者: 0xkevin | 来源:发表于2017-08-02 16:21 被阅读0次

1、pom.xml增加依赖

<!-- swagger-mvc -->
<dependency> 
<groupId>io.springfox</groupId>  
    <artifactId>springfox-swagger2</artifactId> 
    <version>2.4.0</version> 
</dependency> 
<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-annotations</artifactId> 
    <version>2.4.4</version> 
</dependency> 
<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-databind</artifactId> 
    <version>2.4.4</version> 
</dependency> 
<dependency> 
    <groupId>com.fasterxml.jackson.core</groupId> 
    <artifactId>jackson-core</artifactId> 
    <version>2.4.4</version> 
</dependency>
<dependency>
     <groupId>com.google.guava</groupId>  
     <artifactId>guava</artifactId>  
     <version>19.0</version>  
</dependency>
<!-- swagger-springmvc end -->

2、写配置类

@EnableSwagger2
public class SwaggerConfig {

@Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false);
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Api Documents")
                .contact(new Contact(“zhangfengcheng", "", “zhangfengcheng@navercorp.com"))
                .build();
    }
}

3、配置servlet-context.xml,加上上面配置类的bean和静态资源访问:

<mvc:annotation-driven />
<bean class="com.naver.dict.api.controller.config.SwaggerConfig"/>
<mvc:resources mapping="/apidoc/**" location="/apidoc/" />

4、配置web.xml,加上:

<servlet>
     <servlet-name>appServlet</servlet-name>  
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:servlet-context.xml</param-value>
     </init-param>
     <load-on-startup>1</load-on-startup>
</servlet>   
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
<url-pattern>/v2/api-docs</url-pattern>
</servlet-mapping>

5、下载swagger-ui,解压后把dist目录下的所有文件放到项目路径/src/webapp/apidoc目录下面

6、修改apidoc/index.html:

url = "/v2/api-docs";

7、ok,配置完成,输入http://localhost:8081/apidoc

相关文章

网友评论

      本文标题:Springfox与SpringMVC集成笔记

      本文链接:https://www.haomeiwen.com/subject/wthylxtx.html