Swagger Annotation 1.5.14生成字符串示例未带引号问题咨询
I've run into this exact issue with Swagger 1.5.x before—when your example value looks like a number, Swagger automatically infers it as a numeric type, even if your Java field is a String. Here are two reliable fixes to get the quoted string example you want:
1. Explicitly Specify the Data Type
The most clean and maintainable fix is to use the dataType attribute in @ApiModelProperty to force Swagger to treat the field as a string. This overrides its automatic type inference:
@ApiModelProperty(example = "484799", dataType = "string") private String accountNumber;
This will make the generated Swagger output show:
"accountNumber": "484799"
2. Escape Quotes in the Example Value
If for some reason you can't use dataType, you can wrap the example value in escaped double quotes. This tells Swagger to treat the entire value as a string literal:
@ApiModelProperty(example = "\"484799\"") private String accountNumber;
Why This Happens
Swagger 1.5.x uses a loose type inference system for examples. When it sees a value like "484799", it assumes it's a number and drops the quotes in the generated output. Explicitly setting dataType bypasses this behavior and aligns the Swagger schema with your actual Java type.
Note: If you ever upgrade to a newer Swagger version (like 2.x+ via Springfox), this issue is usually resolved out of the box, as newer versions rely more on Java's type information rather than example value inference.
内容的提问来源于stack exchange,提问作者edison ouyang

