Swagger Dropwizard 0.7 - JSON 参数的文本区域未显示

Swagger Dropwizard 0.7 - TextArea for JSON parameter not displayed

问题

我找不到 Swagger 不显示带有文本区域 'body' 的 POST 端点的原因,因此我可以将 JSON 粘贴到。

我希望在 PetStore Swagger

中看到 POST 这样的页面

但我的表格只是在我点击 'Try it out' 后发布,我得到

回应Body

    <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>

回应Headers

{
  "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();
    }

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

最后注释 @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 的帮助。