springboot集成swagger

1.什么是 Swagger?

Swagger™的目标是为REST APIs 定义一个标准的,与语言无关的接口,使人和计算机在看不到源码或者看不到文档或者不能通过网络流量检测的情况下能发现和理解各种服务的功能。当服务通过Swagger定义,消费者就能与远程的服务互动通过少量的实现逻辑。类似于低级编程接口,Swagger去掉了调用服务时的很多猜测。 
浏览 Swagger-Spec 去了解更多关于Swagger 项目的信息,包括附加的支持其他语言的库。


提供了swagger-ui功能:http://127.0.0.1:8666/econtract/swagger-ui.html#/

2.Springboot集成Swagger

2.1添加pom依赖

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.2.2</version>
   <exclusions>
      <exclusion>
         <groupId>org.springframework</groupId>
         <artifactId>spring-beans</artifactId>
      </exclusion>
      <exclusion>
         <groupId>org.springframework</groupId>
         <artifactId>spring-core</artifactId>
      </exclusion>
      <exclusion>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
      </exclusion>
      <exclusion>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
      </exclusion>
      <exclusion>
         <groupId>org.springframework</groupId>
         <artifactId>spring-web</artifactId>
      </exclusion>
      <exclusion>
         <groupId>org.springframework</groupId>
         <artifactId>spring-aop</artifactId>
      </exclusion>
   </exclusions>

</dependency>


2.2配置使用Swagger2 
package com.esgcc.econtract.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {

  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
        .apis(RequestHandlerSelectors.basePackage("com.esgcc.econtract.controller"))
        .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
        .paths(PathSelectors.any()).build();
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder().title("Swagger2 build RESTful APIs on Backend")
        .description("the dynamical API Document by Swagger2")
        .termsOfServiceUrl("http://www.esgcc.com.cn/").contact("Development Team")
        .version("1.0").build();
  }

}

2.3接口类添加swagger声明

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/itest")
public class EContractController {
    **
* 测试接口
* @return 
*/
@ApiOperation(value = "test_list", notes = "test list")
@RequestMapping(value = "/test_list", method = RequestMethod.POST)
public @ResponseBody BootResponse getShippedList(HttpServletRequest request) {
BootResponse response = null;
try {
String msg=env.getProperty("privateKey");  
response = null;//orderService.saveShippedList(orderInfoDto);
} catch (Exception e) {
e.printStackTrace();
response = new BootResponse(e);
}
return response;
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值