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

Swagger Annotation 1.5.14生成字符串示例未带引号问题咨询

Fix: Make Swagger 1.5.14 Generate String Example with Quotes

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:12:31