Swagger Dropwizard 0.7 - TextArea for JSON parameter not displayed

867 views Asked by At

The problem

I can't find the reason Swagger doesn't show the POST end point with the textarea 'body' available so I can paste JSON into.

I expected to see a page like this the POST in PetStore Swagger

But my form is simply posted after I click 'Try it out' and I get

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

Response 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": ""
}

Can you help me with this please?

More information on my project

$ 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

Configuration

package myproject.promotion.app.config;

public class PromotionServiceConfiguration extends Configuration {

    @JsonProperty("swagger")
    public SwaggerBundleConfiguration swaggerBundleConfiguration;    

}

Application

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

}

End point

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

There are 2 answers

0
user3642813 On BEST ANSWER

In the end the annotation @ApiParam did the trick.

New POST method (with the new annotation)

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

Thanks for your help cyrbil.

4
Cyrbil On

You have @Api as a decorator with consumes = "application/json", and your error is about Unsupported Media Type. So your endpoint expect that you send application/json post data, your browser sends multipart/form-data or url encoded data.