Swagger-core: How to add the json request body in swagger ui (for java code, possibly using annotations)?

Created on 20 Aug 2015  路  24Comments  路  Source: swagger-api/swagger-core

I'm writing a dropwizard app. I want to have something similar to this
result

but what I'm having is only this (it doesn't have the json request body text form)
rightnow

my annotations are like these

    @POST
    @UnitOfWork
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Create a new shop", response = long.class, consumes="application/json")
    @ApiResponses(value = {
            @ApiResponse(code = 406, message = R.ERROR_SHOP_NAME_EXISTED),
            @ApiResponse(code = 404, message = R.ERROR_USER_NOT_FOUND),
            @ApiResponse(code = 201, message = "shop id") })
    public Response create(Shop shop) {

How do I make it work like the first picture? Thanks.

Most helpful comment

I had the same problem. I added the annotation @RequestBody Shop shop, then the problem was resolved.
Good luck!

All 24 comments

Which version of swagger-core do you use?

I'm using 1.5.3-M1

Hi if you can, please update to 1.5.3 (yes, no M1 suffix).

Hmm I was using swagger-core from com.wordnik, there was no 1.5.3 version. Changed to io.swagger:swagger-core:1.5.3 but it's still the same

OK, please update your signature with an annotation:

public Response create(@ApiParam("description") Shop shop)

I believe that'll do it.

is com.wordnik a fork from io.swagger? It's was in the first result that's why I added it in

It was the predecessor to the io.swagger project.

I'm using dropwizard-swagger package to work with dropwizard. This package seems to be using swagger-core:1.5.1-M2. I added io.swagger:swagger-core:1.5.3 it doesn't work. Seems like they can't work together & dropwizard-swagger is the culprit here...

Got it. Then you can keep the current dependency and sumply add the @ApiParam annotation and all should be fine. There is probably an update to dropwizard swagger, or you can follow the sample in the swagger-samples project.

:+1: I will try and update this ticket later. Thanks

great, let me know @wakandan

@fehguy I confirm that in 1.5.3 the form for post body shows up with the use of @ApiParam. I'm creating pull-requests on the dropwizard-swagger side. Thanks for your help!

great news, thanks for following up! And yes, perhaps you can reference the PR here for others

@fehguy https://github.com/federecio/dropwizard-swagger/pull/67

That pr would require this one as well (to resolve version conflicting) https://github.com/federecio/dropwizard-junit/pull/7

Slightly off topic, but how would one annotate the method for a purely Servlet project (for input which is a JSON body/model)? Thanks!

We see only paramType = "query" and GET. Is there any POST sample? Is this https://github.com/swagger-api/swagger-samples/blob/master/java/java-servlet/src/main/java/io/swagger/sample/servlet/SampleServlet.java the right sample?

Just change those to the values you need. You can use any of the HTTP verbs supported by the spec, same goes for paramType.

Could you give some example? We don't quite get it and would not like to guess. Thanks!

Sure, you can use something along the lines of @ApiOperation(httpMethod = "POST", value = "Create a new user", nickname = "addtUser"). If you're looking for something more specific, please elaborate.

If I use @ApiOperation over a servlet doPost Method then the generated JSON file also attaches doPost in the end. Expected URL: "/v1/document/doc1/" Generated JSON file has URL "/v2/document/us_1098/doPost". Why is it attaching function name in the end?

I had the same problem. I added the annotation @RequestBody Shop shop, then the problem was resolved.
Good luck!

@diguage u saved my life friend ! <3

I had to do this to add a description to the response-body for OpenAPI Specification 3:

@PostMapping(value="/login", produces={"application/json"})
@Operation(summary = "Login", description = "Login to service")
public ResponseEntity<JWTTokenResponseLight> login(
    @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Use either the email- or username-field")
    @Parameter(required=true)
    @RequestBody
    ServiceCredentials user
) throws Exception {
    return authenticationService.login(user);
}
Was this page helpful?
0 / 5 - 0 ratings