问题
I have configured my spring project using springfox 2.0. I am able to generate the open api spec with it.
"paths": {
"/test/testinfo": {
"post": {
"tags": [
"test-controller"
],
"summary": "getTestInfo",
"operationId": "getTestInfoInfoUsingGET",
"consumes": [
"application/json"
],
"produces": [
"application/json"
]
As you can see the value of operationId is of format
[java_method_name_here]Using[HTTP_verb_here]
ex. getPetsUsingGET
This operationId is used while generating clients using swagger-codegen.
Does anyone know how to customize it? I know this can be done per api using @ApiOperation
but is there a more generic way to define this format for all apis?
回答1:
You can create your own plugin to do it. Here is an example of how we do it in springfox, using the same plugin technique.
@Component
@Order(YOUR_PLUGIN_ORDER) // > Ordered.HIGHEST_PRECEDENCE + 1000
public class OperationNicknameIntoUniqueIdReader implements OperationBuilderPlugin {
@Override
public void apply(OperationContext context) {
//Create your own transformation to format the name in the way
//that you prefer
String operationNameStem = transformName(context.getName());
//Update the method name stem that is used to generate a unique id
context.operationBuilder().codegenMethodNameStem(operationNameStem);
}
...
}
Note: that whatever stem you come up with, springfox will ensure that it is unique across all the APIs. So if you had a duplicate named method, it will start the numbering scheme at the end of your unique name. e.g. if getCustomer
was not unique, it will generate a unique id getCustomer_1
etc.
回答2:
Here a complete exemple to remove the "Using"+VERB :
It's work with :
- Spring version 5.1.9.RELEASE
- Springfox 2.9.2
- Swagger version 1.5.23
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;
@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER + 1000)
public class IncludeMissingNicknameIntoUniqueIdReader implements OperationBuilderPlugin
{
@Override
public void apply(OperationContext context)
{
Optional<ApiOperation> methodAnnotation = context.findControllerAnnotation(ApiOperation.class);
Operation operationBuilder = context.operationBuilder().build();
String uniqueId = operationBuilder.getUniqueId().replaceAll("Using(GET|POST|PUT|DELETE)", "");
if (methodAnnotation.isPresent())
{
ApiOperation operation = methodAnnotation.get();
if (MiscUtils.isNotEmpty(operation.nickname()))
{
// Populate the value of nickname annotation into uniqueId
context.operationBuilder().uniqueId(operation.nickname());
context.operationBuilder().codegenMethodNameStem(operation.nickname());
}
else
{
context.operationBuilder().uniqueId(uniqueId);
context.operationBuilder().codegenMethodNameStem(uniqueId);
}
}
else
{
context.operationBuilder().uniqueId(uniqueId);
context.operationBuilder().codegenMethodNameStem(uniqueId);
}
}
@Override
public boolean supports(DocumentationType delimiter)
{
return SwaggerPluginSupport.pluginDoesApply(delimiter);
}
}
回答3:
We can also use this approach of Nickname which overrides default operationId.
@ApiOperation(value = "", nickname = "getMeAllThePetsPlease")
@RequestMapping(value = "/pets", method = RequestMethod.GET)
public Model getAllThePets() {
...
}
so we will be overriding operationId: getAllThePetsByGet with getMeAllThePetsPlease.
Note: Nickname will overrides the operationId so this can be customized accordingly.
来源:https://stackoverflow.com/questions/38821763/how-to-customize-the-value-of-operationid-generated-in-api-spec-with-swagger