白标错误页面 问题访问 Swagger

huangapple go评论80阅读模式
英文:

Whitelabel Error Page Problem accessing Swagger

问题

访问Swagger文档时,我遇到了这个错误:

白标签错误页面 此应用程序没有对 /error 的显式映射,因此您正在看到它作为回退。

2020年9月23日 星期三 09:38:17 BRT 发生了意外错误(类型=未找到,状态=404)。没有可用的消息

我不知道可能是什么原因...

pom.xml:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

类配置:

package com.simulacao.api.config;

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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket bancoApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.simulacao.api"))
                .paths(PathSelectors.regex("/*.*"))
                .build()
                .apiInfo(metaInfo());
    }

    private ApiInfo metaInfo() {
        return new ApiInfoBuilder()
                .title("API Rest")
                .description("\"API Rest\"")
                .version("1.0.0")
                .license("Apache License Version 2.0")
                .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0")
                .build();
    }
}

控制器:

@RestController
@RequestMapping(value = "/api/customer")
@Api(value = "API Rest Customer")
@CrossOrigin(origins = "*")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private CustomerRepository customerRepository;

    @GetMapping
    @ApiOperation(value = "Returns a list of customers")
    public ResponseEntity<List<Customer>> showAll() {
        return ResponseEntity.ok(this.customerService.findAll());
    }
}
英文:

When trying to access the Swagger documentation, I face this error:

> Whitelabel Error Page This application has no explicit mapping for
> /error, so you are seeing this as a fallback.
>
> Wed Sep 23 09:38:17 BRT 2020 There was an unexpected error (type=Not
> Found, status=404). No message available

I don't know what can be...

pom.xml:

&lt;dependency&gt;
			&lt;groupId&gt;io.springfox&lt;/groupId&gt;
			&lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
			&lt;version&gt;3.0.0&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;io.springfox&lt;/groupId&gt;
			&lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
			&lt;version&gt;3.0.0&lt;/version&gt;
		&lt;/dependency&gt;

Class configuration:

package com.simulacao.api.config;

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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket bancoApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage(&quot;com.simulacao.api&quot;))
                .paths(PathSelectors.regex(&quot;/*.*&quot;))
                .build()
                .apiInfo(metaInfo());
    }

    private ApiInfo metaInfo() {
        return new ApiInfoBuilder()
                .title(&quot;API Rest&quot;)
                .description(&quot;\&quot;API Rest\&quot;&quot;)
                .version(&quot;1.0.0&quot;)
                .license(&quot;Apache License Version 2.0&quot;)
                .licenseUrl(&quot;https://www.apache.org/licenses/LICENSE-2.0\&quot;&quot;)
                .build();
    }
}

Controller:

@RestController
@RequestMapping(value = &quot;/api/customer&quot;)
@Api(value = &quot;API Rest Customer&quot;)
@CrossOrigin(origins = &quot;*&quot;)
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private CustomerRepository customerRepository;

    @GetMapping
    @ApiOperation(value = &quot;Returns a list of customers&quot;)
    public ResponseEntity&lt;List&lt;Customer&gt;&gt; showAll() {
        return ResponseEntity.ok(this.customerService.findAll());
    }

答案1

得分: 0

请添加这些配置:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

    //Swagger UI property
    registry.addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/");

    registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
}

这必须在实现的配置中声明为 "WebMvcConfigurer"。

更多信息请参阅:https://springfox.github.io/springfox/docs/current/

英文:

Please add those configs

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {


    //Swagger UI property
    registry.addResourceHandler(&quot;swagger-ui.html&quot;)
            .addResourceLocations(&quot;classpath:/META-INF/resources/&quot;);

    registry.addResourceHandler(&quot;/webjars/**&quot;)
            .addResourceLocations(&quot;classpath:/META-INF/resources/webjars/&quot;);
}

This must be declareted in the config of implementation with "WebMvcConfigurer".

More information here : https://springfox.github.io/springfox/docs/current/

答案2

得分: 0

对于使用 Spring Boot 和 SpringFox 3,您只需以下内容:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

并且 /swagger-ui.html 现在是 /swagger-ui/index.html/swagger-ui。来自文档的说明:

swagger-ui 的位置已从 http://host/context-path/swagger-ui.html 移动到 http://host/context-path/swagger-ui/index.html 或 http://host/context-path/swagger-ui/(缩写)

英文:

For Spring Boot with SpringFox 3 all you need is following:

    &lt;dependency&gt;
        &lt;groupId&gt;io.springfox&lt;/groupId&gt;
        &lt;artifactId&gt;springfox-boot-starter&lt;/artifactId&gt;
        &lt;version&gt;3.0.0&lt;/version&gt;
    &lt;/dependency&gt;

and /swagger-ui.html is now /swagger-ui/index.html or /swagger-ui. From doc

> swagger-ui location has moved from http://host/context-path/swagger-ui.html to http://host/context-path/swagger-ui/index.html OR http://host/context-path/swagger-ui/ for short

huangapple
  • 本文由 发表于 2020年9月23日 21:05:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64028661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定