Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add the json request body in swagger ui (for java code, possibly using annotations)? #1383

Closed
wakandan opened this issue Aug 20, 2015 · 24 comments

Comments

@wakandan
Copy link

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.

@webron
Copy link
Contributor

webron commented Aug 20, 2015

Which version of swagger-core do you use?

@wakandan
Copy link
Author

I'm using 1.5.3-M1

@fehguy
Copy link
Contributor

fehguy commented Aug 21, 2015

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

@wakandan
Copy link
Author

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

@fehguy
Copy link
Contributor

fehguy commented Aug 21, 2015

OK, please update your signature with an annotation:

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

I believe that'll do it.

@wakandan
Copy link
Author

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

@fehguy
Copy link
Contributor

fehguy commented Aug 21, 2015

It was the predecessor to the io.swagger project.

@wakandan
Copy link
Author

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

@fehguy
Copy link
Contributor

fehguy commented Aug 21, 2015

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.

@wakandan
Copy link
Author

👍 I will try and update this ticket later. Thanks

@fehguy
Copy link
Contributor

fehguy commented Aug 21, 2015

great, let me know @wakandan

@wakandan
Copy link
Author

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

@fehguy
Copy link
Contributor

fehguy commented Aug 21, 2015

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

@wakandan
Copy link
Author

@fehguy federecio/dropwizard-swagger#67

That pr would require this one as well (to resolve version conflicting) federecio/dropwizard-junit#7

@adcoolguy
Copy link

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

@webron
Copy link
Contributor

webron commented Nov 19, 2015

@adcoolguy - check out https://github.com/swagger-api/swagger-samples/tree/master/java/java-servlet.

@adcoolguy
Copy link

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?

@webron
Copy link
Contributor

webron commented Nov 19, 2015

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

@adcoolguy
Copy link

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

@webron
Copy link
Contributor

webron commented Nov 25, 2015

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.

@ayushdatt
Copy link

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?

@diguage
Copy link

diguage commented Sep 30, 2018

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

@KmarAkrout
Copy link

@diguage u saved my life friend ! <3

@JackLeEmmerdeur
Copy link

JackLeEmmerdeur commented Mar 18, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants