Springfox (Swagger) - ApiOperation 注解中的 position 方法无效的拓展解决方案

本文探讨了在Springfox Swagger中使用@ApiOperation注解的position属性无效的问题,由于该属性已过时,作者提供了临时的拓展解决方案,通过创建拓展类并覆盖默认bean来实现API的自定义排序。详细步骤包括添加新类、支持排序以及指定排序规则。同时,对于高版本中前端UI缺失排序功能的情况,作者建议使用swagger-bootstrap-ui或knife4j等替代方案。
摘要由CSDN通过智能技术生成

写在前面

未经允许不得转载!

如果你有现在使用 Springfox 的经历的话,可能就有对 api 进行分组以及排序等需求,而Springfox(应当说是Swagger更确切)默认是根据自然排序对 api 进行排序的,比如:"/aaa1"会排在"/aaa2"前面,会排在"/bbb1"前面。

当需要自定义排序规则时怎么办?

  • 那就是使用ApiOperation注解的position方法,但是很不幸的是该方法已经标记为过时了,现在保留这个方法只是为了兼容之前的版本,且高版本的已经一移除该功能了(包括后端和前端UI)。

那有其他办法实现自定义排序吗?

  • 自己花时间找文档及api看看有没有类似功能
  • 按照我的配置方式实现临时用的拓展解决方案(当然只是临时的 毕竟position已经过时了,之后的版本可能就移除了)

相关链接

临时用的拓展解决方案

第1步:在需要的位置添加 position

/*
 *  Copyright 2019 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Yahuan Jin
 * @since 2019.08.24
 */
@Api(tags = "test", description = "test")
@RequestMapping("test/apiOperation")
@RestController
public class ApiOperationTestController {
   
    @ApiOperation(value = "Test position 2", tags = {
   "position-test"}, position = 2)
    @PostMapping(value = "aaa")
    public String testPositionAaa() {
   
        return "aaa";
    }

    @ApiOperation(value = "Test position 1", tags = {
   "position-test"}, position = 1)
    @RequestMapping(value = "bbb")
    public String testPositionBbb() {
   
        return "bbb";
    }
}

第2步:完成对 position的支持

拓展springfox.documentation.spring.web.readers.operation.ApiOperationReade类。

/*
 *  Copyright 2015-2019 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import com.google.common.base.Optional;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.OperationNameGenerator;
import springfox.documentation.builders.OperationBuilder;
import springfox.documentation.service.Operation;
import springfox.documentation.spi.service.contexts.OperationContext;
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值