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

SSM集成Swagger3

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

SSM集成Swagger3

第一步:添加Maven依赖


    io.springfox
    springfox-swagger2
    3.0.0



    io.springfox
    springfox-swagger-ui
    2.9.2



    com.github.xiaoymin
    knife4j-spring-boot-starter
    3.0.3

第二步:编写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();
	}

	
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				// 大标题
				.title("API接口文档")
				// 描述
				.description("API接口测试")
				// 版本号
				.version("1.0.0")
				.termsOfServiceUrl("").license("").licenseUrl("").build();
	}
}
第三步:修改applicationContext.xml




第四步: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访问不了,具体怎么解决,暂未找到解决方案,哪位同学如果解决了,肯请告知,感谢!

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

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

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