Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger example post body - how to show JSON Body- Swagger-annotations

Requirement: I have a POST method which takes the input JSON as a String and passes it to another microservice. I don't want to create an Object (Bean) of this input JSON.

method:

    @ApiOperation(notes = "example" value = "/example", consumes = ".." , method= "..")
    @RequestMapping(name = "xxx" value ="/hello" ..)
    @ApiResponses(..)
        public @ResponseBody String getXXX (@Apiparam(name="JSONrequest", required = true) @RequestBody String JSONrequest){

    }

Problem: The generated Swagger doesn't show the input as a JSON model where all the JSON attributes are displayed.

Expectation: I want to display my Swagger Something like this :

enter image description here

Definately I am missing the key thing. Any thoughts?

like image 855
Sum Avatar asked Dec 31 '17 04:12

Sum


1 Answers

If changing from String to a concrete object is not okay (although that's what I would recommend you to do since it's cleaner), you can try using @ApiImplicitParams (check out their documentation)

@ApiOperation(notes = "example" value = "/example", consumes = ".." , method= "..")
@ApiImplicitParams({
        @ApiImplicitParam(name = "Object", value = "Object to be created", required = true, dataType = "your.package.BodyClass", paramType = "body")
})
@RequestMapping(name = "xxx" value ="/hello" ..)
@ApiResponses(..)
    public @ResponseBody String getXXX (@Apiparam(name="JSONrequest", required = true) @RequestBody String JSONrequest){

}

(not sure if you still need the @Apiparam(name="JSONrequest", required = true) bit from the method parameter)

like image 198
Andrei Sfat Avatar answered Oct 12 '22 04:10

Andrei Sfat