这不需要额外的依赖,也很容易自定义命名。(这里的改进)
app/api/src/main/java/com/observatory/api/config/SwaggerIncludeMissingNicknameIntoUniqueIdReader.java
package com.observatory.api.config;
import com.google.common.base.Optional;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import springfox.documentation.service.Operation;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;
import java.util.Locale;
@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER + 1000)
public class SwaggerIncludeMissingNicknameIntoUniqueIdReader implements OperationBuilderPlugin {
@Override
public void apply(OperationContext context) {
Optional<ApiOperation> methodAnnotation = context.findControllerAnnotation(ApiOperation.class);
Operation operationBuilder = context.operationBuilder().build();
String uniqueId = operationBuilder.getUniqueId();
if(operationBuilder.getTags().stream().findFirst().get().isEmpty())
throw new RuntimeException("operationBuilder.getTags().stream().findFirst()");
uniqueId = uniqueId.substring(0,1).toUpperCase(Locale.ROOT)+ uniqueId.substring(1);
uniqueId = uniqueId.replaceAll("[_].+","");
String tag = operationBuilder.getTags().stream().findFirst().get();
tag = tag.replace("-controller","s");
int index = tag.indexOf("-");
while (index >= 0) {
tag = tag.substring(0,index) + tag.substring(index+1,index+2).toUpperCase(Locale.ROOT)+ tag.substring(index+2);
index = tag.indexOf("-");
}
uniqueId = tag + uniqueId;
// If nickname exists, populate the value of nickname annotation into uniqueId
String fillId = methodAnnotation.transform(ApiOperation::nickname).or(uniqueId);
context.operationBuilder().uniqueId(fillId);
context.operationBuilder().codegenMethodNameStem(fillId);
}
@Override
public boolean supports(DocumentationType delimiter) {
return SwaggerPluginSupport.pluginDoesApply(delimiter);
}
}