spring mvc 整合swagger

1.首先須要導入jar包html

<!-- 生成swagger接口 -->     
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.5.0</version>
</dependency>
<!--web展現生成的接口 -->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>2.5.0</version>
</dependency>
<!-- 好像是生成接口中用到的序列化,maven依賴中已經存在,但不知道爲啥仍是須要這裏顯示導入 -->
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
	<version>2.8.2</version>
</dependency> 

2.新建配置文件   Swagger2Configurationjava

@EnableSwagger2
@Configuration
@EnableWebMvc
public class Swagger2Configuration {

	@Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())//api信息
                .select()
                .apis(RequestHandlerSelectors.any()) //api選擇
                .paths(PathSelectors.any())//指定api路徑
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(" 接口列表 v1.0.0")                
                .description("swagger_test")                
                .version("1.0.0")
                .build();
    }
}

3.修改XML配置文件web

<!-- 使靜態資源使用默認的Servlet 來處理,不聲明的話,可能訪問不到 swagger-ui.html-->
<mvc:default-servlet-handler />     
<!-- 這裏不顯示聲明bean,會出現頁面能訪問到接口不能展現的狀況 -->
<bean class="com.demo.swagger.config.Swagger2Configuration"/>

4.編寫swagger風格的接口spring

@Api(value="swaggertest")
@Controller
public class SwaggerTestAction {
	
	
	@ApiOperation("swagger_test")
	@ResponseBody
	@RequestMapping(value = "/test2/{id}",method=RequestMethod.POST,produces="application/json;charset=UTF-8")
	public String test (@ApiParam @PathVariable("id")String id){
		
		return id;

	}
}
相關文章
相關標籤/搜索