集成swagger
集成swagger
部署配置
在pom文件中添加插件swagger:
<properties>
<swagger.version>2.7.0</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
创建swagger配置文件
package com.course.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.pathMapping("/")
.select()
.paths(PathSelectors.regex("/.*"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("myApiListDoc")
.contact(new Contact("hanzy","xxx","333@qq.com"))
.description("这是swagger生成的API文档")
.version("1.0.0")
.build();
}
}
给接口添加注解
给类添加注解@Api
@RestController
@Api(value = "/",description = "这是我全部的get方法")
public class MyGetMethod {}
给方法添加注解@ApiOperation
@RequestMapping(value = "/getCookies",method = RequestMethod.GET)
@ApiOperation(value = "通过本方法可获取cookie",httpMethod = "GET")
public String getCookies(HttpServletResponse response){
//HttpServletRequest 装请求信息类
//HttpServletResponse 装响应信息类
Cookie cookie = new Cookie("login","true");
response.addCookie(cookie);
return "成功获取cookie";
}
分类:
接口测试
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?