You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Swagger Generator为模型变量生成@JsonFormat注解?

How to Generate @JsonFormat Annotation for LocalDate Fields with Swagger Generator

Got it, let's fix this so your Swagger Generator outputs the @JsonFormat annotation automatically for your date fields. Here are a few reliable approaches depending on your setup:

1. Use Swagger Generator Configuration Parameters (Simplest Method)

If you're using the Maven/Gradle plugin or command-line tool, you can pass specific config options to enable Jackson date formatting annotations.

First, ensure your OpenAPI YAML definition is correct:

startDate:
  type: string
  format: date
  example: "2024-05-20" # Optional but helps with clarity

Maven Plugin Example

Add these config options to your pom.xml Swagger Codegen plugin setup:

<plugin>
  <groupId>io.swagger.codegen.v3</groupId>
  <artifactId>swagger-codegen-maven-plugin</artifactId>
  <version>3.0.46</version> <!-- Use the latest stable version -->
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>${project.basedir}/src/main/resources/openapi.yml</inputSpec>
        <language>java</language>
        <configOptions>
          <dateLibrary>java8</dateLibrary> <!-- Ensures LocalDate is used -->
          <useJacksonAnnotations>true</useJacksonAnnotations> <!-- Enables Jackson annotations -->
          <jacksonDateFormat>yyyy-MM-dd</jacksonDateFormat> <!-- Sets your desired pattern -->
        </configOptions>
      </configuration>
    </execution>
  </executions>
</plugin>

When you run the code generation, this will automatically add the @JsonFormat annotation to all date-formatted string fields, along with the @JsonProperty annotation.

2. Customize the Mustache Template (For Full Control)

If you need more flexibility (e.g., different patterns for different fields), you can modify the Swagger Generator's model template.

  1. Locate the default model.mustache template for Java (you can find it in the Swagger Codegen GitHub repo or extract it from the plugin JAR).
  2. Copy it to a local directory (e.g., src/main/resources/templates).
  3. Find the section that handles date fields and add the @JsonFormat annotation:
{{#isDate}}
@JsonProperty("{{name}}")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private {{datatype}} {{name}} = null;
{{/isDate}}
  1. Update your plugin configuration to use the custom template:
<configuration>
  <!-- Existing config -->
  <templateDirectory>${project.basedir}/src/main/resources/templates</templateDirectory>
</configuration>

3. Use OpenAPI Extension Attributes (Per-Field Control)

If you only want the annotation on specific fields, you can use OpenAPI's extension properties to add custom annotations directly in your YAML:

startDate:
  type: string
  format: date
  x-jackson-annotations:
    - "@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = \"yyyy-MM-dd\")"

Make sure your Swagger Generator version supports this extension (most v3 versions do). You'll still need to enable useJacksonAnnotations in your config for this to work.

Don't Forget the Jackson Dependency

Whichever method you use, ensure your project has the Jackson JSR310 dependency to handle Java 8 date types:

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-jsr310</artifactId>
  <version>2.15.2</version> <!-- Match your Jackson version -->
</dependency>

内容的提问来源于stack exchange,提问作者0190198

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 06:34:37