gpt4 book ai didi

java - Swagger codegen 插件生成对 getter 的约束

转载 作者:行者123 更新时间:2023-12-01 17:24:11 24 4
gpt4 key购买 nike

我正在使用 spring boot 和 swagger 使用 swagger 第一种方法生成模型和代表。问题在于诸如@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-父级 2.3.0.M4 尼科网 切片器数据库 0.0.1-快照 切片器数据库 切片系统数据库

<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>

这是 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})$'

生成的java类示例: Generated java code

最佳答案

pom有问题。应该有验证启动器,而不是 javax.validation.validation-api

关于java - Swagger codegen 插件生成对 getter 的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61235649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com