1
resposta

Exibindo string Hello World ao invés da API Swagger

Olá, ao entrar com o endereço http://localhost:8080/swagger-ui.html está exibindo no browser a String "Hello World" do controller HelloController. Tive que comentar toda a classe HelloController para exibir a documentação da API, mas nesse caso, fica faltando a HelloController na lista. Na vídeo aula isso não acontece e exibe o controller HelloController na lista de controllers. Não consegui identificar porque está exibindo a String "Hello World" ao invés da documentação Swagger da API Seguem as classes.

@Controller
public class HelloController {

    @RequestMapping
    @ResponseBody 
    public String hello() {
        return "Hello World";
    }
}
@SpringBootApplication
@EnableSpringDataWebSupport 
@EnableCaching 
@EnableSwagger2
public class ForumApplication {
    public static void main(String[] args) {
        SpringApplication.run(ForumApplication.class, args);
    }
}
@Configuration
public class SwaggerConfigurations {
    @Bean
    public Docket forumApi() {        
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("br.com.alura.forum"))
                .paths(PathSelectors.ant("/**"))
                .build()
                .ignoredParameterTypes(Usuario.class);
    }
}
@EnableWebSecurity
@Configuration
public class SecurityConfigurations extends WebSecurityConfigurerAdapter{

    @Autowired
    private AutenticacaoService autenticacaoService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private UsuarioRepository usuarioRepository;

    @Override
    @Bean
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(autenticacaoService).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers(HttpMethod.GET, "/topicos").permitAll() 
            .antMatchers(HttpMethod.GET, "/topicos/*").permitAll()
            .antMatchers(HttpMethod.POST, "/auth").permitAll()
            .antMatchers(HttpMethod.GET, "/actuator/**").permitAll()
            .anyRequest().authenticated()    
            .and().csrf().disable()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
            .and().addFilterBefore(new AutenticacaoViaTokenFilter(tokenService, usuarioRepository), UsernamePasswordAuthenticationFilter.class); //Spring Boot API Rest: Segurança da API, Cache e Monitoramento - 05.Autenticação via JWT, ITEM 02. Recuperando o token do header Authorization 
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
        .antMatchers("/h2-console/**")//para liberar o acesso ao banco h2
        .antMatchers("/**.html", "/v2/api-docs", "/webjars/**", "/configuration/**", "/swagger-resources/**");//Para liberar o acesso a API Swagger sem precisar autenticar
    }
1 resposta

Oi Márcio,

É que no seu HelloController a anotação @RequestMapping está sem o endereço "/".

Altere para:

@RequestMapping("/")

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software