温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot中如何使用Swagger

发布时间:2021-08-03 14:36:04 来源:亿速云 阅读:217 作者:Leah 栏目:编程语言

这篇文章将为大家详细讲解有关SpringBoot中如何使用Swagger,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

项目结构

SpringBoot中如何使用Swagger

  1. 配置文件

    @Configuration
    @EnableSwagger2
    public class Swagger2Config {
        //swagger2的配置文件,可以配置swagger2的一些基本的内容,比如扫描的包等等
        @Bean
        public Docket defaultApi(){
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("默认分组").select()
                    .apis(RequestHandlerSelectors.basePackage("com.chenwenhuan.springbootlearning.controller"))
                    .paths(PathSelectors.any()).build();
        }
    
        // 预览地址:http://localhost:8082/swagger-ui.html#/
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("整合swagger构建测试系统api文档")
                    .description("接口访问地址:http://localhost:8082/")
                    .termsOfServiceUrl("http://localhost:8082/")
                    .version("1.0")
                    .build();
        }
    }


     

  2. pom.xml 新增依赖

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


     

  3. Controller注解

    @Api(value="用户api", tags="用户api")
    @RestController
    @RequestMapping("/user")
    public class UserController {
    
        @Autowired
        UserService userService;
    
        @GetMapping("/findAll")
        public List<User> findAll(){
            return userService.findAll();
        }
    
        @ApiOperation(value="获取用户信息", notes="根据url的id来获取信息")
        @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户ID", paramType = "query",required = true)})
        @GetMapping("/getUser")
        public User getUser(@RequestParam int id){
            return userService.getUserById(id);
        }
    
        @PostMapping("/creatUser")
        public void creatUser(User user){
            System.out.println(user);
            userService.insert(user);
        }
    
        @PostMapping("/creatUserList")
        public void creatUserList(@RequestBody List<User> list){
            userService.insertList(list);
        }
    }


     

  4. 启动项目访问预览地址  http://localhost:8082/swagger-ui.html#/,效果如下。(可以看出此处很多信息都源自配置Swagger2Config)SpringBoot中如何使用Swagger

  5. 示例1

SpringBoot中如何使用Swagger

输入参数点击Try it out!,可以查看返回码和返回数据

SpringBoot中如何使用Swagger

  • 示例2:

参考右边提示,按格式输入Json格式的对象数组

SpringBoot中如何使用Swagger

  • 示例3:

controller需要注解 paramType = "query",否则需要按Example Value 格式输入。required:是否必输

@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户ID", paramType = "query",required = true)})

SpringBoot中如何使用Swagger

关于SpringBoot中如何使用Swagger就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI