【发布时间】:2021-12-09 19:26:47
【问题描述】:
我已经使用 openAPI 生成器 maven 插件生成了 rest api,并且我已经覆盖了 MyApiDelegate 接口的默认方法,但是 /endpoint 上的 POST 请求提供了 501 NOT IMPLEMENTED,就好像我没有给出我自己的实现一样MyApiDelegateImpl 中的那个方法。
Maven 插件配置:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>4.3.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<configOptions>
<inputSpec>${project.basedir}/src/main/resources/latest.yaml</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>my.rest.api</apiPackage>
<modelPackage>my.rest.model</modelPackage>
<supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
<delegatePattern>true</delegatePattern>
<useBeanValidation>false</useBeanValidation>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
/* code generated by plugin */
package my.rest;
public interface MyApiDelegate {
default Optional<NativeWebRequest> getRequest() {
return Optional.empty();
}
default ResponseEntity<Void> doSmth(Smth smth) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
}
package my.rest.api;
public interface MyApi {
default MyApiDelegate getDelegate() {
return new MyApiDelegate() {};
}
/*...Api operations annotations...*/
@RequestMapping(value = "/endpoint",
produces = { "application/json" },
consumes = { "application/json", "application/xml" },
method = RequestMethod.POST)
default ResponseEntity<Void> doSmth(@ApiParam(value = "" ,required=true) @RequestBody Smth smth) {
return getDelegate().doSmth(smth);
}
}
我的实现:
package my.rest.api;
@Service
@RequiredArgsConstructor
public class MyApiDelegateImpl implements MyApiDelegate {
private final MyService s;
@Override
public ResponseEntity<Void> doSmth(Smth smth) {
s.doIt(smth);
return ResponseEntity.ok().build();
}
}
如何让我的程序在具体类中使用我自己的方法实现,而不是接口中提供的默认实现?
【问题讨论】:
标签: spring openapi-generator default-method