SSM集成Swagger3

第一步:添加Maven依赖

<!--springfox的核心jar包-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>
<!-- 官方UI包-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
<!--        漂亮UI包-->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>

第二步:编写Swagger配置文件

@Configuration
@EnableSwagger2 // 重要!
//@EnableWebMvc
@ComponentScan(basePackages = { "com.hc" }) // Swagger扫描的package
public class SwaggerConfig {
	@Bean
	public Docket api() {
		return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()//选择哪些路径和API会生成document 
                //扫描指定包中的swagger注解
                //.apis(RequestHandlerSelectors.basePackage("com.hc"))
                //扫描所有有注解的api,用这种方式更灵活
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //扫描所有的api(没有添加注解也可以扫描出来),用这种方式更直接
                //.apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
	}

	/**
	 * 这是匹配api的信息
	 * 
	 * @return
	 */
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				// 大标题
				.title("API接口文档")
				// 描述
				.description("API接口测试")
				// 版本号
				.version("1.0.0")
				.termsOfServiceUrl("").license("").licenseUrl("").build();
	}
}

第三步:修改applicationContext.xml

<!--向容器自动注入配置-->
<context:annotation-config />
<!--重要!将你的SwaggerConfig配置类注入-->
<bean class="com.hc.config.SwaggerConfig"/>

第四步:Controller代码:

@Api(tags = "部门管理", value = "部门接口")
@RestController
@RequestMapping("/dept")
public class DeptController {
    @ApiResponse(message = "测试", code = 200)
    @ApiOperation(value = "fun", notes = "测试方法fun", httpMethod = "GET")
    @GetMapping("/fun")
    public String fun() {
        return "fun";
    }

    @PutMapping(value = "/v1/{dname}")
    @ApiOperation(value = "修改部门信息", notes = "", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "deptno", value = "部门ID", required = true, dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(paramType = "path", name = "dname", value = "部门名称", required = true, dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "body", name = "dept", value = "用户实体", required = true, dataType = "Dept", dataTypeClass = Dept.class)
    })
    public String fun(@RequestParam(name = "deptno", value = "deptno", required = false) Integer deptno,
                      @PathVariable(value = "dname", required = true) String dname,
                      @RequestBody(required = true) Dept dept,
                      HttpServletRequest request) {
        String token = request.getHeader("token");
        return token + " " + deptno + " " + dname + " " + dept;
    }
}

第五步:浏览器访问

官方UI

网址: http://localhost:8080/项目名称/swagger-ui.html
在这里插入图片描述

漂亮UI

网址:http://localhost:8080/项目名称/doc.html#/home
在这里插入图片描述

第一步中,引入的springfox-swagger-ui的版本为2.9.2,如果改成3.0.0以上,发现官方UI访问不了,具体怎么解决,暂未找到解决方案,哪位同学如果解决了,肯请告知,感谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁云亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值