带有tomcat的spring boot webapplication在启动时挂起

时间:2017-08-01 18:44:05

标签: java tomcat spring-boot netbeans-8

我正在尝试使用netbeans 8.2中的spring boot initiliazer项目选项构建一个web应用程序。当我启动它时,RUN进度条在完成调度程序Servlet的初始化后挂起。当我尝试从localhost获取页面时:8080我收到错误。我认为这与调度程序servlet配置有关,但我不确定,有人可以帮我吗?以下是输出日志。

cd C:\Users\maurice\Documents\NetBeansProjects\seedcalendarwebinit; SPRING_OUTPUT_ANSI_ENABLED=always "JAVA_HOME=C:\\Program Files\\Java\\jdk1.8.0_131" cmd /c "\"\"C:\\Program Files\\NetBeans 8.2\\java\\maven\\bin\\mvn.bat\" -Drun.jvmArguments=\"-noverify -XX:TieredStopAtLevel=1\" -Drun.mainClass=com.example.demo.SeedcalendarApplication -Dmaven.ext.class.path=\"C:\\Program Files\\NetBeans 8.2\\java\\maven-nblib\\netbeans-eventspy.jar\" -Dfile.encoding=UTF-8 spring-boot:run\""
Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
Scanning for projects...

------------------------------------------------------------------------
Building seedcalendar 0.0.1-SNAPSHOT
------------------------------------------------------------------------

>>> spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) @ demo >>>

--- maven-resources-plugin:2.6:resources (default-resources) @ demo ---
Using 'UTF-8' encoding to copy filtered resources.
Copying 1 resource
Copying 3 resources

--- maven-compiler-plugin:3.1:compile (default-compile) @ demo ---
Nothing to compile - all classes are up to date

--- maven-resources-plugin:2.6:testResources (default-testResources) @ demo ---
Using 'UTF-8' encoding to copy filtered resources.
skip non existing resourceDirectory C:\Users\maurice\Documents\NetBeansProjects\seedcalendarwebinit\src\test\resources

--- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ demo ---
Nothing to compile - all classes are up to date

<<< spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) @ demo <<<

--- spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) @ demo ---
Attaching agents: []

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

2017-08-01 20:31:48.213  INFO 2796 --- [           main] c.example.demo.SeedcalendarApplication   : Starting SeedcalendarApplication on DESKTOP-70S441T with PID 2796 (C:\Users\maurice\Documents\NetBeansProjects\seedcalendarwebinit\target\classes started by maurice in C:\Users\maurice\Documents\NetBeansProjects\seedcalendarwebinit)
2017-08-01 20:31:48.215  INFO 2796 --- [           main] c.example.demo.SeedcalendarApplication   : No active profile set, falling back to default profiles: default
2017-08-01 20:31:48.412  INFO 2796 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4fcd19b3: startup date [Tue Aug 01 20:31:48 CEST 2017]; root of context hierarchy
2017-08-01 20:31:48.879  INFO 2796 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'dataSource' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Tomcat; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]]
2017-08-01 20:31:49.675  INFO 2796 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-08-01 20:31:49.685  INFO 2796 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-08-01 20:31:49.687  INFO 2796 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.16
2017-08-01 20:31:49.764  INFO 2796 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-08-01 20:31:49.765  INFO 2796 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1357 ms
2017-08-01 20:31:49.877  INFO 2796 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-08-01 20:31:49.881  INFO 2796 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-08-01 20:31:49.882  INFO 2796 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-08-01 20:31:49.882  INFO 2796 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-08-01 20:31:49.883  INFO 2796 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-08-01 20:31:50.319  INFO 2796 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-08-01 20:31:50.329  INFO 2796 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2017-08-01 20:31:50.378  INFO 2796 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2017-08-01 20:31:50.379  INFO 2796 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2017-08-01 20:31:50.380  INFO 2796 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2017-08-01 20:31:50.410  INFO 2796 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-08-01 20:31:50.499  INFO 2796 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2017-08-01 20:31:50.756  INFO 2796 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-08-01 20:31:51.074  INFO 2796 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4fcd19b3: startup date [Tue Aug 01 20:31:48 CEST 2017]; root of context hierarchy
2017-08-01 20:31:51.290  INFO 2796 --- [           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)
2017-08-01 20:31:51.291  INFO 2796 --- [           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)
2017-08-01 20:31:51.317  INFO 2796 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-01 20:31:51.317  INFO 2796 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-01 20:31:51.348  INFO 2796 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-01 20:31:51.745  INFO 2796 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-08-01 20:31:51.791  INFO 2796 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-08-01 20:31:51.795  INFO 2796 --- [           main] c.example.demo.SeedcalendarApplication   : Started SeedcalendarApplication in 3.8 seconds (JVM running for 4.118)
2017-08-01 20:32:08.031  INFO 2796 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-08-01 20:32:08.031  INFO 2796 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-08-01 20:32:08.047  INFO 2796 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms

编辑:我这次只在DEBUG LOG模式下重新运行,这是我从调度程序servlet获得的额外信息

