Eclipse中对Spring Boot 项目使用Swagger
【摘要】
1.添加swagger依赖:
dependencies {
compile('io.springfox:springfox-swagger2:2.2.2')
compile('io.springfox:springfox-swagger-ui:2.2.2')
}
2.添加Swagger2Config.java类:
pac...
1.添加swagger依赖:
dependencies {
compile('io.springfox:springfox-swagger2:2.2.2')
compile('io.springfox:springfox-swagger-ui:2.2.2')
}
2.添加Swagger2Config.java类:
package com.yunfeng.YFApiCommon;
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.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@Configuration
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.yunfeng.YFApiCommon"))//注意这个包名,根据项目自行修改
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("YFApiCommon")
.description("API根地址:http://localhost:8080/")
.build();
}
}
3.对RestController类方法进行注释:
package com.yunfeng.YFApiCommon;
import com.yunfeng.Response.*;
import com.yunfeng.Request.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;
@RestController
public class TestController {
@ApiOperation(value = "查找用户", httpMethod = "POST")
@RequestMapping("/api/Test1")
public TestResponse showPerson(@RequestBody TestRequest input) {
TestResponse res = new TestResponse();
res.setUserName("zzzili");
res.setUserPass("pass");
return res;
}
}
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
4.项目启动后访问:
http://127.0.0.1:8080/swagger-ui.html
文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。
原文链接:zzzili.blog.csdn.net/article/details/79346392
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)