Java的。春天的靴子。应用启动失败

时间:2017-07-23 07:20:35

标签: java spring maven spring-mvc

我试图自己学习Spring Boot。我使用Maven并使用Tomcat容器创建了spring boot应用程序。当我使用 mvn spring-boot:run 命令运行应用程序时,它工作正常。但是当我用ApplicationLoader类启动我的应用程序时,它给了我一个错误。此外,当我使用ARC时,它会给我错误 404 not found 。 当然,我知道我的ApplicationLoader类出了问题,但是我无法理解我应该改变什么才能使它工作。非常感谢您的帮助!

应用程序加载程序

package com.boris2barak.samplemvc.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication

public class ApplicationLoader extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder     application) {
return application.sources(ApplicationLoader.class);
}

public static void main(String[] args) throws Exception {
SpringApplication.run(ApplicationLoader.class, args);
}
}    

控制器

package com.boris2barak.samplemvc.app;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class myFirstController {


@RequestMapping("/temperature")
public String whatIsTheWeather(Model model, @RequestParam(value="city",        required=false, defaultValue="World") String city) {
model.addAttribute("city", city);
WeatherApp data = new WeatherApp();
return    data.getTemperatureForCity(city);
}
}

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>
<packaging>war</packaging>
<groupId>com.boris2barak</groupId>
<artifactId>samplemvc</artifactId>
<version>1.0-SNAPSHOT</version>


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

<properties>
    <java.version>1.8</java.version>
</properties>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.6.2</version>
    </dependency>



    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>



    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

错误看起来像那样:

objc[1937]: Class JavaLaunchHelper is implemented in both       /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/bin/java      (0x1043444c0) and      /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/libinstru      ment.dylib (0x1053b54e0). One of the two will be used. Which one is undefined.

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

2017-07-23 10:13:07.163  INFO 1937 --- [           main]     c.b.samplemvc.app.ApplicationLoader      : Starting ApplicationLoader on MBP-Boris     with PID 1937 (/Users/boris/Java/samplemvc/target/classes started by boris in     /Users/boris/Java/samplemvc)
2017-07-23 10:13:07.166  INFO 1937 --- [           main]     c.b.samplemvc.app.ApplicationLoader      : No active profile set, falling back to      default profiles: default
 2017-07-23 10:13:07.287  INFO 1937 --- [           main]      s.c.a.AnnotationConfigApplicationContext : Refreshing      org.springframework.context.annotation.AnnotationConfigApplicationContext@16022d9d     : startup date [Sun Jul 23 10:13:07 EEST 2017]; root of context hierarchy
