十分钟玩转Springboot集成Swagger

Swagger是干什么的?有什么作用等?就不多说,自行官网查看或者百度。

1.创建一个springboot项目

省略创建步骤···

2.在pom.xml引入Swagger的相关maven

       <!--引入swagger2-->
        <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>

3.配置configSwagger

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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;

import java.util.ArrayList;

@Configuration
@EnableSwagger2  //开启swagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(Environment environment){
        return  new Docket(DocumentationType.SWAGGER_2)
                .enable(true)  //是否开启Swagger
                .apiInfo(apiInfo())  //API信息
                .groupName("Demo") //组的名称
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.controller")) //选择扫描的包
                .paths(PathSelectors.any())  //选择过滤路径
                .build();
    }
    private ApiInfo apiInfo(){
        //联系人信息
        Contact contact = new Contact("张三","http://www.baidu.com","shang0869@sina.com");
        return  new ApiInfo(
                "Swagger使用学习",  //标题
                "自律更自由",  //表述
                "1.0",  //版本
                "http://www.baidu.com",
                contact, //联系人
                "Apache 2.0", //apache版本
                "http://www.apache.org/licenses/LICENSE-2.0", //许可证
                new ArrayList());
    }

}

4.启动项目

访问Swagger:  http://localhost:8080/swagger-ui.html

在这里插入图片描述

5.编写实体类和接口

实体类

package com.example.pojo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("用户实体类")
public class User {

    @ApiModelProperty("用户名")
    public String username;

    @ApiModelProperty("密码")
    public String password;

}

接口

package com.example.controller;

import com.example.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    @ApiOperation("hello接口")
    public String hello(){
        return "HELLO WORLD!";
    }

    @GetMapping("/user")
    @ApiOperation("用户接口")
    public User hello1(@ApiParam("用户名") String username){
        //处理相关数据
        return new User();
    }
}

5.重启项目,查看结果

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

巴黎有个小铁匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值