swagger3.0(springboot)消除basic-error-controller


1、新建springboot项目,可以通过https://start.spring.io/快速生成springboot项目。

2、引入jar依赖:

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-boot-starter</artifactId>
  <version>3.0.0</version>
</dependency>

3、编写Controller,并配置相应的注解,见下图

package com.demo.incubator.swaggerdemo.controller;

import java.util.Map;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/demo")
@Api(protocols = "http,https", produces = "application/json", tags = "mock样例")
public class SwaggerDemoController {
    @PostMapping("/mokeInvoke")
    @ApiOperation(value = "mock方法样例", notes = "返回一个字符串")
    public String mockInvoke(@ApiParam(value = "显示的信息", required = true) @RequestParam("showMsg") String showMsg) {
        return showMsg;
    }

    @PostMapping("/mokeInvoke2")
    @ApiOperation(value = "mock方法样例2", notes = "返回一个字符串")
    @ApiImplicitParams({@ApiImplicitParam(value = "手机号", name = "userPhone", required = true, paramType = "query"),
            @ApiImplicitParam(value = "姓名", name = "userName", required = true, paramType = "query"),
            @ApiImplicitParam(value = "地址", name = "address", required = true, paramType = "query"),
            @ApiImplicitParam(value = "系统来源", name = "appid", required = true, paramType = "header")})
    public String mockInvoke2(@RequestBody Map<String, Object> params) {
        return JSON.toJSONString(params);
    }

}

4、启动项目,访问链接地址http://localhost:8080/swagger-ui/index.html(端口换成自己配置的端口),即可看到swagger页面,如下图

5、但是会发现多出来basic-error-controller、operation-handler、web-mvc-links-handler,可以通过新建一个Configration来消除,只保留我们对外提供的接口

package com.demo.incubator.swaggerdemo.config;

import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfiguration {
//注意标黄的地方要求能匹配到你所有的接口路径,比如我的例子中两个接口都是以demo开始的,/demo/mockInvoke和/demo/mockInvoke2 @Bean Docket xiaokeai() {
return new Docket(DocumentationType.OAS_30).useDefaultResponseMessages(false) .produces(Stream.of("application/xml", "application/json").collect(Collectors.toSet())).select() .paths(PathSelectors.regex("/demo/.*")).build() .protocols(Stream.of("http", "https").collect(Collectors.toSet())); } }

6、再次启动项目,就会发现只保留了自有的controller暴露的接口。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM