Failed to get Swagger interface with spring boot

I follow an article at http://raibledesigns.com/rd/entry/documenting_your_spring_api_with .

Everything works fine, but is not able to integrate the Swagger interface.

http://localhost:8080/docs/index.html 

leads to redirect / error.

+6
source share
3 answers

I know this is an old question, but maybe this will help someone with a similar problem in the future.

I followed a similar guide to what you mentioned, and I did it without problems. I put my own document on how to configure Swagger with a user interface in a Spring boot project a couple of weeks ago. Perhaps this will help you, as it is shorter and more relevant.

Add Maven Dependencies

Paste them into your pom.xml:

 <dependency> <groupId>com.mangofactory</groupId> <artifactId>swagger-springmvc</artifactId> <version>1.0.2</version> <type>jar</type> </dependency> 

Add Swagger UI

Download Swagger UI from github. Copy the dist folder to the webapp directory and rename dist to swagger (or any other name).

Open the index.html file inside the copied directory and change the url in the first javascript function to point to /api-docs endpoint :

 var url = window.location.search.match(/url=([^&]+)/); if (url && url.length > 1) { url = decodeURIComponent(url[1]); } else { url = "/project-name/api-docs"; } 

Configuring Swagger

Create the SwaggerConfig.java class and configure swagger there:

 @Configuration @EnableSwagger @EnableAutoConfiguration public class SwaggerConfig { private SpringSwaggerConfig springSwaggerConfig; @Autowired public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) { this.springSwaggerConfig = springSwaggerConfig; } @Bean public SwaggerSpringMvcPlugin customImplementation() { return new SwaggerSpringMvcPlugin(this.springSwaggerConfig) // Root level documentation .apiInfo(new ApiInfo("Swagger-demo JSON API", "This service provides a JSON representation the service API", null, null, null, null)) .useDefaultResponseMessages(false) // Map the specific URL patterns into Swagger .includePatterns("/greeting.*"); } } 

Your chwanz should now be running. Try accessing /project-name/swagger/index.html .

+2
source

I answer this with the swagger2 configuration inside the spring boot application. Below is the configuration required for Swagger2.

Add gradle Configuration

Add gradle dependencies inside build.gradle file

 dependencies { compile("io.springfox:springfox-swagger2:2.0.2") compile("io.springfox:springfox-swagger-ui:2.0.2") } 

Swagger2 Lock Class

 @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket userApi() { return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()).build().pathMapping("/") .directModelSubstitute(LocalDate.class, String.class) .genericModelSubstitutes(ResponseEntity.class) .alternateTypeRules(newRule( typeResolver.resolve(DeferredResult.class, typeResolver.resolve(ResponseEntity.class, WildcardType.class)), typeResolver.resolve(WildcardType.class))) .useDefaultResponseMessages(false) .globalResponseMessage(RequestMethod.GET, newArrayList(new ResponseMessageBuilder().code(500).message("500 message") .responseModel(new ModelRef("Error")).build())) .securitySchemes(newArrayList(apiKey())).securityContexts(newArrayList(securityContext())) .apiInfo(apiInfo()); } @Autowired private TypeResolver typeResolver; private ApiKey apiKey() { return new ApiKey("mykey", "api_key", "header"); } private SecurityContext securityContext() { return SecurityContext.builder().securityReferences(defaultAuth()) .forPaths(PathSelectors.regex("/anyPath.*")).build(); } List<SecurityReference> defaultAuth() { AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; return newArrayList(new SecurityReference("mykey", authorizationScopes)); } @Bean SecurityConfiguration security() { return new SecurityConfiguration("123456", "test-app-realm", "clientapp", "apiKey"); } @Bean UiConfiguration uiConfig() { return new UiConfiguration("validatorUrl"); } private ApiInfo apiInfo() { ApiInfo apiInfo = new ApiInfo("DSM API", "API for DSM", "1.0.0", "termsOfServiceUrl", " nijo@ndimensionz.com ", null, null); return apiInfo; } } 

Add Swagger UI

Download Swagger UI from github. Copy the dist folder to the src / main / resources / static directory and rename dist to swagger

HomeController.class

 @Api(basePath = "/", value = "/", description = "Home Controller") @Controller public class HomeController { @RequestMapping("/") public String home() { return "redirect:swagger-ui.html"; } } 

Myapplication.class

 @SpringBootApplication @ComponentScan(basePackageClasses = SwaggerConfig.class) public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } 

Configure the spring boot application with dependencies and run to see the API.

The URL will be http: // localhost: 8080 / v2 / swagger-ui.html you can also configure this as the answer above.

+1
source

I have the same problem too. I was able to see json but not swagger ui after adding dest aslo folder. In the initialization class that extends SpringBootServletInitializer , I added a method below, then it worked fine

  @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SampleWebStaticApplication.class); } 

refer link

0
source

Source: https://habr.com/ru/post/969601/


All Articles