【问题标题】:Swagger Dropwizard 0.7 - TextArea for JSON parameter not displayedSwagger Dropwizard 0.7 - 未显示 JSON 参数的 TextArea
【发布时间】:2015-06-10 20:08:21
【问题描述】:

问题

我找不到 Swagger 不显示带有可用 textarea 'body' 的 POST 端点的原因,因此我可以将 JSON 粘贴到其中。

我希望在PetStore Swagger 的 POST 中看到这样的页面

但我的表单只是在我点击“试用”后发布

响应正文

    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Error 415 Unsupported Media Type</title>
      </head>
      <body><h2>HTTP ERROR 415</h2>
        <p>Problem accessing /promotions. Reason:
          <pre>    Unsupported Media Type</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>
      </body>
    </html>

响应标头

{
  "access-control-allow-origin": "http://localhost:8080",
  "date": "Thu, 11 Jun 2015 07:37:15 GMT",
  "cache-control": "must-revalidate,no-cache,no-store",
  "access-control-allow-credentials": "true",
  "content-type": "text/html; charset=ISO-8859-1",
  "content-length": "320",
  "access-control-expose-headers": ""
}

你能帮我解决这个问题吗?

关于我的项目的更多信息

$ java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

Maven

parent
    - pom.xml:   <dropwizard.version>0.8.1</dropwizard.version>
                 <swagger.version>0.7.0</swagger.version>)
  app
    - pom.xml:   <dependency>
                    <groupId>io.dropwizard</groupId>
                    <artifactId>dropwizard-core</artifactId>
                    <version>${dropwizard.version}</version>
                 </dependency>
                 <dependency>
                    <groupId>io.federecio</groupId>
                    <artifactId>dropwizard-swagger</artifactId>
                    <version>${swagger.version}</version>
                 </dependency>
    - config.yml: 
                 swagger:
                   resourcePackage: myproject.promotion.v1.resource             
  representation
    - pom.xml

配置

package myproject.promotion.app.config;

public class PromotionServiceConfiguration extends Configuration {

    @JsonProperty("swagger")
    public SwaggerBundleConfiguration swaggerBundleConfiguration;    

}

应用程序

package myproject.promotion.app;

public class PromotionServiceApplication extends Application<PromotionServiceConfiguration> {

public static void main(String[] args) throws Exception {
    new PromotionServiceApplication().run(args);
}

@Override
public void initialize(Bootstrap<PromotionServiceConfiguration> bootstrap) {
    bootstrap.addBundle(new PromotionSwaggerBundle());
}

@Override
public void run(PromotionServiceConfiguration configuration, Environment environment) {
//Deleted to make it short
}

}

PromotionSwaggerBundle

package myproject.promotion.app.config;

public class PromotionSwaggerBundle extends SwaggerBundle<PromotionServiceConfiguration> {

@Override
protected SwaggerBundleConfiguration getSwaggerBundleConfiguration(PromotionServiceConfiguration configuration) {
    return configuration.swaggerBundleConfiguration;
}

}

终点

package myproject.v1.resource;

@Path("/promotions")
@Api(value = "/promotions/", description = "Promotions' possible operations", consumes = "application/json", produces = "application/json")
public class PromotionManagementResource {

    private static final String PROMO_PARAM = "promotionId";

    private final PromotionManagementService promotionManagementService;

    @Inject
    public PromotionManagementResource(PromotionManagementService promotionManagementService) {
        this.promotionManagementService = promotionManagementService;
    }

    @POST
    @Consumes(APPLICATION_JSON)
    @ApiOperation(value = "Create promotion")
    @ApiResponses(value = {
            @ApiResponse(code = 201, message = "Promotion created. Link to it in Location HEADER"),
            @ApiResponse(code = 409, message = "Promotion already exists")
            })
    public Response create(final Promotion promotion, @Context final UriInfo uriInfo) throws IOException {
        Promotion createdPromotion = promotionManagementService.create(promotion);

        URI createdInventoryURI = inventory(createdPromotion, uriInfo);
        return Response.created(createdInventoryURI).build();
    }

【问题讨论】:

    标签: java jersey jackson swagger dropwizard


    【解决方案1】:

    最后,@ApiParam 注释成功了。

    新的 POST 方法(带有新注释)

    @POST
    @Consumes(APPLICATION_JSON)
    @ApiOperation(value = "Create promotion", notes = "", response = Promotion.class)
    @ApiResponses(value = {
            @ApiResponse(code = 201, message = "Promotion created. Link to it in Location HEADER."),
            @ApiResponse(code = 409, message = "Promotion already exist.")
    })
    public Response create(@ApiParam final Promotion promotion, @Context final UriInfo uriInfo) throws IOException {
        Promotion createdPromotion = promotionManagementService.create(promotion);
    
        URI createdPromotionURI = uriTo(createdPromotion, uriInfo);
        return Response.created(createdPromotionURI).build();
    }
    

    感谢您的帮助 cyrbil。

    【讨论】:

      【解决方案2】:

      你有@Api作为consumes = "application/json"的装饰器,你的错误大约是Unsupported Media Type。 因此,您的端点希望您发送 application/json 发布数据,您的浏览器发送 multipart/form-data 或 url 编码数据。

      【讨论】:

      • 我尝试了以下组合,但仍然得到 415 @Api(value = "/promotions/", description = "Promotions' possible operations", consumes = "multipart/form-data", produces = "multipart/form-data")@Api(value = "/promotions/", description = "Promotions' possible operations")
      • 您从错误的部分获取它,您的浏览器需要将其作为 application/json 发送。您可以使用 ajax (and jquery for easier use) 或插件 (1,2,3...)
      • Swagger 部署在“/swagger”上下文中,该上下文有一个加载自己的 UI 的脚本。这个 UI 会自动从我自己的环境中加载生成的 'swagger.json' 并呈现可用的操作。我使用了您提到的插件,并且该应用程序按预期工作。但是“Swagger UI”无法识别 POST,这从 0.7.x 版本开始发生。感谢您迄今为止的帮助。
      • ApiOperation 装饰器中,您可以指定httpMethod。详情请见doc here