{"id":23170,"date":"2015-07-17T22:35:19","date_gmt":"2015-07-17T17:05:19","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=23170"},"modified":"2015-07-23T22:32:46","modified_gmt":"2015-07-23T17:02:46","slug":"integrate-swaggerui-with-spring-boot-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integrate-swaggerui-with-spring-boot-application\/","title":{"rendered":"Integrate SwaggerUI with spring boot application"},"content":{"rendered":"<p>Hi Guys,<\/p>\n<p>In this artical we learn how to integrate SwaggerUI with spring boot application.<\/p>\n<p>Basically &#8216;SwaggerUI&#8217; is used to serves documentation of an REST API and another most powerfull feature is, it provides a way to enable developer to play around the REST API without having write any code.<\/p>\n<p><strong>Steps to Integrate SwaggerUI<\/strong> :-<\/p>\n<p>1. First of all we need to add swagger dependency in our spring boot application.<\/p>\n<p>if you are using gradle as build tool then you need to add following dependency in your build file<\/p>\n<pre>'com.mangofactory:swagger-springmvc:1.0.2'\r\n<\/pre>\n<p>and for maven project you need to add following dependency in our build xml file:-<\/p>\n<pre>\r\n&lt;dependency&gt;\r\n  &lt;groupId&gt;com.mangofactory&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;swagger-springmvc&lt;\/artifactId&gt;\r\n  &lt;version&gt;1.0.2&amp;lt\/version&gt;\r\n&lt;dependency&gt;\r\n<\/pre>\n<p>2. Then we need to add a java class that enables the swagger into action.<\/p>\n<p>[code]<br \/>\npackage com.intelligrape.com.config;<\/p>\n<p>import com.mangofactory.swagger.configuration.SpringSwaggerConfig;<br \/>\nimport com.mangofactory.swagger.models.dto.ApiInfo;<br \/>\nimport com.mangofactory.swagger.plugin.EnableSwagger;<br \/>\nimport com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;<br \/>\nimport org.springframework.beans.factory.annotation.Autowired;<br \/>\nimport org.springframework.boot.autoconfigure.EnableAutoConfiguration;<br \/>\nimport org.springframework.context.annotation.Bean;<br \/>\nimport org.springframework.context.annotation.Configuration;<\/p>\n<p>@Configuration<br \/>\n@EnableSwagger<br \/>\n@EnableAutoConfiguration<br \/>\npublic class SwaggerConfig {<\/p>\n<p>    private SpringSwaggerConfig springSwaggerConfig;<\/p>\n<p>    @Autowired<br \/>\n    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {<br \/>\n        this.springSwaggerConfig = springSwaggerConfig;<br \/>\n    }<\/p>\n<p>    @Bean<br \/>\n    public SwaggerSpringMvcPlugin customImplementation() {<br \/>\n        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)<br \/>\n                .apiInfo(new ApiInfo(<br \/>\n                        &quot;SWAGGER DEMO WITH SPRING BOOT REST API&quot;,<br \/>\n                        &quot;This API is for swagger demo person analysis.&quot;,<br \/>\n                        null,<br \/>\n                        null,<br \/>\n                        null,<br \/>\n                        null<br \/>\n                ))<br \/>\n                        \/\/Here we disable auto generating of responses for REST-endpoints<br \/>\n                .useDefaultResponseMessages(false)<br \/>\n                        \/\/Here we specify URI patterns which will be included in Swagger docs. Use regex for this purpose.<br \/>\n                .includePatterns(&quot;\/swagger-demo\/.*&quot;);<br \/>\n    }<\/p>\n<p>}<br \/>\n[\/code]<\/p>\n<p><strong>Note<\/strong>: you have to add swagger configuration class in the application classpath so that it will be scan by compiler at run time.<\/p>\n<p>3. After the configuration file is done we can jump on to Controllers.<\/p>\n<p>[code]<br \/>\npackage com.intelligrape.com.person<\/p>\n<p>import com.wordnik.swagger.annotations.Api<br \/>\nimport com.wordnik.swagger.annotations.ApiOperation<br \/>\nimport com.wordnik.swagger.annotations.ApiResponse<br \/>\nimport com.wordnik.swagger.annotations.ApiResponses<br \/>\nimport org.springframework.http.HttpStatus<br \/>\nimport org.springframework.http.MediaType<br \/>\nimport org.springframework.http.ResponseEntity<br \/>\nimport org.springframework.web.bind.annotation.*<\/p>\n<p>\/**<br \/>\n * Created by Ashu on 2\/7\/15.<br \/>\n *\/<\/p>\n<p>@Api(basePath = &quot;swagger-demo\/person&quot;, value = &quot;Person&quot;, description = &quot;Operations with person&quot;, produces = &quot;application\/json&quot;)<br \/>\n@RestController<br \/>\n@RequestMapping(value = &quot;swagger-demo\/person&quot;, produces = MediaType.APPLICATION_JSON_VALUE)<br \/>\nclass PersonController {<\/p>\n<p>    @RequestMapping(method = RequestMethod.POST, value = &quot;\/create&quot;)<br \/>\n    @ResponseStatus(HttpStatus.NOT_FOUND)<br \/>\n    @ApiOperation(value = &quot;Get Person&quot;, notes = &quot;Fetch List of Person&quot;)<br \/>\n    @ApiResponses(value = [<br \/>\n            @ApiResponse(code = 400, message = &quot;Fields are with validation errors&quot;),<br \/>\n            @ApiResponse(code = 201, message = &quot;List&quot;),<br \/>\n            @ApiResponse(code = 500, message = &quot;Error occurred while fetching Person&quot;)<br \/>\n    ])<br \/>\n    ResponseEntity create(@RequestBody PersonCO co) {<br \/>\n        return new ResponseEntity(new Person(firstName: co.firstName, lastName: co.lastName, age: co.age), HttpStatus.OK)<br \/>\n    }<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>4. Lastly we need to add swagger UI plug. you can Download swagger ui from its <a href=\"https:\/\/github.com\/swagger-api\/swagger-ui\/\">official git repoAfter<\/a> that extract it and copy dist directory and paste it in folder \/public or \/static or \/resources located in src\/java\/resources. Now start the application and navigate to<\/p>\n<p>http:\/\/localhost:8080\/swagger\/index.html<\/p>\n<p>You have in screen something like this :-<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/07\/rsz_swagger.png\" alt=\"rsz_swagger\" width=\"623\" height=\"235\" class=\"alignnone size-full wp-image-23178\" \/><\/p>\n<p>Hope this helps \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi Guys, In this artical we learn how to integrate SwaggerUI with spring boot application. Basically &#8216;SwaggerUI&#8217; is used to serves documentation of an REST API and another most powerfull feature is, it provides a way to enable developer to play around the REST API without having write any code. Steps to Integrate SwaggerUI :- [&hellip;]<\/p>\n","protected":false},"author":92,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6},"categories":[1],"tags":[1202,1992,1990,1991],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23170"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/92"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=23170"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23170\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=23170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=23170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=23170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}