私が構築しようとしているのは、スプリング ブート (v1.2.3) アプリケーションであり、Rest API を SpringFox(swagger2) v2.0.0 で公開します。
私のSwagger Spring構成
@EnableSwagger2
@Configuration
public class SwaggerConfig {
@Bean
public Docket myApi() {
return new Docket(DocumentationType.SWAGGER_2)
.genericModelSubstitutes(DeferredResult.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(false)
.pathMapping("/my-prj");
}
}
pojo を json に変換するには gson を使用する必要があり、次のようにします。
@Configuration
public class GsonHttpMessageConverterConfig {
@Bean
public GsonHttpMessageConverter gsonHttpMessageConverter(Gson gson) {
GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
converter.setGson(gson);
return converter;
}
}
問題は、 を使用するGsonHttpMessageConverter
と、swagger v2 が間違った json を生成することです。
{
"value": "{\"swagger\":\"2.0\",\"info\":{\"description\":\"Api Documentation\",\"version\":\"1.0\",\"title\":\"Api Documentation\",\"termsOfService\":\"urn:tos\",\"contact\":{\"name\":\"Contact Email\"},\"license\":{\"name\":\"Apache 2.0\",\"url\":\"http:
...
JSON には値のプレフィックスが付けられ、実際の JSON はエスケープされた文字列になります。
を使用しない場合は、次のようになりGsonHttpMessageConverter
ます。
{
"swagger": "2.0",
"info": {
"description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a
...
値とエスケープなしで正しいswagger JSONを作成するソリューションはありますか?