当前位置: 首页 > 知识库问答 >
问题:

Swagger codegen插件对getter生成约束

濮阳默
2023-03-14

我正在使用spring boot和swagger生成模型和委托使用swagger first方法。问题是,诸如@NotNull@size等约束被放置在getter方法上,而不是字段上。这就是为什么我可以传递随机值,例如电子邮件字段。请帮我解决这个问题。下面是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

https://maven.apache.org/xsd/maven-4.0.0.xsd“>4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.0.m4 com.niko slicer-db 0.0.1-snapshot slicer-db slicer系统数据库

<properties>
    <java.version>14</java.version>
    <swagger-annotations.version>1.6.1</swagger-annotations.version>
    <mapstruct.version>1.3.1.Final</mapstruct.version>
    <maven-processor-plugin.version>3.3.3</maven-processor-plugin.version>
    <swagger-codegen-maven-plugin.version>3.0.19</swagger-codegen-maven-plugin.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>${mapstruct.version}</version>
    </dependency>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>${mapstruct.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
    </dependency>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>${swagger-annotations.version}</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>io.swagger.codegen.v3</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>${swagger-codegen-maven-plugin.version}</version>
            <executions>
                <execution>
                    <id>server-delegate-generation</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>${project.basedir}/src/conf/swagger/slicer-db.yaml</inputSpec>
                        <language>spring</language>
                        <library>spring-boot</library>
                        <generateApiTests>false</generateApiTests>
                        <generateModelTests>false</generateModelTests>
                        <generateApiDocumentation>false</generateApiDocumentation>
                        <generateModelDocumentation>false</generateModelDocumentation>
                        <generateSupportingFiles>false</generateSupportingFiles>
                        <apiPackage>${project.groupId}.slicer.db.api</apiPackage>
                        <modelPackage>${project.groupId}.slicer.db.model</modelPackage>
                        <invokerPackage>${project.groupId}.slicer.db.invoker</invokerPackage>
                        <configOptions>
                            <java8>false</java8>
                            <dateLibrary>offsetdatetime</dateLibrary>
                            <delegatePattern>true</delegatePattern>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.bsc.maven</groupId>
            <artifactId>maven-processor-plugin</artifactId>
            <version>${maven-processor-plugin.version}</version>
            <executions>
                <execution>
                    <id>process</id>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <phase>generate-sources</phase>
                    <configuration>
                        <processors>
                            <processor>org.mapstruct.ap.MappingProcessor</processor>
                        </processors>
                        <outputDirectory>${basedir}/target/generated-sources-mappers</outputDirectory>
                        <compilerArguments>-source 14 -target 14</compilerArguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
    </pluginRepository>
</pluginRepositories>

null

下面是来自swagger文件的示例:

userPostDto:
  type: object
  required:
    - firstName
    - lastName
    - password
    - email
  properties:
    firstName:
      type: string
      maxLength: 20
    lastName:
      type: string
      maxLength: 20
    password:
      type: string
      pattern: '^[a-z0-9_-]{6,18}$'
    email:
      type: string
      pattern: '^([a-z0-9_\.-]+)@([a-z0-9_\.-]+)\.([a-z\.]{2,6})$'

共有1个答案

鲁博瀚
2023-03-14

POM有问题。应该有验证启动程序,而不是javax.validation.validation-api

 类似资料:
  • cmf_plugin_url($url, $param = [], $domain = false) 功能 生成访问插件的url 参数 $url: string url格式:插件名://控制器名/方法 $param: array 参数 $domain:boolean|array 是否显示域名 或者直接传入域名 返回 string 插件的url 例子 echo cmf_plugin_url('De

  • X1.4.0新增 sp_plugin_url($url,$param,$domain) 功能: 生成访问插件的url 参数: $url: url 格式:插件名://控制器名/方法 $param:额外参数,默认为空数组 $domain:是否添加域名,默认false 返回: 类型url 模板使用: {:sp_plugin_url('Demo://Index/index',array('id'=>2)

  • cmf_plugin_url($url, $param = [], $domain = false) 功能 生成访问插件的url 参数 $url: string url格式:插件名://控制器名/方法 $param: array 参数 $domain:boolean|array 是否显示域名 或者直接传入域名 返回 string 插件的url 例子 echo cmf_plugin_url('De

  • 嗨,我有一个包含两个库模块的gradle android项目: 所以LibB依赖于LibA。因此,LibB 的 build.gradle 文件如下所示: 如您所见,我想为gradle项目中的每个库模块生成. aar文件。到目前为止还不错,但是生成对LibA具有正确依赖关系的pom.xml文件并没有像预期的那样工作: pom。LibB的xml文件如下所示: 有没有办法在生成的pom文件中指定LibA

  • cmf_plugin_url($url, $param = [], $domain = false) 功能 生成访问插件的url 参数 $url: string url格式:插件名://控制器名/方法 $param: array 参数 $domain:boolean|array 是否显示域名 或者直接传入域名 返回 string 插件的url 例子 echo cmf_plugin_url('De