栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合swagger

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合swagger

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。可用于:1.接口的文档在线自动生成、2.功能测试。

首先创建一个简单的maven项目,整合swagger一定要注意springboot与swagger的版本要适配,pom文件如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.1.RELEASE
         
    
    com.example
    swagger
    0.0.1-SNAPSHOT
    swagger
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.2
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            io.springfox
            springfox-swagger2
            2.7.0
        
        
            io.springfox
            springfox-swagger-ui
            2.7.0
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


项目的目录如下所示,创建swagger配置文件,具体内容如下,包自己导入。

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }
    private ApiInfo webApiInfo(){
         return new ApiInfoBuilder()
         .title("API文档")
         .description("接口定义")
         .version("1.0")
         .contact(new Contact("mzz", "http://www.baidu.com",
                "1345656307@qq.com"))
         .build();
         }
}

 数据库信息:

CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL COMMENT '姓名',
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  `sex` varchar(1) DEFAULT NULL COMMENT '性别',
  `address` varchar(255) DEFAULT NULL COMMENT '地址',
  `phone` varchar(20) DEFAULT NULL COMMENT '电话',
  `create_time` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8


insert into `user` (`id`, `name`, `age`, `sex`, `address`, `phone`, `create_time`) values('2','张三','22','男','上海市','13867675656','2020-12-26');
insert into `user` (`id`, `name`, `age`, `sex`, `address`, `phone`, `create_time`) values('3','李四','20','男','合肥市','13978786565','2020-11-18');
insert into `user` (`id`, `name`, `age`, `sex`, `address`, `phone`, `create_time`) values('5','赵柳','20','男','南京市','13978786565','2020-11-18');
insert into `user` (`id`, `name`, `age`, `sex`, `address`, `phone`, `create_time`) values('6','钱望','22','男','深圳市','13867675656','2020-11-18');
insert into `user` (`id`, `name`, `age`, `sex`, `address`, `phone`, `create_time`) values('7','李云','20','男','合肥市','13978786565','2020-11-18');
insert into `user` (`id`, `name`, `age`, `sex`, `address`, `phone`, `create_time`) values('8','张傅','22','男','上海市','13867675656','2020-11-18');
insert into `user` (`id`, `name`, `age`, `sex`, `address`, `phone`, `create_time`) values('9','李青','20','男','合肥市','13978786565','2020-11-18');
insert into `user` (`id`, `name`, `age`, `sex`, `address`, `phone`, `create_time`) values('10','王二发','22','男','昆明市','13867675656','2020-11-18');
insert into `user` (`id`, `name`, `age`, `sex`, `address`, `phone`, `create_time`) values('12','王得贵','22','女','成都市','13867674565','2022-05-10');
insert into `user` (`id`, `name`, `age`, `sex`, `address`, `phone`, `create_time`) values('13','lih','23','男','我的粉丝的方式电池','12345678901','2022-05-09');

数据库:

 application.properties配置文件:

# 服务端口
server.port=8080

# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/users?serverTimezone=GMT%2B8&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=

mybatis.mapper-locations=classpath:mapper/*.xml

#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

entity部分代码,使用lombok注解:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private int age;
    private String sex;
    private String address;
    private String phone;
    private String createTime;
}

mapper部分代码,只简单的写了二个方法:

public interface UserMapper {

    //根据id查询
    public User getUserById(int id);

    //查询所有
    public List getAllUser();



}

service及实现类部分代码:(代码跟mapper差不多,遵循规范最好写一下)

public interface UserService {

    //根据id查询
    public User getUserById(int id);

    //查询所有
    public List getAllUser();


}

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(int id) {
        User user = userMapper.getUserById(id);
        return user;
    }

    @Override
    public List getAllUser() {
        return userMapper.getAllUser();
    }
    }

}

xml部分代码:





    
        select id,name,age,sex,address,phone,create_time from user
    



controller代码:

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "id获取用户信息")
    @GetMapping("{id}")
    public User getUserById(@PathVariable("id") int id){
        return userService.getUserById(id);
    }

    @ApiOperation(value = "获取所有用户信息")
    @GetMapping("/allUser")
    public List getAllUser(){
        return userService.getAllUser();
    }

}

启动类(注意注解):

@SpringBootApplication
@MapperScan("com.example.swagger.mapper")
public class SwaggerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SwaggerApplication.class, args);
    }

}

最后启动项目,输入地址http://localhost:8080/swagger-ui.html#/,出现该页面表示swagger整合成功。

点击对应的接口进行测试:

 

1.查询所有用户信息:

2.通过id查询用户

 所有结果与数据库结果对应,swagger整合成功,测试成功。

注:上述entity,mapper,service以及xml等都可以通过mybatis-plus来实现。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/975508.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号