我创建了一个简单的spring-boot-starter-web(v2.1.5)项目,该项目需要为OpenApi / Swagger生成的@RequestMapping接口实现“控制器”类
这是生成的界面的外观:-
@Validated
@Api(value = "passbook", description = "the passbook API")
public interface PassbookApi {
default Optional<NativeWebRequest> getRequest() {
return Optional.empty();
}
@ApiOperation(value = "Get passbook information for a given account number", nickname = "fetchPassbook", notes = "", response = PagedTransactionDetails.class, tags={ "passbook", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Passbook information", response = PagedTransactionDetails.class),
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 500, message = "Internal Server Error"),
@ApiResponse(code = 401, message = "Unauthorized") })
@RequestMapping(value = "/passbook",
produces = { "application/json" },
method = RequestMethod.GET)
default ResponseEntity<PagedTransactionDetails> fetchPassbook(@NotNull @ApiParam(value = "Account Number", required = true) @Valid @RequestParam(value = "accountNumber", required = true) String accountNumber,@ApiParam(value = "Offset for transactions to be displayed") @Valid @RequestParam(value = "offset", required = false) Integer offset,@ApiParam(value = "Last transaction serial number") @Valid @RequestParam(value = "transactionSerialNumber", required = false) String transactionSerialNumber,@ApiParam(value = "Last transactionId") @Valid @RequestParam(value = "transactionId", required = false) String transactionId,@ApiParam(value = "Last amount value") @Valid @RequestParam(value = "amountValue", required = false) Integer amountValue) {
getRequest().ifPresent(request -> {
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
ApiUtil.setExampleResponse(request, "application/json", "{ \"txns\" : [ { \"asdf\" : \"asdf\" }, { \"asdf\" : \"asdf\" } ]}");
break;
}
}
});
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
}
它的实现:-
@Slf4j
@Validated
@RestController
@AllArgsConstructor
class PassbookController implements PassbookApi {
@Override
public ResponseEntity<PagedTransactionDetails> fetchPassbook(@NotNull @Valid String accountNumber,
@Valid Integer offset, @Valid String transactionSerialNumber, @Valid String transactionId, @Valid Integer amountValue) {
return ResponseEntity.ok(new PagedTransactionDetails()); // !! THIS NEVER GETS INVOKED
}
}
当我删除implements PassbookApi
部分并使用@RequestMapping对该方法进行注释时,将调用请求处理程序方法
在使用implements PassbookApi
的同时如何实现它,而又不在实现类中再次使用@RequestMapping对该方法进行注释。
我正在使用MockMVC进行测试
Maven插件:-
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.0.0</version>
<executions>
<execution>
<id>spring-server</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/account-summary-api.yaml</inputSpec>
<generatorName>spring</generatorName>
<library>spring-boot</library>
<output>${project.build.directory}/generated-openapi/spring</output>
<generateApis>true</generateApis>
<addCompileSourceRoot>true</addCompileSourceRoot>
<artifactVersion>${project.version}</artifactVersion>
<groupId>com.x.summary.generated</groupId>
<artifactId>account-summary-api</artifactId>
<generateApiTests>false</generateApiTests>
<modelPackage>com.x.summary.generated.model</modelPackage>
<apiPackage>com.x.summary.generated.api</apiPackage>
<generateApiDocumentation>true</generateApiDocumentation>
<configOptions>
<dateLibrary>java8-localdatetime</dateLibrary>
<java8>true</java8>
<interfaceOnly>true</interfaceOnly>
<reactive>false</reactive>
<useBeanValidation>true</useBeanValidation>
<performBeanValidation>true</performBeanValidation>
<useOptional>false</useOptional>
<serviceInterface>true</serviceInterface>
<serviceImplementation>false</serviceImplementation>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>