Spring Boot与Swagger2的奇妙之旅

在Spring Boot工程的pom文件中引入swagger2的依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

编写swagger2的配置文件

@Configuration
@EnableSwagger2
public class Swagger2 {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //com.person.demo 项目的包名
                .apis(RequestHandlerSelectors.basePackage(("com.person.demo")))
                .paths(PathSelectors.any())
                .build();
    }
    public ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("springboot利用swagger构建api文档")
                .description("简单优雅的restful风格")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }
}

使用swagger2示例,通过@ApiOperation注解描述生成在线文档的具体API说明,其中value值为该接口的名称,notes值为该接口的详细文档说明。如果不需要某个接口生成文档,只需要再加上@ApiIgnore注解即可。启动工程,在浏览器中访问http://localhost:8080/swagger-ui.html 浏览器就会显示在线文档的界面了(8080为工程启动的端口号)

@CrossOrigin
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ApiOperation(value = "登录",notes = "用户登录接口")
    public ResultVo login(@RequestParam("account") String account, @RequestParam("password") String password ){
       return loginService.login(account,password);
     
    @RequestMapping(value = "/getPhoto",method = RequestMethod.GET)
    @ApiIgnore //使用该注解忽略这个API
    public ResultVo getPhoto(String account){
        return loginService.getPhoto(account);
    }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值