Swagger Dropwizard 0.7 - 未显示JSON参数的TextArea

时间:2015-06-10 20:08:22

标签: java jersey jackson swagger dropwizard

问题

我找不到Swagger没有用textarea' body'来显示POST结束点的原因。可用,所以我可以将JSON粘贴到。

我希望在PetStore Swagger

中看到这样的页面

但是,点击“试一试”后,我的表单就会发布。我得到了

响应主体

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

2 个答案:

答案 0 :(得分: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。

答案 1 :(得分:0)

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