REST接口集成Swagger

本文详细介绍了如何在Spring Boot项目中集成Swagger,包括添加依赖、创建配置类及注解修改,实现RESTful API的文档自动生成,提高开发效率。

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

  • 添加pom依赖
<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>
  • 添加swaggerConfig类

注:spring配置文件中必须包含swaggerconfig类所在的包:

<context:component-scan base-package="com.xxl.job.admin.controller,com.xxl.job.admin.scan" />

@Configuration
@EnableWebMvc
@EnableSwagger2
@ComponentScan(basePackages = { "com.sts.rest.controller" })
public class SwaggerConfig {

	@Bean
	public Docket platformApi() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).forCodeGeneration(true);
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("xxx-API").description("©2017 Copyright. Powered By xxx.")
				// .termsOfServiceUrl("")
				.contact(new Contact("xxx-API", "", "xxxxx@qq.com")).license("xxx Group").version("1.0")
				.build();
	}

}
  • 修改spring配置

Springmvc-context.xml文件中添加配置:

<mvc:default-servlet-handler />

<mvc:annotation-driven />

  • 修改接口注解
@ApiOperation(value = "任务管理页面初始化")
@RequestMapping(value="/index",method={RequestMethod.POST, RequestMethod.GET})
@ResponseBody
public Map<String, Object> index(@RequestParam(required = false, defaultValue = "-1") int jobGroup) {

	// package result
	Map<String, Object> maps = new HashMap<String, Object>();		    
	return maps;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值