2017-08-01 21:07:28.999 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing servlet 'dispatcherServlet'
2017-08-01 21:07:28.999  INFO 13376 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-08-01 21:07:29.000  INFO 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-08-01 21:07:29.000 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Using MultipartResolver [org.springframework.web.multipart.support.StandardServletMultipartResolver@adcfad9]
2017-08-01 21:07:29.004 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Unable to locate LocaleResolver with name 'localeResolver': using default [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver@58fd7fbe]
2017-08-01 21:07:29.008 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Unable to locate ThemeResolver with name 'themeResolver': using default [org.springframework.web.servlet.theme.FixedThemeResolver@f0ee540]
2017-08-01 21:07:29.013 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator': using default [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@2dae4a0b]
2017-08-01 21:07:29.019 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Unable to locate FlashMapManager with name 'flashMapManager': using default [org.springframework.web.servlet.support.SessionFlashMapManager@d8475b6]
2017-08-01 21:07:29.019 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Published WebApplicationContext of servlet 'dispatcherServlet' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet]
2017-08-01 21:07:29.019  INFO 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms
2017-08-01 21:07:29.019 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Servlet 'dispatcherServlet' configured successfully
2017-08-01 21:07:29.033 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/]
2017-08-01 21:07:29.036 DEBUG 13376 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /
2017-08-01 21:07:29.040 DEBUG 13376 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/]
2017-08-01 21:07:29.040 DEBUG 13376 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/] are [/**]
2017-08-01 21:07:29.041 DEBUG 13376 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/] are {}
2017-08-01 21:07:29.042 DEBUG 13376 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@163042ea]]] and 1 interceptor
2017-08-01 21:07:29.043 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/] is: -1
2017-08-01 21:07:29.044 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-08-01 21:07:29.044 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2017-08-01 21:07:29.053 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2017-08-01 21:07:29.053 DEBUG 13376 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2017-08-01 21:07:29.057 DEBUG 13376 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]
2017-08-01 21:07:29.057 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/error] is: -1
2017-08-01 21:07:29.118 DEBUG 13376 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html])
2017-08-01 21:07:29.128 DEBUG 13376 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView@3b27b497] based on requested media type 'text/html'
2017-08-01 21:07:29.128 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Rendering view [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView@3b27b497] in DispatcherServlet with name 'dispatcherServlet'
2017-08-01 21:07:29.142 DEBUG 13376 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Successfully completed request

我的调度程序servlet初始化程序类

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 *
 * @author maurice
 */
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected String[] getServletMappings(){
        return new String[]{"/"};
    }

    @Override
    protected Class<?>[] getRootConfigClasses(){
        return new Class<?>[] {RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses(){
        return new Class<?>[] {WebConfig.class};
    }
}

我的webconfig

@Configuration
@EnableWebMvc
@ComponentScan("Controllers")
public class WebConfig extends WebMvcConfigurerAdapter{

    @Bean
    public ViewResolver viewResolver(
                    SpringTemplateEngine templateEngine){
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine);
        return viewResolver;
    }

    @Bean
    public TemplateEngine templateEngine(
                        TemplateResolver templateResolver){
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        return templateEngine;
    }

    @Bean
    public TemplateResolver templateResolver(){
        TemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setPrefix("/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        return templateResolver;
    }


    //zorgt ervoor dat statische requests niet meer door de servletdispatcher afgehandeld worden
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
        configurer.enable();
    }
}

我的控制器

@Controller
@RequestMapping(value = {"","/"})
public class LoginScreenController
 {


    public LoginScreenController() {

    }

    public LoginScreenController(User_ user) {
        this.user = user;
    }


    @RequestMapping(value = "/hello", method = GET)
    public String loginForm(){
        return "login";
    }
}

这是我的POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.seedcalendar</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>seedcalendar</name>
    <description>seedcalendar</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>2.5.1</version>
</dependency>         
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这是我在浏览器中看到的错误

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Aug 01 20:32:31 CEST 2017
There was an unexpected error (type=Not Found, status=404).
No message available

谢谢

编辑:

所以我已经将它添加到我的主类中,以检查控制器是否包含在应用程序上下文中。

@SpringBootApplication
public class SeedcalendarApplication implements ApplicationContextAware {
private static ApplicationContext ac;
    public static void main(String[] args) {
        SpringApplication.run(SeedcalendarApplication.class, args);
                System.out.println("---------------"+ac.containsBean("loginScreenController"));
                LoginScreenController log = ac.getBean(LoginScreenController.class);
                //for (String i : ac.getBeanDefinitionNames()) System.out.println(i);
    }
            @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.ac = applicationContext;
    }
}

它输出FALSE,意味着登录控制器存在于应用程序上下文中,这解释了为什么找不到get方法的原因。我采取的下一步是将logincontroller作为bean包含在Rootconfig和Webconfig类中,如下所示:

    @Bean
public LoginScreenController loginScreenController(){
    return new LoginScreenController();
}

但是当我尝试在主类中使用getBean()方法时,我仍然得到一个org.springframework.beans.factory.NoSuchBeanDefinitionException!有谁知道为什么?

1 个答案:

答案 0 :(得分:0)

问题的原因是主类和配置类在不同的包中。很奇怪......但问题解决了。