1.

Explain how you will you go about Integration of Swagger with Spring

Answer»

Swagger is a set of open-source tools that helps with creating documentation for your REST services.

<dependency>     <groupId>io.springfox</groupId>     <artifactId>springfox-swagger2</artifactId>     <version>2.4.0</version>     <scope>COMPILE</scope> </dependency> <dependency>     <groupId>io.springfox</groupId>     <artifactId>springfox-swagger-ui</artifactId>     <version>2.4.0</version>     <scope>compile</scope> </dependency

This is something you can ATTACH to your methods exposing REST endpoints.

@Api @RestController public class MyController {     @RequestMapping(method = RequestMethod.GET, path = "/welcome")     @ApiOperation(value = "Welcome<Name> !!",                     NOTES = "returns welcome “)     @ApiResponses(value = {             @ApiResponse(code = 200, message = "Success"),             @ApiResponse(code = 404, message = "Service not available"),             @ApiResponse(code = 500, message = "Unexpected Runtime error") })     public String welcome(@RequestParam(value = "destination", defaultValue = "local") String CITY) {         return "Welcome to an event @ " + city;     } }

To enable Swagger into your application , you will need the following configuration :

@Configuration @EnableSwagger2 public class SwaggerConfig {     public Docket helloApi() {         return new Docket(DocumentationType.SWAGGER_2)                 .SELECT()                 .apis(RequestHandlerSelectors.any())                 .paths(PathSelectors.ant("/welcome/*"))                 .build();     } }

Start the application and to test the application, go to the following URL in the browser :

http://localhost:8080/hello?name=John

It should print “Welcome to an Event @ Singapore”.

For checking the generated Swagger documentation, open this URL in the browser :

http://localhost:8080/swagger-ui.html.



Discussion

No Comment Found