Skip to content

Instantly share code, notes, and snippets.

@codinko
Created March 1, 2016 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codinko/20efa47944de9b1fd262 to your computer and use it in GitHub Desktop.
Save codinko/20efa47944de9b1fd262 to your computer and use it in GitHub Desktop.
Spring REST API - Integrating Swagger
/* Mentioning all the swagger changes made on top of
Spring REST app: https://gist.github.com/codinko/79986dbed38d0f2b2d1b */
---------------------------------------
Modified File: pom.xml
---------------------------------------
<properties>
<swagger.version>1.0.2</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
---------------------------------------
Modified File: spring-context.xml [Reference: https:// github.com/martypitt/ swagger-springmvc]
---------------------------------------
<!-- to enable the default documentation controller-->
<context:component-scan base-package="com.mangofactory.swagger.controllers"/>
<!-- to pick up the bundled spring configuration-->
<context:component-scan base-package="com.mangofactory.swagger.configuration"/>
<!-- Direct static mappings -->
<mvc:resources mapping="*.html" location="/, classpath:/swagger-ui"/>
<!-- Serve static content-->
<mvc:default-servlet-handler/>
---------------------------------------
New File added Refer: SwaggerConfiguration.java
---------------------------------------
---------------------------------------
Modified File: EmployeeController.java
---------------------------------------
@Configuration
@PropertySource("classpath:messages.properties")
@Api(value = "EmployeeController", description = "EmployeeController")
@RestController
@RequestMapping(value = "/employee")
public class EmployeeController {
.....
@ApiOperation(value = "Get all employees")
@RequestMapping(value = "/all", method = RequestMethod.GET)
public APIContract<List<Employee>> getEmployees() {
..... //Swagger will detect @RequestMapping automatically. @ApiOperation is only for more documentation.
---------------------------------------------
Now if you start the server and go to http://localhost:8080/springrestapi/api-docs ,
you should see the below result
{
"apiVersion": "1.0",
"apis": [
{
"description": "EmployeeController",
"path": "/springrestapi/employeecontroller",
"position": 0
}
],
"authorizations": {},
"info": {
"contact": "contact@localhost.com",
"description": "API for my app",
"license": "License type",
"licenseUrl": "something like a License URL",
"termsOfServiceUrl": "",
"title": "Springrest API"
},
"swaggerVersion": "1.2"
}
------------
Now to see the actual Swagger UI with the GET/POST/PUT method level mappings,
and the URL's, we need to download the SwaggerUI which is available here:
https://github.com/swagger-api/swagger-ui
and copy the 'dist' directory and rename to 'swagger-ui' and place it under src\main\webapp
Navigate to index.html that was inside dist, and change the url to your api-docs URL.
window.swaggerUi = new SwaggerUi({
url: "/springrestapi/api-docs", // change needed only in this line.
dom_id: "swagger-ui-container",
supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
-----------------------------------------------------------------------------
THATS IT!!! Now access the SwaggerUI through http://localhost:8080/springrestapi/swagger-ui/index.html
package com.codinko.springrestapi.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
@EnableSwagger
@Configuration
public class SwaggerConfiguration {
private SpringSwaggerConfig swaggerConfig;
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig swaggerConfig) {
this.swaggerConfig = swaggerConfig;
}
@Bean
// Don't forget the @Bean annotation
public SwaggerSpringMvcPlugin customImplementation() {
return new SwaggerSpringMvcPlugin(this.swaggerConfig)
.apiInfo(apiInfo())
.includePatterns("/employee/.*")
.swaggerGroup("springrestapi")
.build();
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("Springrest API", "API for my app", "", "contact@localhost.com", "License type",
"something like a License URL");
return apiInfo;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment