【SpringBoot】springboot整合swagger、swagger-bootstrap-ui

(目前项目正在做微服务架构调整,之前微服务接口信息维护,是用的YApi,地址参数什么的,都是需要开发人员手动维护的,现在正在改造使用swagger替代。)

学习于【狂神说Java】一小时掌握Swagger技术_哔哩哔哩_bilibili

swagger-ui页面比较丑,改用bootstrap-ui替代。

1.引入依赖

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.3</version>
        </dependency>

2.SwaggerConfig配置

package pers.wk.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


import java.util.ArrayList;

@Configuration
@EnableSwagger2  //开启swagger
public class SwaggerConfig {

    @Value("${enableSwagger}")
    private Boolean enableSwagger;

    /**
     * 配置Swagger的Docket的bean实例
     *
     * @return
     */
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo())

                //为false不能在浏览器访问 " Could not render e, see the console."
                .enable(enableSwagger)
                .select()
                //指定扫描包
                .apis(RequestHandlerSelectors.basePackage("pers.wk.controller"))
                //路径过滤
                .paths(PathSelectors.any())
                .build()
                ;
    }

    private ApiInfo getApiInfo() {
        Contact contact = new Contact("", "", "");
        return new ApiInfo(
                "KAI丶的Swagger API文档",
                "Api Documentation",
                "1.0",
                "https://blog.csdn.net/qq_36762765",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>());
    }
}

3.application.yml

server:
  port: 8080

#配置swagger能否在浏览器访问
enableSwagger: true

4.接口注解配置

package pers.wk.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pers.wk.dto.Response;
import pers.wk.dto.UserDto;

@Api(tags = "CRM用户接口")
@RequestMapping(value = "/web/crm/api/")
public interface UserApi {

    @ApiOperation(value = "获取哟用户信息")
    @PostMapping(value = "/web/crm/api/user/getUserInfo")
    String getUses(UserDto UserDto);

    @ApiImplicitParam(name = "userName", value = "姓名", defaultValue = "杜甫", required = true)
    @GetMapping(value = "/web/crm/api/user/getUseName")
    Response<String> getUseName(@RequestParam String userName);


    @GetMapping(value = "user/getUseAge")
    Response<String> getUseAge();

    @PostMapping(value = "user/getUseName")
    Response<String> postUseName();


}
package pers.wk.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import pers.wk.api.UserApi;
import pers.wk.dto.Response;
import pers.wk.dto.UserDto;

@RestController
public class UserController implements UserApi {

    @Override
    public String getUses(@RequestBody UserDto UserDto) {
        UserDto user = new UserDto();
        user.setAge(122);
        user.setUserName("李白");
        return JSONObject.toJSONString(user);
    }

    @Override
    public Response<String> getUseName(String userName) {
        return Response.ok("杜甫");
    }

    @Override
    public Response<String> getUseAge() {
        return Response.ok("getUseAge");
    }

    @Override
    public Response<String> postUseName() {
        return Response.ok("postUseName");
    }
}
package pers.wk.dto;

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

@ApiModel(value = "用户实体类")
public class UserDto {

    @ApiModelProperty(value = "姓名", required = true, example = "李白")
    private String userName;

    @ApiModelProperty(value = "年龄", hidden = true)
    private Integer age;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

5.访问 http://localhost:8080/doc.html

6.问题记录:

访问swagger,服务端控制台报错:NumberFormatException

解决方法:Swagger2.9.2的NumberFormatException - 简书

    # 增加两个配置
           <dependency>
               <groupId>io.swagger</groupId>
               <artifactId>swagger-annotations</artifactId>
               <version>1.5.22</version>
           </dependency>
           <dependency>
               <groupId>io.swagger</groupId>
               <artifactId>swagger-models</artifactId>
               <version>1.5.22</version>
           </dependency>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值