2017-07-23 10:13:07.450  WARN 1937 --- [           main]      s.c.a.AnnotationConfigApplicationContext : Exception encountered during context       initialization - cancelling refresh attempt:      org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse      configuration class [com.boris2barak.samplemvc.app.ApplicationLoader]; nested      exception is java.lang.IllegalStateException: Failed to introspect annotated      methods on class org.springframework.boot.context.web.SpringBootServletInitializer
2017-07-23 10:13:07.487 ERROR 1937 --- [           main]      o.s.boot.SpringApplication               : Application startup failed

 org.springframework.beans.factory.BeanDefinitionStoreException: Failed to      parse configuration class [com.boris2barak.samplemvc.app.ApplicationLoader];      nested exception is java.lang.IllegalStateException: Failed to introspect      annotated methods on class      org.springframework.boot.context.web.SpringBootServletInitializer
    at      org.springframework.context.annotation.ConfigurationClassParser.parse(Configuratio     nClassParser.java:182) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at      org.springframework.context.annotation.ConfigurationClassPostProcessor.processConf     igBeanDefinitions(ConfigurationClassPostProcessor.java:321) ~[spring-context-     4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at      org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcess     BeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243) ~[spring-context-     4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at      org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDe     finitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273) ~     [spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at       org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFa      ctoryPostProcessors(PostProcessorRegistrationDelegate.java:98) ~[spring-context-      4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at      org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPo      stProcessors(AbstractApplicationContext.java:678) ~[spring-context-      4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at      org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApp     licationContext.java:520) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at      org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)       [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at      org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplicati     on.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at      org.springframework.boot.SpringApplication.run(SpringApplication.java:307)      [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at      org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)      [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at      org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)      [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at      com.boris2barak.samplemvc.app.ApplicationLoader.main(ApplicationLoader.java:24)       [classes/:na]
  Caused by: java.lang.IllegalStateException: Failed to introspect annotated      methods on class org.springframework.boot.context.web.SpringBootServletInitializer
    at      org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(Stand      ardAnnotationMetadata.java:163) ~[spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at      org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigura      tionClass(ConfigurationClassParser.java:292) ~[spring-context-      4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at      org.springframework.context.annotation.ConfigurationClassParser.processConfigurati      onClass(ConfigurationClassParser.java:232) ~[spring-context-      4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at      org.springframework.context.annotation.ConfigurationClassParser.parse(Configuratio     nClassParser.java:199) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at      org.springframework.context.annotation.ConfigurationClassParser.parse(Configuratio     nClassParser.java:168) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    ... 12 common frames omitted
 Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_131]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~     [na:1.8.0_131]
    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_131]
    at       org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(Stand      ardAnnotationMetadata.java:152) ~[spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    ... 16 common frames omitted
 Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_131]
   at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_131]
   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~     [na:1.8.0_131]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_131]
    ... 20 common frames omitted

2017-07-23 10:13:07.502  INFO 1937 --- [           main]      .b.l.ClasspathLoggingApplicationListener : Application failed to start with      classpath:      [file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/charsets.jar,      file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/deploy.jar,      file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/     cldrdata.jar,      file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/dnsns.jar,      file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/jaccess.jar,      file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/jfxrt.jar,      file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/localedata.jar,      file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/nashorn.jar,      file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunec.jar,      file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar,      file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar,      file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/ext/zipfs.jar,      file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/javaws.jar,       file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jce.jar, file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfr.jar, file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jfxswt.jar, file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/jsse.jar, file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/management-agent.jar, file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/plugin.jar, file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/resources.jar, file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/rt.jar, file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/ant-javafx.jar, file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/dt.jar, file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/javafx-mx.jar, file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/jconsole.jar, file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/packager.jar, file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/sa-jdi.jar, file:/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/lib/tools.jar, file:/Users/boris/Java/samplemvc/target/classes/, file:/Users/boris/.m2/repository/org/springframework/boot/spring-boot-starter-web/1.3.5.RELEASE/spring-boot-starter-web-1.3.5.RELEASE.jar, file:/Users/boris/.m2/repository/org/springframework/boot/spring-boot-starter/1.3.5.RELEASE/spring-boot-starter-1.3.5.RELEASE.jar, file:/Users/boris/.m2/repository/org/springframework/boot/spring-boot/1.3.5.RELEASE/spring-boot-1.3.5.RELEASE.jar, file:/Users/boris/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/1.3.5.RELEASE/spring-boot-autoconfigure-1.3.5.RELEASE.jar, file:/Users/boris/.m2/repository/org/springframework/boot/spring-boot-starter-logging/1.3.5.RELEASE/spring-boot-starter-logging-1.3.5.RELEASE.jar, file:/Users/boris/.m2/repository/ch/qos/logback/logback-classic/1.1.7/logback-classic-1.1.7.jar, file:/Users/boris/.m2/repository/ch/qos/logback/logback-core/1.1.7/logback-core-1.1.7.jar, file:/Users/boris/.m2/repository/org/slf4j/slf4j-api/1.7.21/slf4j-api-1.7.21.jar, file:/Users/boris/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.21/jcl-over-slf4j-1.7.21.jar, file:/Users/boris/.m2/repository/org/slf4j/jul-to-slf4j/1.7.21/jul-to-slf4j-1.7.21.jar, file:/Users/boris/.m2/repository/org/slf4j/log4j-over-slf4j/1.7.21/log4j-over-slf4j-1.7.21.jar, file:/Users/boris/.m2/repository/org/springframework/spring-core/4.2.6.RELEASE/spring-core-4.2.6.RELEASE.jar, file:/Users/boris/.m2/repository/org/yaml/snakeyaml/1.16/snakeyaml-1.16.jar, file:/Users/boris/.m2/repository/org/springframework/boot/spring-boot-starter-validation/1.3.5.RELEASE/spring-boot-starter-validation-1.3.5.RELEASE.jar, file:/Users/boris/.m2/repository/org/hibernate/hibernate-validator/5.2.4.Final/hibernate-validator-5.2.4.Final.jar, file:/Users/boris/.m2/repository/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar, file:/Users/boris/.m2/repository/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.jar, file:/Users/boris/.m2/repository/com/fasterxml/classmate/1.1.0/classmate-1.1.0.jar, file:/Users/boris/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.6.6/jackson-databind-2.6.6.jar, file:/Users/boris/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.6.6/jackson-annotations-2.6.6.jar, file:/Users/boris/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.6.6/jackson-core-2.6.6.jar, file:/Users/boris/.m2/repository/org/springframework/spring-web/4.2.6.RELEASE/spring-web-4.2.6.RELEASE.jar, file:/Users/boris/.m2/repository/org/springframework/spring-aop/4.2.6.RELEASE/spring-aop-4.2.6.RELEASE.jar, file:/Users/boris/.m2/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar, file:/Users/boris/.m2/repository/org/springframework/spring-beans/4.2.6.RELEASE/spring-beans-4.2.6.RELEASE.jar, file:/Users/boris/.m2/repository/org/springframework/spring-context/4.2.6.RELEASE/spring-context-4.2.6.RELEASE.jar, file:/Users/boris/.m2/repository/org/springframework/spring-webmvc/4.2.6.RELEASE/spring-webmvc-4.2.6.RELEASE.jar, file:/Users/boris/.m2/repository/org/springframework/spring-expression/4.2.6.RELEASE/spring-expression-4.2.6.RELEASE.jar, file:/Users/boris/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/8.0.33/tomcat-embed-el-8.0.33.jar, file:/Users/boris/.m2/repository/com/google/code/gson/gson/2.6.2/gson-2.6.2.jar,      file:/Users/boris/.m2/repository/commons-io/commons-io/2.5/commons-io-2.5.jar,      file:/Applications/IntelliJ%20IDEA%20CE.app/Contents/lib/idea_rt.jar]

Process finished with exit code 1

2 个答案:

答案 0 :(得分:1)

从堆栈跟踪中可以清楚地看到问题是java.lang.NoClassDefFoundError: javax/servlet/ServletContext意味着该类不在CLASSPATH中,而这些存在于tomcat jar中,它是spring-boot-starter-tomcat依赖项的一部分。

因此,要使其与spring boot standalone war / fat jar一起使用,spring-boot-starter-tomcat依赖项应该在compile范围内。

但是如果你想部署到像tomcat这样的java容器中,容器本身就有servlet类,那么你就可以在提供的范围内保留spring-boot-starter-tomcat

最好的处理方法是创建两个maven配置文件,并在一个中将spring-boot-starter-tomcat保留在编译范围内,并保留在提供范围内的另一个配置文件中。

答案 1 :(得分:0)

您需要使用提供

的类更改SpringBoot主类ApplicationLoader
@SpringBootApplication(scanBasePackages = "com.boris2barak")
public class ApplicationLoader{

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

}