首页 > 解决方案 > Swagger UI 版本 3 返回 404 页面但 api-docs 正在工作

问题描述

我正在尝试设置 swagger-UI 来测试我的 springboot REST API 而不是使用邮递员但是在经历了一些教程和一些问题之后,当我尝试通过我的浏览器访问 html 页面时,我似乎无法克服 404 错误。

我的依赖:

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

弹簧配置:


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SpringFoxConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

}

和我的控制器


@Controller
@CrossOrigin
@RequestMapping(path = "/api")
public class AppController {

    @ResponseBody
    @PostMapping(path ="/api")
    public String home() {
        return "Hello World";
    }
    @GetMapping(path = { "/api/Teacher/{id}"})
    public TeacherPayload getTeacher(@PathVariable(required=false,name="id") String id) throws 
    Exception{

        if (id != null) {
            return teacher.getTeacher(id);

        } else {
            return teacher.getTeachers();

        }

....

我将端口号从默认的 8080 更改为 3005,但我认为这不是问题,因为我尝试恢复到 8080 无济于事。

编辑:我的安全配置如下,注意我允许所有路径在测试时绕过安全

    @Autowired
    private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;

    @Autowired
    private UserDetailsServiceImpl jwtUserDetailsService;

    @Autowired
    private JwtRequestFilter jwtRequestFilter;


    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        // configure AuthenticationManager so that it knows from where to load
        // user for matching credentials
        // Use BCryptPasswordEncoder
        auth.userDetailsService(jwtUserDetailsService).passwordEncoder(passwordEncoder());
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(12);
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/authenticate");
        web.ignoring().antMatchers("/recoverPortal");
    }


    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        // We don't need CSRF for this example
        httpSecurity.cors();
        httpSecurity.csrf().disable()
                // dont authenticate this particular request

                .authorizeRequests().antMatchers(HttpMethod.OPTIONS, "**").permitAll()
                .antMatchers("/**").permitAll()
                .antMatchers("/newUser").authenticated()
                .antMatchers("/newUser").authenticated()
                .antMatchers("/admin/**").hasAnyAuthority("USER_ADMIN")
                .antMatchers("/resetPassword").authenticated()// all other requests need to be authenticated
                        .anyRequest().authenticated().and().
                // make sure we use stateless session; session won't be used to
                // store user's state.
                        exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        // Add a filter to validate the tokens with every request
        httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
    }

标签: spring-bootswagger-uispringfox

解决方案


你在打什么端点?它应该/swagger-ui/在最后,而不是/swagger-ui.html。另外,我认为您可以省略springfox-swagger-ui依赖项。在我的设置中,我只需要springfox-boot-starter一个。


推荐阅读