Springboot swagger配置过程详解(idea社区版2023.1.4+apache-maven-3.9.3-bin)

 更新时间:2023年07月25日 15:57:26   作者:红目香薰  
这篇文章主要介绍了Springboot-swagger配置(idea社区版2023.1.4+apache-maven-3.9.3-bin),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

本文是配置swagger的文章,版本是【2.9.2】

1、pom.xml的jar包引入

这里为了后文操作方便,我多添加了一个commons-lang3的包用于字符串的非空判断。

    <!-- swagger包这里2.9.2 -->
    <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>
    <!-- 用作字符串非空判断 -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.12.0</version>
    </dependency>

2、创建【com.item.swagger】的配置文件【SwaggerConfig.java】

package com.item.swagger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.ApiInfo;
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 {
    private static Logger log = LoggerFactory.getLogger(SwaggerConfig.class);
    @Bean
    public Docket createRestApi() {
        log.info("进入到swagger的配置中");
        return new Docket(DocumentationType.SWAGGER_2)
                // 指定构建api文档的详细信息的方法:apiInfo()
                .apiInfo(apiInfo())
                .groupName("红目香薰·为大一孩子准备的")
                .select()
                // 指定要生成api接口的包路径,这里把controller作为包路径,生成controller中的所有接口
                .apis(RequestHandlerSelectors.basePackage("com.item.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    /**
     * 构建api文档的详细信息
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 设置页面标题
                .title("Spring Boot集成Swagger2接口总览")
                // 设置接口描述
                .description("Swagger接口")
                // 设置联系方式
                .contact(new Contact("测试swagger","https://laoshifu.blog.csdn.net/",""))
                // 设置版本
                .version("1.0")
                // 构建
                .build();
    }
}

配置文件在上文的代码中就可以看到,并且我写了很多注释,应该是可以帮助到大家的。

3、示例swagger注解写法

package com.item.controller;
import com.item.model.Users;
import com.item.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
@Api("用户操作接口")
@RestController
@CrossOrigin
public class UsersController {
    @Autowired
    private UsersService usersService;
    /**
     * 获取所有信息
     * @return
     */
    @GetMapping("/GetInfoApi")
    @ApiOperation(value = "获取信息",notes = "没啥留言的")
    public Object GetInfoApi(){
        List<Users> list=usersService.GetInfo();
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("state",true);
        map.put("msg","成功");
        map.put("result",list);
        return map;
    }
    @GetMapping("/GetName")
    @ApiOperation(value = "获取信息",notes = "没啥留言的")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nickName",required = true,paramType = "query",dataType = "String",value = "通过昵称模糊查询")
    })
    public Object GetName(HttpServletRequest request,Model model){
        String nickName = request.getParameter("nickName");
        List<Users> list=usersService.SelectName(nickName);
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("state",true);
        map.put("msg","成功");
        map.put("result",list);
        return map;
    }
    /**
     * 添加信息
     * @param userName
     * @param pwd
     * @param nickName
     * @return
     */
    @PostMapping(value = "/UserAddInfoApi")
    @ApiOperation(value = "添加",notes = "没啥留言的")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",required = true,paramType = "query",dataType = "String",value = "用户名"),
            @ApiImplicitParam(name = "pwd",required = true,paramType = "query",dataType = "String",value = "密码"),
            @ApiImplicitParam(name = "nickName",required = true,paramType = "query",dataType = "String",value = "昵称")
    })
    public Object UserAddInfoApi(String userName,String pwd,String nickName){
        HashMap<String,Object> map=new HashMap<String,Object>();
        if(
                StringUtils.isEmpty(userName)||
                        StringUtils.isEmpty(pwd)||
                        StringUtils.isEmpty(nickName)
        ){
            map.put("state",false);
            map.put("msg","参数不润许为空");
            map.put("result","");
            return map;
        }
        usersService.UsersAddInfo(userName, pwd, nickName);
        map.put("state",true);
        map.put("msg","成功");
        map.put("result","");
        return map;
    }
    /**
     * 单个查询
     * @param id
     * @return
     */
    @GetMapping("/UsersSelectById")
    @ApiOperation(value = "id查询",notes = "没啥留言的")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",required = true,paramType = "query",dataType = "String",value = "编号")
    })
    public Object UsersSelectById(String id){
        Users users = usersService.UsersSelectById(Integer.parseInt(id));
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("state",true);
        map.put("msg","成功");
        map.put("result",users);
        return map;
    }
    /**
     * 修改api
     * @param id
     * @param pwd
     * @return
     */
    @PostMapping(value = "/UserUpdateInfoApi")
    @ApiOperation(value = "添加",notes = "没啥留言的")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",required = true,paramType = "query",dataType = "String",value = "编号"),
            @ApiImplicitParam(name = "pwd",required = true,paramType = "query",dataType = "String",value = "密码"),
    })
    public Object UserUpdateInfoApi(String id,String pwd){
        usersService.UsersUpdateInfo(pwd,Integer.parseInt(id));
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("state",true);
        map.put("msg","成功");
        map.put("result","");
        return map;
    }
    /**
     * 删除api
     * @param id
     * @return
     */
    @GetMapping(value = "/UsersDeleteById")
    @ApiOperation(value = "根据id删除",notes = "没啥留言的")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",required = true,paramType = "query",dataType = "String",value = "编号")
    })
    public Object UsersDeleteById(String id){
        usersService.UsersDeleteById(Integer.parseInt(id));
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("state",true);
        map.put("msg","成功");
        map.put("result","");
        return map;
    }
}

