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

springboot04、swagger配置

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

springboot04、swagger配置

springboot04、swagger配置

前言:

springboot的swagger配置与SSM稍微有些不同,SSM对2.9.0以上的兼容性很差,但是springboot就可以使用2.9.0以上的包了,其实区别不算太大,除了能对对象直接操作外就是页面更清爽了。

目录

1、pom依赖

2、swagger配置文件

3、接口api写法

4、启动效果:【http://127.0.0.1:8088/swagger-ui.html】

5、使用方法

​编辑

6、可能出现的异常总结:


1、pom依赖


    io.springfox
    springfox-swagger2
    2.9.2


    io.springfox
    springfox-swagger-ui
    2.9.2

2、swagger配置文件

这里单独创建了一个包【com.item.swagger】来放置swagger的配置文件

需要注意的是:【com.item.controller】这里需要改成自己的包位置。

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();
    }

    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 设置页面标题
                .title("Spring Boot集成Swagger2接口总览")
                // 设置接口描述
                .description("Swagger接口")
                // 设置联系方式
                .contact(new Contact("测试swagger","http://localhost:8080/",""))
                // 设置版本
                .version("1.0")
                // 构建
                .build();
    }

}

3、接口api写法

我写了一套的注释方法,一目了然

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;

    
    @GetMapping("/GetInfoApi")
    @ApiOperation(value = "获取信息",notes = "没啥留言的")
    public Object GetInfoApi(){
        List list=usersService.GetInfo();
        HashMap map=new HashMap();
        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 list=usersService.SelectName(nickName);
        HashMap map=new HashMap();
        map.put("state",true);
        map.put("msg","成功");
        map.put("result",list);
        return map;
    }

    
    @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 map=new HashMap();
        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;
    }

    
    @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 map=new HashMap();
        map.put("state",true);
        map.put("msg","成功");
        map.put("result",users);
        return map;
    }

    
    @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 map=new HashMap();
        map.put("state",true);
        map.put("msg","成功");
        map.put("result","");
        return map;
    }

    
    @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 map=new HashMap();
        map.put("state",true);
        map.put("msg","成功");
        map.put("result","");
        return map;
    }
}

4、启动效果:【http://127.0.0.1:8088/swagger-ui.html】

这里为了看着方便,我将服务路径改为了【/】

# 服务路径
server.servlet.context-path=/

5、使用方法

POST的也类似

6、可能出现的异常总结:

1、SwaggerConfig的配置文件中忘记写注解,就2个注解:

@Configuration
@EnableSwagger2

2、接口中的注解:

@Api("用户操作接口")
@ApiOperation(value = "获取信息",notes = "没啥留言的")
@ApiImplicitParams({
      @ApiImplicitParam(name = "nickName",required = true,paramType = "query",dataType = "String",value = "通过昵称模糊查询")
})

3、没有明确接口的访问类型,导致出现一堆的同名不同访问类型的接口提示。

使用@GetMapping或者@PostMapping就可以解决此问题。

希望能给大家带来帮助。

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

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

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