spring集成swagger2

1、POM.xml加入依赖包:(采用swagger2,)

             <!--swagger2相关-->
       <dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger2</artifactId>
		<version>2.7.0</version>
	</dependency>
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger-ui</artifactId>
		<version>2.7.0</version>
	</dependency>
             <!--json相关-->
        <dependency>
		  <groupId>com.fasterxml.jackson.core</groupId>
		  <artifactId>jackson-core</artifactId>
		  <version>2.8.1</version>
	  </dependency>
	  <dependency>
		  <groupId>com.fasterxml.jackson.core</groupId>
		  <artifactId>jackson-databind</artifactId>
		  <version>2.8.1</version>
	  </dependency>
	  <dependency>
		  <groupId>com.fasterxml.jackson.core</groupId>
		  <artifactId>jackson-annotations</artifactId>
		  <version>2.8.1</version>
	  </dependency>

2、编写swagger2的配置类

   @WebAppConfiguration
   @EnableSwagger2
   @EnableWebMvc
   @ComponentScan(basePackages = "controller")
   public class SwaggerConfig {
   	@Bean
   	pu  blic Docket api() {
   		return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
   				.paths(PathSelectors.any()).build().apiInfo(apiInfo());
  	}
   
   	private ApiInfo apiInfo() {
   		return new ApiInfoBuilder().title("项目接口文档").description("bingo").version("1.0.0").termsOfServiceUrl("")
   				.license("").licenseUrl("").build();
   
   	}
  }

3、配置springMvc相关文件, 在原有基础上加上如下代码

     <bean class="config.SwaggerConfig"/><!--刚才写的swagger配置类-->
     <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/><!--必须-->
     <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/><!--必须-->

4、编写controller测试:

@RestController
@RequestMapping(value = "/userInfo")
@Api(value = "用户信息")
public class UserInfoController {
private Logger logger = LoggerFactory.getLogger(getClass());
@ResponseBody
@RequestMapping(value = "/selectAllUsers", method = RequestMethod.GET)
@ApiOperation(value = "查询所有的人员信息并分页展示", notes = "查询所有的人员信息并分页展示")
@ApiImplicitParams({
        @ApiImplicitParam(name = "page",value = "跳转到的页数", required = true, paramType = "query"),
        @ApiImplicitParam(name = "size",value = "每页展示的记录数", required = true, paramType = "query")
})
public ServerResponse selectAllUsers(Integer page, Integer size) {

    return ServerResponse.createServerResponseBySuccess("sueess");
}

@ResponseBody
@RequestMapping(value = "/selectContacts", method = RequestMethod.GET)
@ApiOperation(value = "查询通讯录人员信息", notes = "查询通讯录人员信息")
public ServerResponse selectContacts() {
    return ServerResponse.createServerResponseBySuccess("sueess");
}

}
5、启功项目输入访问路径:http://localhost:8080/test/swagger-ui.html (test为项目名)



猜你喜欢

转载自blog.csdn.net/bingo_fang/article/details/79875880