DOCUMENTACION DE API'S CON SWAGGER2
Para Agregar Swagger a un proyecto Spring se debe seguir los siguientes pasos:
1. Agregar la dependencia Swagger en el pom.xml
2. Definir parametros en el application.yml
3. Crear una Clase Swagger2Config, con el siguiente codigo
4. Finalmente agregar la anotacion @Api en tus Clases Controller
5. Para ver la documentacion ejecutar el proyecto y escribir loclahost:8080/swagger-ui.html

<properties>
<springfox.version>2.8.0</springfox.version>
</properties>
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
swagger:
api:
title: "Spring Boot
REST API"
description: "Clientes REST
API"
license: "Apache
2.0"
termsOfServiceUrl: "http://www.apache.org/licenses/LICENSE-2.0.html"
version: 1.0.0
controller:
basepackage: com.besoft.siserp.pos.controller
contact:
nombres : "Nerio
Baez"
web : "http://besoft-ti.blogspot.com"
email: "nbaez001@gmail.com"
3. Crear una Clase Swagger2Config, con el siguiente codigo
package
com.besoft.siserp.pos.config;
import
org.springframework.beans.factory.annotation.Value;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration;
import
springfox.documentation.builders.ApiInfoBuilder;
import
springfox.documentation.builders.PathSelectors;
import
springfox.documentation.builders.RequestHandlerSelectors;
import
springfox.documentation.service.ApiInfo;
import
springfox.documentation.service.Contact;
import
springfox.documentation.spi.DocumentationType;
import
springfox.documentation.spring.web.plugins.Docket;
import
springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Value("${swagger.api.title}")
private String title;
@Value("${swagger.api.description}")
private String description;
@Value("${swagger.api.license}")
private String license;
@Value("${swagger.api.termsOfServiceUrl}")
private String termsOfServiceUrl;
@Value("${swagger.api.version}")
private String version;
@Value("${swagger.api.controller.basepackage}")
private String basePackage;
@Value("${swagger.api.contact.nombres}")
private String nombresContacto;
@Value("${swagger.api.contact.web}")
private String webContacto;
@Value("${swagger.api.contact.email}")
private String emailContacto;
@Bean
public Docket api() {
return new
Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage)
.paths(PathSelectors.regex("/.*"))
.build()
.apiInfo(apiEndPointsInfo());
}
private ApiInfo
apiEndPointsInfo() {
return new ApiInfoBuilder()
.title(title)
.description(description)
.contact(new Contact(nombresContacto, webContacto, emailContacto))
.license(license)
.licenseUrl(termsOfServiceUrl)
.version(version)
.build();
}
}
4. Finalmente agregar la anotacion @Api en tus Clases Controller
package
com.besoft.siserp.pos.controller;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.web.bind.annotation.CrossOrigin;
import
org.springframework.web.bind.annotation.PostMapping;
import
org.springframework.web.bind.annotation.RequestBody;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RestController;
import
com.besoft.siserp.pos.dto.ApiOutResponse;
import
com.besoft.siserp.pos.dto.request.CompraRequest;
import
com.besoft.siserp.pos.dto.request.EliminarCompraRequest;
import
com.besoft.siserp.pos.service.CompraService;
import
io.swagger.annotations.Api;
@RestController
@RequestMapping("/compra")
@CrossOrigin(origins = "http://localhost:4200")
@Api(value = "API
Compra")
public class CompraController {
@Autowired
CompraService
compraService;
@PostMapping("/registrarCompra")
public ApiOutResponse
registrarCompra(@RequestBody CompraRequest c) {
ApiOutResponse
entity = compraService.registrarCompra(c);
return entity;
}
}
5. Para ver la documentacion ejecutar el proyecto y escribir loclahost:8080/swagger-ui.html

0 Comentarios