使用swagger-codegen maven插件生成代码时删除默认实现

时间:2019-05-10 16:02:04

标签: java maven-plugin swagger-codegen

我必须从yaml文件生成代码,在我放进的招摇maven插件中:

<configOptions>
  <java8>true</java8>
  <sourceFolder>src/main/java</sourceFolder>
  <interfaceOnly>true</interfaceOnly>
  <dateLibrary>java8</dateLibrary>
  <singleContentTypes>true</singleContentTypes>
</configOptions>

即使它说iinterfaceOnly> true,但是代码生成也会生成具有默认实现的接口,如下所示:

@ApiOperation(value = "", nickname = "billetsFichiersHealthGet", notes = "Obtient l'état de santé de l'API. ", tags={  })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "OK"),
        @ApiResponse(code = 200, message = "Erreur", response = Error.class) })
    @RequestMapping(value = "/bills/health",
        produces = "application/json", 
        consumes = "",
        method = RequestMethod.GET)
    default ResponseEntity<Void> billetsFichiersHealthGet() {
        if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
        } else {
            log.warn("ObjectMapper or HttpServletRequest not configured in default BilletsApi interface so no example is generated");
        }
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }

如何禁用默认接口方法的生成,而仅在接口中定义而不是默认实现。

当我删除以下两个标签时,它会起作用

<java8>true</java8> 
<dateLibrary>java8</dateLibrary>

但是我的模型使用的是localdatetime,所以我应该在java8上并且不能真正删除这两个标签

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

将java8设置为false,但将dateLibrary设置为java8可以解决openapi插件版本4.1.2中的问题

<java8>false</java8> 
<dateLibrary>java8</dateLibrary>

答案 1 :(得分:0)

请尝试:

<configOptions>
    <dateLibrary>java8</dateLibrary>
    <java8>true</java8>
    <defaultInterfaces>false</defaultInterfaces>
</configOptions>

https://github.com/swagger-api/swagger-codegen/issues/8833

答案 2 :(得分:0)

如果需要LocalDateTime支持并且不需要默认方法实现,则可以使用以下技巧:

<configuration>
    ...
    <typeMappings>
        <typeMapping>date=LocalDate</typeMapping>
        <typeMapping>date-time=LocalDateTime</typeMapping>
    <typeMappings>
    <importMappings>
        <importMapping>LocalDate=java.time.LocalDate</importMapping>
        <importMapping>LocalDateTime=java.time.LocalDateTime</importMapping>
    </importMappings>
    <configOptions>
        <interfaceOnly>true</interfaceOnly>
        <dateLibrary>legacy</dateLibrary>
    </configOptions>
<configuration>

使用旧版dateLibrary排除默认方法,但是可以将日期手动映射为java8日期格式。 它适用于swagger-codegen插件3.0.18。

请注意,指定

<dateLibrary>java8</dateLibrary>
<defaultInterfaces>false</defaultInterfaces>

将避免默认实现,但会导致API中的冗余方法(如getRequest()getObjectMapper()等)。