4、swagger的常用注解

@Api:用于标识一个类为 Swagger 文档的资源。 用法示例:@Api(value = "User API", = "User Management")

@ApiOperation:用于描述一个方法的操作信息。 用法示例:@ApiOperation(value = "Get user by ID", notes = "Returns a user based on ID")

@ApiParam:用于描述一个方法参数的信息。 用法示例:@ApiParam(value = "User ID", required = true) @PathVariable("id") Long id

@ApiModel:用于描述一个数据模型(DTO)的信息。 用法示例:@ApiModel(value = "User", description = "User details")

@ApiModelProperty:用于描述一个属性或字段的信息。 用法示例:@ApiModelProperty(value = "User name")

@ApiResponses:用于描述一个方法的多个响应。 用法示例:@ApiResponses(value = { @ApiResponse(code = 200, message = "Success"), @ApiResponse(code = 404, message = "Not Found") })

@ApiIgnore:用于指定某个方法或类不在 Swagger 文档中显示。 用法示例:@ApiIgnore

5、访问效果

网页的路径是【http://127.0.0.1:8088/MyAPI/swagger-ui.html】 ,拼接方法是【ip:port/path/swagger-ui.html】

这里的path是在配置文件中的服务路径【server.servlet.context-path=/MyAPI】

总结

swagger是比较常用的一种API交流插件,JAVA和.NET都用,且大多数程序员都会使用,方便大家交流API。

到此这篇关于Springboot-swagger配置(idea社区版2023.1.4+apache-maven-3.9.3-bin)的文章就介绍到这了,更多相关Springboot-swagger配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • KotlinScript构建SpringBootStarter保姆级教程

    KotlinScript构建SpringBootStarter保姆级教程

    这篇文章主要为大家介绍了KotlinScript构建SpringBootStarter的保姆级教程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • MyBatis-plus的五种批量插入方式对比分析

    MyBatis-plus的五种批量插入方式对比分析

    本文主要介绍了MyBatis-plus的五种批量插入方式对比分析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Java各种排序算法汇总(冒泡,选择,归并,希尔及堆排序等)

    Java各种排序算法汇总(冒泡,选择,归并,希尔及堆排序等)

    这篇文章主要介绍了Java各种排序算法,以大量实例形式汇总分析了Java常用的各种排序算法,包括冒泡排序、快速排序、堆排序、插入排序、希尔排序、选择排序、归并排序等,需要的朋友可以参考下
    2015-11-11
  • SpringCloud将Nacos作为配置中心实现流程详解

    SpringCloud将Nacos作为配置中心实现流程详解

    这篇文章主要介绍了Springcloud中的Nacos Config服务配置,本文以用户微服务为例,进行统一的配置,结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-10-10
  • Spring AOP的底层实现方式-代理模式

    Spring AOP的底层实现方式-代理模式

    这篇文章主要介绍了Spring AOP的底层实现方式-代理模式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • SpringBoot入门原理及优势分析

    SpringBoot入门原理及优势分析

    本篇文章主要来为大家介绍SpringBoot入门原理及优势分析,有需要的朋友可以借鉴参考下,希望可以有所帮助,祝大家多多进步,早日升职加薪
    2021-09-09
  • Spring的初始化和XML解析的实现

    Spring的初始化和XML解析的实现

    这篇文章主要介绍了Spring的初始化和XML解析的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • Java的访问修饰符与变量的作用域讲解

    Java的访问修饰符与变量的作用域讲解

    这篇文章主要介绍了Java的访问修饰符与变量的作用域讲解,是Java入门学习中的基础知识,需要的朋友可以参考下
    2015-09-09
  • springboottest测试依赖和使用方式

    springboottest测试依赖和使用方式

    这篇文章主要介绍了springboottest测试依赖和使用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Maven项目部署到Jboss出现Failed to create a new SAX parser

    Maven项目部署到Jboss出现Failed to create a new SAX parser

    这篇文章主要为大家详细介绍了Maven项目部署到Jboss出现Failed to create a new SAX parser的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11

最新评论