|
| 1 | +package br.com.virtuallibrary.config; |
| 2 | + |
| 3 | +import java.time.LocalDate; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | +import org.springframework.context.annotation.Import; |
| 10 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 11 | + |
| 12 | +import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration; |
| 13 | +import springfox.documentation.builders.PathSelectors; |
| 14 | +import springfox.documentation.builders.RequestHandlerSelectors; |
| 15 | +import springfox.documentation.builders.ResponseMessageBuilder; |
| 16 | +import springfox.documentation.service.ApiInfo; |
| 17 | +import springfox.documentation.service.Contact; |
| 18 | +import springfox.documentation.service.ResponseMessage; |
| 19 | +import springfox.documentation.spi.DocumentationType; |
| 20 | +import springfox.documentation.spring.web.plugins.Docket; |
| 21 | +import springfox.documentation.swagger2.annotations.EnableSwagger2; |
| 22 | + |
| 23 | +/** |
| 24 | + * Configuração do swagger. Para acesso, link abaixo: |
| 25 | + * http://localhost:8080/v2/api-docs |
| 26 | + * http://localhost:8080/swagger-ui.html#/ |
| 27 | + */ |
| 28 | +@Configuration |
| 29 | +@EnableSwagger2 |
| 30 | +@Import(BeanValidatorPluginsConfiguration.class) |
| 31 | +public class SwaggerConfig { |
| 32 | + |
| 33 | + @Bean |
| 34 | + public Docket api() { |
| 35 | + List<ResponseMessage> list = new java.util.ArrayList<>(); |
| 36 | + list.add(new ResponseMessageBuilder() |
| 37 | + .code(401) |
| 38 | + .message("Você não tem permissão para acessar esse recurso") |
| 39 | + .build()); |
| 40 | + list.add(new ResponseMessageBuilder() |
| 41 | + .code(403) |
| 42 | + .message("É proibido acessar o recurso") |
| 43 | + .build()); |
| 44 | + list.add(new ResponseMessageBuilder() |
| 45 | + .code(404) |
| 46 | + .message("O recurso que você estava tentando acessar não foi encontrado") |
| 47 | + .build()); |
| 48 | + list.add(new ResponseMessageBuilder() |
| 49 | + .code(500) |
| 50 | + .message("Erro interno do servidor") |
| 51 | + .build()); |
| 52 | + |
| 53 | + return new Docket(DocumentationType.SWAGGER_2) |
| 54 | + .select() |
| 55 | + .apis(RequestHandlerSelectors.basePackage("br.com.virtuallibrary.controllers")) |
| 56 | + .paths(PathSelectors.ant("/api/**")) |
| 57 | + .build() |
| 58 | + .directModelSubstitute(LocalDate.class, String.class) |
| 59 | + .useDefaultResponseMessages(false) |
| 60 | + .globalResponseMessage(RequestMethod.GET, list) |
| 61 | + .globalResponseMessage(RequestMethod.POST, list) |
| 62 | + .globalResponseMessage(RequestMethod.PUT, list) |
| 63 | + .globalResponseMessage(RequestMethod.DELETE, list) |
| 64 | + .apiInfo(apiInfo()); |
| 65 | + } |
| 66 | + |
| 67 | + private ApiInfo apiInfo() { |
| 68 | + return new ApiInfo( |
| 69 | + "Virtual Library REST API", |
| 70 | + "API para cadastro e consulta de livros.", |
| 71 | + "0.1.0", |
| 72 | + "https://smartbear.com/terms-of-use/", |
| 73 | + new Contact( "Daniel Oliveira", "", "[email protected]"), |
| 74 | + "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0.html", Collections.emptyList()); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments