Swagger笔记

9 篇文章 0 订阅
2 篇文章 0 订阅

因为使用的是swagger3.0
所以需要多导入一个包springfox-boot-starter
新版访问swagger路径为
http://localhost:8081/swagger-ui/index.htm

   <!--导入swagger包-->
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

swaggerConfig配置


@Configuration
@EnableSwagger2 //开启swagger
//3.0版本默认访问路径
//http://localhost:8080/swagger-ui/index.html
public class SwaggerConfig {

    @Value("${swagger.show}")
    private boolean swaggerShow;
    //配置bean实例
    @Bean
    public Docket docket()
    {

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .groupName("wu")//设置组
                .enable(swaggerShow)//是否开启swagger
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wu.controller"))
                .build()
                ;
        /*
        * RequestHandlerSelectors,配置要扫描接口的方式
        *.basePackage(),配置要扫描的包
        *.any():扫描全部
        *.none()不扫描
        *.withClassAnnotation()扫描类上的注解
        *.withMethodAnnotation()扫描方法上的注解
        *.paths(PathSelectors.ant("/wu/**"))过滤什么路径
        * */
    }
    //配置apiInfo 信息
    public ApiInfo apiInfo()
    {
        //作者信息
        Contact contact = new Contact("wu", "", "");
        return new ApiInfo(
                "Api 标题",
                "Api 描述",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}

application.yaml开启开发者环境

spring:
  profiles:
    active: dev

application-dev.yaml配置开发者环境

server:
  port: 8081
swagger:
  show: true

helloController配置


@RestController
public class helloController {
    @GetMapping(value = "/hello")
    public String hello()
    {
        return "hello,world";
    }
    @PostMapping(value = "/user")
    public User user(String username,String password)
    {
        return new User(username,password);
    }

    @PostMapping(value = "/userModel")
    public String userModel(String username, String password, Model model)
    {
        model.addAttribute("msg",new User(username,password));

        return "helloModelAndView";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值