spring集成swagger2

本文介绍如何使用Swagger2快速搭建RESTful API文档,包括添加依赖、配置类编写、Spring MVC配置及控制器测试。

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

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为项目名)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值