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

Swagger接口文档的配置与使用

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

Swagger接口文档的配置与使用

Swagger接口文档的配置与使用
  1. 新建一个maven项目
  2. 配置pom.xml 添加依赖

springfox-swagger2

  		
            io.springfox
            springfox-swagger2
            2.9.2
        

springfox-swagger-ui

 		
            io.springfox
            springfox-swagger-ui
            2.9.2
        
  1. 配置 resources 目录下的 spring-mvc.xml文件


	
	
	
	
	
	
		
		
	

	

	
	


  1. 配置 web.xml 文件


	
		DispatcherServlet
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:spring-mvc.xml
		
		1
	
	
		DispatcherServlet
		*.do
		/swagger/*
		/api-docs
	
	
		DispatcherServlet
		*.action
	


  1. 添加 配置类 SwaggerConfig
@Configuration
@EnableWebMvc
@EnableSwagger2
public class SwaggerConfig{
    @Bean
    public Docket api() {
        return new Docket(documentationType.SWAGGER_2)
                .select()            .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build()
                .apiInfo(apiInfo());
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("标题")
                .description("http不对外开放接口")
                .version("1.0.0")
                .termsOfServiceUrl("http://xxx.xxx.com")
                .license("假装这里有license")
                .licenseUrl("http://xxx.xxx.com")
                .build();
    }

}

  1. 编写控制器(没有与接口内容对应匹配)
@Api(value = "User控制器")
@RestController
public class HelloController {
    @ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @RequestMapping(value="hi",method = {RequestMethod.GET})
    public String hi(){
        return "hi";
    }
}
  1. 测试
    http://localhost:8089/swagger/swagger-ui.html

    http://localhost:8089/doc.html

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

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

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