swagger2+springboot - Go语言中文社区

swagger2+springboot


首先添加pom依赖

	    <!-- swagger -->
	    <dependency>
	        <groupId>io.springfox</groupId>
	        <artifactId>springfox-swagger2</artifactId>
	        <version>2.5.0</version>
	    </dependency>
	    <!-- swagger-ui -->
	    <dependency>
	        <groupId>io.springfox</groupId>
	        <artifactId>springfox-swagger-ui</artifactId>
	        <version>2.5.0</version>
	    </dependency>   

swagger配置类

package com.example.demo.config;

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

@Configuration
@EnableSwagger2
public class SwaggerConfig {
  //swagger2的配置
  @Bean
  public Docket createRestApi() {
      return new Docket(DocumentationType.SWAGGER_2)
              .apiInfo(apiInfo())
              .enable(true)
              .select()
              //接口包的路径  
              .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
              .paths(PathSelectors.any())
              .build();
  }
  
  //构建 api文档的详细信息
  private ApiInfo apiInfo() {
      return new ApiInfoBuilder()
              //页面标题
              .title("使用 Swagger2 构建RESTful API")
              //创建人 名称 网址 邮箱
              .contact(new Contact("zhangc", "", "zhangc@jsyl.com.cn"))
              //版本号
              .version("1.0")
              //描述
              .description("API 测试")
              .build();
  }
}

接口编写

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.DemoBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(value = "示例接口")
@RequestMapping("/demo")
@RestController
public class DemoController {

	@GetMapping("/getDemo")
	@ApiOperation(value="示例方法",notes="示例方法")
	public DemoBean getDemo(@RequestParam("str1")@ApiParam(required = true,name="str1")String str1,@RequestParam("str2")@ApiParam(required = true,name="str2")String str2){
		DemoBean bean = new DemoBean();
		bean.setField1(str1);
		bean.setField2(str2);
		return bean;
	}
}

这就完成了,打开网页http://localhost:8080/swagger-ui.html

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/zhangcjsyl/article/details/86604792
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 17:50:31
  • 阅读 ( 998 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