为什么这里@ApiModelProperty批注的“ example”属性中有多余的反斜杠?

时间:2019-08-19 08:54:26

标签: java spring-boot swagger swagger-maven-plugin swagger-codegen-maven-plugin

我使用swagger-maven-plugin从一种api方法源代码生成yaml:

@ApiModelProperty(value = "Сенсоры устройства", example = "{\"BATTERY\":67, \"VOLUME\":50, \"AIRPLANE\":false, \"ALARM\":true, \"CHARGE\":false, \"MUTE\":false, \"SCREEN\":true, \"WIFI\":true, \"LOCATION\": {\"latitude\":55.78409222171274,\"precision\":65.0,\"time\":\"2017-09-29T14:55:00Z\",\"detectionTechnology\":\"GPS\",\"msisdn\":\"79851620850\",\"deviceId\":\"28255923\",\"longitude\":37.62893324268332}}")

在生成的Yaml中,我有:

example: "{\"BATTERY\":67, \"VOLUME\":50, \"AIRPLANE\":false, \"ALARM\":true,\
      \ \"CHARGE\":false, \"MUTE\":false, \"SCREEN\":true, \"WIFI\":true, \"LOCATION\"\
      : {\"latitude\":55.78409222171274,\"precision\":65.0,\"time\":\"2017-09-29T14:55:00Z\"\
      ,\"detectionTechnology\":\"GPS\",\"msisdn\":\"79851620850\",\"deviceId\"\
      :\"28255923\",\"longitude\":37.62893324268332}}"

然后,我使用swagger-codegen-maven-plugin将api方法生成回Java,并得到以下信息:

@ApiModelProperty(example = "\"{\\\"BATTERY\\\":67, \\\"VOLUME\\\":50, \\\"AIRPLANE\\\":false, \\\"ALARM\\\":true, \\\"CHARGE\\\":false, \\\"MUTE\\\":false, \\\"SCREEN\\\":true, \\\"WIFI\\\":true, \\\"LOCATION\\\": {\\\"latitude\\\":55.78409222171274,\\\"precision\\\":65.0,\\\"time\\\":\\\"2017-09-29T14:55:00Z\\\",\\\"detectionTechnology\\\":\\\"GPS\\\",\\\"msisdn\\\":\\\"79851620850\\\",\\\"deviceId\\\":\\\"28255923\\\",\\\"longitude\\\":37.62893324268332}}\"", value = "Сенсоры устройства")

还有更多-在为tis api方法生成的控制器中,我得到了另一个结果:

"sensors" : \"{\\"BATTERY\\":67, \\"VOLUME\\":50, \\"AIRPLANE\\":false, \\"ALARM\\":true, \\"CHARGE\\":false, \\"MUTE\\":false, \\"SCREEN\\":true, \\"WIFI\\":true, \\"LOCATION\\": {\\"latitude\\":55.78409222171274,\\"precision\\":65.0,\\"time\\":\\"2017-09-29T14:55:00Z\\",\\"detectionTechnology\\":\\"GPS\\",\\"msisdn\\":\\"79851620850\\",\\"deviceId\\":\\"28255923\\",\\"longitude\\":37.62893324268332}}\"

代码已损坏! \\“-无效序列

1 个答案:

答案 0 :(得分:0)

反斜杠用于转义特定字符,例如''':字符串结尾或'\':特殊字符输入。