Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何使用swagger自定义api规范中生成的operationId的值?_Spring_Swagger_Springfox - Fatal编程技术网

Spring 如何使用swagger自定义api规范中生成的operationId的值?

Spring 如何使用swagger自定义api规范中生成的operationId的值?,spring,swagger,springfox,Spring,Swagger,Springfox,我已经使用springfox 2.0配置了我的spring项目。我能够用它生成OpenAPI规范 "paths": { "/test/testinfo": { "post": { "tags": [ "test-controller" ], "summary": "getTestInfo", "operationId": "getTestInfoInfoUsingGET",

我已经使用springfox 2.0配置了我的spring项目。我能够用它生成OpenAPI规范

 "paths": {
    "/test/testinfo": {
      "post": {
        "tags": [
          "test-controller"
        ],
        "summary": "getTestInfo",
        "operationId": "getTestInfoInfoUsingGET",
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ]
正如您所看到的,operationId的值是格式

[java_method_name_here]Using[HTTP_verb_here]
例如:getPetsUsingGET

使用swagger codegen生成客户端时使用此operationId。 有人知道如何定制吗?我知道每个api都可以使用
@ApiOperation
来实现这一点,但是有没有更通用的方法来为所有api定义这种格式呢?

您可以这样做。下面是我们如何在springfox中使用相同的插件技术来实现这一点

@组件
@顺序(您的插件顺序)/>已排序。最高优先级+1000
公共类Operation昵称为UniquedReader实现OperationBuilderPlugin{
@凌驾
公共无效应用(OperationContext上下文){
//创建您自己的转换以按以下方式格式化名称
//你喜欢什么
字符串操作名称项=transformName(context.getName());
//更新用于生成唯一id的方法名称stem
context.operationBuilder().codeGenMethodNameItem(operationNameItem);
}
...
}

注意:无论您提出什么样的stem,springfox都将确保它在所有API中都是唯一的。因此,如果您有一个重复的命名方法,它将在您唯一名称的末尾开始编号方案。e、 g.如果
getCustomer
不是唯一的,它将生成一个唯一的id
getCustomer\u 1
等。

这里有一个删除“使用”动词的完整示例:

它的工作是:

  • Spring版本5.1.9.1发布
  • Springfox 2.9.2
  • 招摇过市版本1.5.23
import com.google.common.base.Optional;
导入io.swagger.annotations.api操作;
导入org.springframework.core.annotation.Order;
导入org.springframework.stereotype.Component;
导入springfox.documentation.service.Operation;
导入springfox.documentation.spi.DocumentationType;
导入springfox.documentation.spi.service.OperationBuilderPlugin;
导入springfox.documentation.spi.service.contexts.OperationContext;
导入springfox.documentation.swagger.common.SwaggerPluginSupport;
@组成部分
@订单(SwaggerPluginSupport.SWAGGER\u PLUGIN\u订单+1000)
公共类IncludeMissing昵称为UniquedReader实现OperationBuilderPlugin
{
@凌驾
公共无效应用(OperationContext上下文)
{
可选的methodAnnotation=context.findControllerAnnotation(apioction.class);
Operation operationBuilder=context.operationBuilder().build();
字符串uniqueId=operationBuilder.getUniqueId().replaceAll(“使用(GET | POST | PUT | DELETE)”,“”);
if(methodAnnotation.isPresent())
{
ApiOperation operation=methodAnnotation.get();
if(MiscUtils.isNotEmpty(operation.昵称()))
{
//将昵称注释的值填充到uniqueId中
context.operationBuilder().uniqueId(operation.昵称());
context.operationBuilder().codeGenMethodNameItem(operation.昵称());
}
其他的
{
context.operationBuilder().uniqueId(uniqueId);
context.operationBuilder().codeGenMethodNameItem(uniqueId);
}
}
其他的
{
context.operationBuilder().uniqueId(uniqueId);
context.operationBuilder().codeGenMethodNameItem(uniqueId);
}
}
@凌驾
公共布尔支持(DocumentationType分隔符)
{
返回SwaggerPluginSupport.pluginDoesApply(分隔符);
}
}

我们也可以使用这种昵称方法,它覆盖默认操作ID

@ApiOperation(value = "", nickname = "getMeAllThePetsPlease")
@RequestMapping(value = "/pets", method = RequestMethod.GET)
public Model getAllThePets() {
    ...
}
因此,我们将覆盖操作ID:getAllThePetsByGet,使用GetMeallThepestPlease


注意:昵称将覆盖operationId,因此可以相应地进行自定义。

基于@bdzzaid的代码版本更短更清晰:

@组件
@订单(SwaggerPluginSupport.SWAGGER\u PLUGIN\u订单+1000)
公共类swagger包括分配昵称到UniquedReader实现OperationBuilderPluggin{
@凌驾
公共无效应用(OperationContext上下文){
可选的methodAnnotation=context.findControllerAnnotation(apioction.class);
Operation operationBuilder=context.operationBuilder().build();
字符串uniqueId=operationBuilder.getUniqueId().replaceAll(“使用(GET | POST | PUT | DELETE)”,“”);
//如果存在昵称,则将昵称注释的值填充到uniqueId中
字符串fillId=methodAnnotation.transform(ApiOperation::昵称).or(uniqueId);
context.operationBuilder().uniqueId(fillId);
context.operationBuilder().codeGenMethodNameItem(fillId);
}
@凌驾
公共布尔支持(DocumentationType分隔符){
返回SwaggerPluginSupport.pluginDoesApply(分隔符);
}
}

这不需要额外的依赖项,而且很容易自定义命名。(改进)

app/api/src/main/java/com/observer/api/config/swaggerincludemissing昵称为uniqueidReader.java


包com.observation.api.config;
导入com.google.common.base.Optional;
导入io.swagger.annotations.api操作;
导入org.springframework.core.annotation.Order;
导入org.springframework.stereotype.Component;
导入springfox.documentation.service.Operation;
导入springfox.documentation.spi.DocumentationType;
导入springfox.documentation.spi.service.OperationBuilderPlugin;
导入springfox.documentation.spi.service.contexts.OperationContext;
导入springfox.documentation.swagger.common.SwaggerPluginSupport;
导入java.util.Locale;
@组成部分
@订单(SwaggerPluginSupport.SWAGGER\u PLUGIN\u订单+1000)
公共类swagger包括分配昵称到UniquedReader实现OperationBuilderPluggin{
@凌驾
公共无效应用(OperationContext上下文){