springboot配置swagger

增加请求不被拦截

,"/swagger-ui.html"
 ,"/swagger-resources/**"


方式1:


配置POM.xml

<!--swagger-->
<swagger.version>2.2.2</swagger.version>
    
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${swagger.version}</version>
</dependency>
 
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swagger.version}</version>
</dependency>

开启swagger

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
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 Swagger2Config {
    /**
     * UI页面显示信息
     */
    private final String SWAGGER2_API_BASEPACKAGE = "com.op.copyems.Controller";
    private final String SWAGGER2_API_TITLE = "ms-API";
    private final String SWAGGER2_API_DESCRIPTION = "com.op.copyems.Controller";
    private final String SWAGGER2_API_CONTACT = "0000";
    private final String SWAGGER2_API_VERSION = "1.0";
    /**
     * createRestApi
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(SWAGGER2_API_BASEPACKAGE))
                .paths(PathSelectors.any())
                .build();
    }
    /**
     * apiInfo
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(SWAGGER2_API_TITLE)
                .description(SWAGGER2_API_DESCRIPTION)
                .contact(SWAGGER2_API_CONTACT)
                .version(SWAGGER2_API_VERSION)
                .build();
    }
}



方式2:

<dependency>
    <groupId>com.spring4all</groupId>
    <artifactId>spring-boot-starter-swagger</artifactId>
    <version>${spring-boot-starter-swagger.version}</version>
</dependency>

添加yml配置

swagger:
  enabled: true
  title: oms-API
  description: API文档
  base-package:  com.op.copyems.Controller
  base-path: /**
  exclude-path: /error
  version: @project.version@

访问对应接口即可--------------------------至此已配置完毕

配置为@RequestBody  

    @RequestMapping(value="", method=RequestMethod.POST)
    public String post(@RequestBody Book book) {
        books.put(book.getId(), book);
        return "success";
    }

在实体类中添加注解

public class Book {
    @ApiModelProperty("名字")
    private String name;
    @ApiModelProperty("价格")
    private String price;
    @ApiModelProperty("关联id")
    private Long id;

访问:http://localhost:8081/swagger-ui.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值