swagger

请求地址:http://localhost:8080/server.context-path/swagger-ui.html

maven依赖:

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

swagger配置类

import org.springframework.beans.factory.annotation.Value;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 配置Swagger,用于测试rest,默认只在本地环境开启
 *
 */
@EnableSwagger2
@Configuration
public class Swagger2Config {

    /**
     * 是否开启swagger
     */
    @Value(value = "${swagger.enabled}")
    Boolean swaggerEnabled;

    /**
     * 扫描对应的包路径,生成API
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                // 是否开启
                .enable(swaggerEnabled).select()
                // 要扫描的包
                .apis(RequestHandlerSelectors.basePackage("cn.com.test.channel.controller"))
                .paths(PathSelectors.any()).build().pathMapping("/");
    }

    /**
     * 设置API 信息
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxx")
                .description("xxx")
                .version("1.0.0")
                .build();
    }
}

常用注解:

  • Api
  • ApiModel
  • ApiModelProperty
  • ApiOperation
  • ApiParam
  • ApiResponse
  • ApiResponses
  • ResponseHeader

Ex:


@Data
@ApiModel
public class UserRepay {

    @NotNull(message = "用户ID不能为空")
    @ApiModelProperty("用户ID")
    private Long userId;
}

@RestController
@Api(tags = "还款管理", description = "还款管理API")
@RequestMapping("repay")
public class UserRepayController extends BaseController {

    @Resource
    private UserRepayService userRepayService;

    @ApiOperation(value = "获取还款列表", notes = "获取还款列表 userId用户ID必填")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "pageNumber", value = "页码", dataType = "Long", paramType = "query")
    })
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public Object get(Long pageNumber, Long pageSize,
                      @RequestBody @ApiParam(value = "还款") UserRepay userRepay) {
        return ResponseUtil.getSuccessMap(userRepayService.get(pageNumber, pageSize, userRepay));
    }
}

转载于:https://my.oschina.net/u/1770537/blog/3071982

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值