Swagger整合SpringBoot

Swagger整合SpringBoot


引入的依赖
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

Configuration类
package cn.liuweiwei.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.builders.RequestHandlerSelectors;
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 createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/").select()
                .apis(RequestHandlerSelectors.basePackage("cn.liuweiwei.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                .title("Springboot整合Swagger")
                .description("Springboot整合swagger详细信息....")
                .version("版本号1.0")
                .contact(new Contact("vivi","http://www.vivi.cn","2500247684@qq.com"))
                .license("The Vivi License")
                .licenseUrl("http://www.vivi.cn")
                .build());
    }

}


浏览器地址栏访问的路径

​ http://localhost:8080/swagger-ui.html

Swagger相关注解
  • @Api(tags="")
  • @ApiOperation(value="",notes="")
  • @ApiImplicitParams(name="",value="",dataType="","paramType="")
  • @ApiResponses({@ApiResponse(code=404,message="")})

package cn.liuweiwei.controller;

import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
@Api(tags = "用户服务相关接口的描述")
public class HelloController {

    @GetMapping("/findAll")
    @ResponseBody
    @ApiOperation(value = "用来查询所有用户的信息",notes = "返回所有用户的数据")
    @ApiResponses({//设置响应的状态码的相关信息
        @ApiResponse(code = 404,message = "访问的资源不存在"),
        @ApiResponse(code = 500,message = "服务器繁忙...")
    })
    public Map<String,Object> findAll(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("success",true);
        map.put("noSuccess",false);
        return map;
    }

    @PostMapping("/save/{username}/{password}")
    @ResponseBody
    @ApiOperation(value = "保存一个用户的接口",notes = "一次只能保存一个用户")
    @ApiImplicitParams({ //对接口的参数进行详细说明
        @ApiImplicitParam(name = "username",value = "用户名",dataType = "String",
                          defaultValue = "vivi",paramType = "path"),
        @ApiImplicitParam(name = "password",value = "用户的密码",dataType = "String",
                          defaultValue = "123",paramType = "path")
    })
    public String save(@PathVariable("username") String username,
                       @PathVariable("password") String password){
        System.out.println(username+"==" + password);
        return "保存成功!!";
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值