1

Olá, estou estudando Spring Mvc e Spring Boot e me deparei com seguinte problema que até o momento não consegui identificar. Após executar meu método main recebo a seguinte mensagem.

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing **this as a fallback. Tue Jul 19 21:22:43 BRT 2016 There was an unexpected error (type=Not Found, status=404). No message available

Controller

@Controller
public class HomeController {

    @RequestMapping(value="/login", method=RequestMethod.GET)
    public String redirect() {
        return "login/login";
    }
}

Método Main

@SpringBootApplication()
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Configuration

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.gervasios.rsw")
public class ApplicationConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/pages/");
        viewResolver.setSuffix(".jsp");
        registry.viewResolver(viewResolver);
    }
}

POM

<!-- Spring BOOT-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.2.5.RELEASE</spring.version>          
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <!-- Javax - Jsf -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

LOG localhost:8080/restauranteWeb

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.5.RELEASE)

2016-07-19 21:22:34.153  INFO 4480 --- [           main] com.gervasios.rsw.run.Application        : Starting Application on Helio-PC with PID 4480 (C:\Users\Helio\git\restauranteWeb\restauranteWeb\target\classes started by Helio in C:\Users\Helio\git\restauranteWeb\restauranteWeb)
2016-07-19 21:22:34.160  INFO 4480 --- [           main] com.gervasios.rsw.run.Application        : No active profile set, falling back to default profiles: default
2016-07-19 21:22:34.251  INFO 4480 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1f32bf7: startup date [Tue Jul 19 21:22:34 BRT 2016]; root of context hierarchy
2016-07-19 21:22:36.222  INFO 4480 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$c19cd918] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-07-19 21:22:36.976  INFO 4480 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-07-19 21:22:36.997  INFO 4480 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-07-19 21:22:36.999  INFO 4480 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.33
2016-07-19 21:22:37.169  INFO 4480 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-07-19 21:22:37.169  INFO 4480 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2920 ms
2016-07-19 21:22:37.606  INFO 4480 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-07-19 21:22:37.611  INFO 4480 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-07-19 21:22:37.611  INFO 4480 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-07-19 21:22:37.611  INFO 4480 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-07-19 21:22:37.611  INFO 4480 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-07-19 21:22:38.370  INFO 4480 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2016-07-19 21:22:38.383  INFO 4480 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2016-07-19 21:22:38.483  INFO 4480 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {4.3.11.Final}
2016-07-19 21:22:38.486  INFO 4480 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2016-07-19 21:22:38.488  INFO 4480 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2016-07-19 21:22:38.732  INFO 4480 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2016-07-19 21:22:38.806  INFO 4480 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2016-07-19 21:22:38.872  INFO 4480 --- [           main] o.h.h.i.ast.ASTQueryTranslatorFactory    : HHH000397: Using ASTQueryTranslatorFactory
2016-07-19 21:22:39.332  INFO 4480 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1f32bf7: startup date [Tue Jul 19 21:22:34 BRT 2016]; root of context hierarchy
2016-07-19 21:22:39.423  INFO 4480 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-07-19 21:22:39.424  INFO 4480 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-07-19 21:22:39.462  INFO 4480 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-07-19 21:22:39.463  INFO 4480 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-07-19 21:22:39.523  INFO 4480 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-07-19 21:22:39.796  INFO 4480 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-07-19 21:22:39.906  INFO 4480 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-07-19 21:22:39.911  INFO 4480 --- [           main] com.gervasios.rsw.run.Application        : Started Application in 6.145 seconds (JVM running for 6.645)
2016-07-19 21:22:43.072  INFO 4480 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-07-19 21:22:43.072  INFO 4480 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2016-07-19 21:22:43.094  INFO 4480 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 22 ms

Index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h1>Por Favor Aguarde...</h1>
<c:redirect url="/login"/>
2
  • Existe a necessidade das páginas jsp ficarem dentro da pasta WEB-INF?
    – adelmo00
    Commented 20/07/2016 às 2:26
  • Olá adelmo00, bom não tem necessidade! Eu segui de acordo com autor do livro Vire o Jogo com Spring Framework da casa do código. Depois fui incorporando por conta própria a aplicação, retirando configurações xml, usando o spring boot etc..
    – Ger
    Commented 20/07/2016 às 14:17

1 Resposta 1

1

Pelo erro que foi exibido (404), você provavelmente está acessando uma URL que não está mapeada no seu servidor. Eu copiei sua classe e executei normalmente na minha aplicação, acessando localhost:8080/login (url que foi mapeada no @RequestMaping do seu controller).

Por qual endereço que você está tentando acessar sua view?

Para acessar localhost:8080/restauranteWeb você precisará criar um controller que responderá a esta URL, caso contrário você não conseguirá acessar.

Tenta criar algo do tipo :

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value="/restauranteWeb")
public class RestauranteWebController {

 @RequestMapping(method=RequestMethod.GET)
    public ModelAndView redirect() {
        ModelAndView mv = new ModelAndView("Index.jsp");
        return mv;
    }

}

4
  • Obrigado pela resposta Jeronimo Nascimento. Bom, estou passando o endereço do sistema localhost:8080/restauranteWeb. Na minha classe Configuration eu defini como default o mapeamento "/" para minha página index.jsp, essa definição fiz no método addViewControllers da classe Configuration. Na minha página index.jsp tem apensas um redirecionamento para login. Inseri também minha página index.jsp na minha pergunta.
    – Ger
    Commented 21/07/2016 às 16:05
  • Olá, @EdeGerSil. Editei minha resposta, dá uma conferida pra ver se resolve teu problema. Espero ter ajudado. Commented 21/07/2016 às 17:00
  • Ok, Jeronimo Nascimento, obrigado pela resposta novamente. Vou fazer as mudanças e posto os resultados!
    – Ger
    Commented 21/07/2016 às 17:07
  • Mesmo log Jeronimo Nascimento e mesmo problema, depois tentei inserindo "/" no requestMapping e também ocorreu o mesmo problema!
    – Ger
    Commented 21/07/2016 às 21:22

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .