Swagger-Codegen:如何将JSON对象存入字符串字段
To get your field3 to serialize arbitrary JSON structures into a Java String when generating code from your Swagger/OpenAPI YAML, you'll need to adjust your definition to explicitly mark it as a string and provide clear hints to the code generator. Here's how to do it properly:
Corrected OpenAPI YAML Definition
SomeStructure: type: object required: - field1 - field2 properties: field1: type: string field2: type: string field3: type: string description: | Stores an arbitrary JSON structure serialized as a string. Accepts any valid JSON value (object, array, string, number, boolean, null). example: '{"subvalue1":"subvalue1", "subvalue2": 456, "subvalue3": true}' # Vendor extension to enforce Java String type in generated code x-codegen-type: java.lang.String
Key Changes Explained
- Top-level
requiredarray: This is the standard OpenAPI way to list mandatory fields (replacing inlinerequired: truefor cleaner structure, though both are valid). field3astype: string: This tells the code generator to create aStringfield in your Java class, which aligns with your goal of storing JSON as a string.- Clear description & example: The example shows how a JSON object would be represented as a string (using single quotes to escape double quotes in YAML), making expectations clear for both developers and tools.
x-codegen-typeextension: This vendor-specific hint ensures OpenAPI Generator explicitly usesjava.lang.Stringforfield3, preventing accidental auto-mapping toMapor custom objects if the tool tries to infer type from the example.
Handling Serialization/Deserialization
When your API receives a request where field3 is a JSON object (like your example), you need to ensure the framework converts that object to its string representation. If you're using Jackson (the default for Spring Boot and most Java REST frameworks), you can add a custom deserializer:
First, create the deserializer class:
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonNode; import java.io.IOException; public class JsonToStringDeserializer extends JsonDeserializer<String> { @Override public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { JsonNode node = p.getCodec().readTree(p); return node.toString(); } }
Then annotate the field3 in your generated Java class:
@JsonDeserialize(using = JsonToStringDeserializer.class) private String field3;
This deserializer will take any incoming JSON value (object, array, etc.) and convert it to its stringified JSON representation, exactly as you need.
To avoid modifying generated code manually, you can add an extension to your YAML to auto-include the annotation:
field3: # ... existing properties ... x-jackson-deserializer: com.yourpackage.JsonToStringDeserializer
Just ensure the deserializer class exists in your project before generating the code.
内容的提问来源于stack exchange,提问作者JS Bournival

