使用Spring MVC运行Sprint Boot时抛出Nullpointer异常

时间:2017-11-08 22:16:04

标签: spring spring-mvc spring-boot

我正在使用spring boot构建spring mvc项目,但我无法启动我的Web应用程序。我已经关注了几个教程,但无法弄明白。我可以用spring boot启动我的应用程序但是当我添加Spring mvc时,它开始抛出错误。我收到了以下错误

  

java.lang.NullPointerException:null           在org.springframework.core.BridgeMethodResolver.findBridgedMethod(BridgeMethodResolver.java:60)           在org.springframework.beans.GenericTypeAwarePropertyDescriptor。(GenericTypeAwarePropertyDescriptor.java:70)           at org.springframework.beans.CachedIntrospectionResults.buildGenericTypeAwarePropertyDescriptor(CachedIntrospectionResults.java:366)           在org.springframework.beans.CachedIntrospectionResults。(CachedIntrospectionResults.java:302)           在org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:189)           在org.springframework.beans.BeanUtils.getPropertyDescriptors(BeanUtils.java:354)           在org.springframework.boot.bind.PropertiesConfigurationFactory.getNames(PropertiesConfigurationFactory.java:288)           在org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:269)           在org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240)           在org.springframework.boot.context.config.ConfigFileApplicationListener.bindToSpringApplication(ConfigFileApplicationListener.java:241)           在org.springframework.boot.context.config.ConfigFileApplicationListener.postProcessEnvironment(ConfigFileApplicationListener.java:197)           at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:182)           在org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:168)           在org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)           在org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)           在org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)           在org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)           在org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74)           在org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)           在org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:325)           在org.springframework.boot.SpringApplication.run(SpringApplication.java:296)           在org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)           在org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)           在com.azzimov.search.BootStarter.StartApplication.main(StartApplication.java:17)           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)           at java.lang.reflect.Method.invoke(Method.java:498)           在org.springframework.boot.maven.AbstractRunMojo $ LaunchRunner.run(AbstractRunMojo.java:527)           在java.lang.Thread.run(Thread.java:745)[警告] java.lang.reflect.InvocationTargetException           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)           at java.lang.reflect.Method.invoke(Method.java:498)           在org.springframework.boot.maven.AbstractRunMojo $ LaunchRunner.run(AbstractRunMojo.java:527)           at java.lang.Thread.run(Thread.java:745)引起:java.lang.NullPointerException           在org.springframework.core.BridgeMethodResolver.findBridgedMethod(BridgeMethodResolver.java:60)           在org.springframework.beans.GenericTypeAwarePropertyDescriptor。(GenericTypeAwarePropertyDescriptor.java:70)           at org.springframework.beans.CachedIntrospectionResults.buildGenericTypeAwarePropertyDescriptor(CachedIntrospectionResults.java:366)           在org.springframework.beans.CachedIntrospectionResults。(CachedIntrospectionResults.java:302)           在org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:189)           在org.springframework.beans.BeanUtils.getPropertyDescriptors(BeanUtils.java:354)           在org.springframework.boot.bind.PropertiesConfigurationFactory.getNames(PropertiesConfigurationFactory.java:288)           在org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:269)           在org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240)           在org.springframework.boot.context.config.ConfigFileApplicationListener.bindToSpringApplication(ConfigFileApplicationListener.java:241)           在org.springframework.boot.context.config.ConfigFileApplicationListener.postProcessEnvironment(ConfigFileApplicationListener.java:197)           at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:182)           在org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:168)           在org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)           在org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)           在org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)           在org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)           在org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74)           在org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)           在org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:325)           在org.springframework.boot.SpringApplication.run(SpringApplication.java:296)           在org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)           在org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)           在com.azzimov.search.BootStarter.StartApplication.main(StartApplication.java:17)           ......还有6个

我的pom.xml是

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.0</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.1.RELEASE</version>
    </dependency>


    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.1.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.0.1.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>1.5.8.RELEASE</version>
    </dependency>

  </dependencies>
  <build>
    <finalName>azzimov-search</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
      </plugin>

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

    </plugins>

    <resources>
      <resource>
        <directory>src/clients</directory>
      </resource>
    </resources>
  </build>

我的web.xml是

<webapp>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/web-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/gw/*</url-pattern>
  </servlet-mapping>
</web-app>

我的web-config.xml是

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd ">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.project.book" />
</beans>

3 个答案:

答案 0 :(得分:2)

从例外情况来看,它显然是一个依赖问题。您可以尝试使用最新版本的spring-boot而不是1.5.8.RELEASE吗?

答案 1 :(得分:2)

您正在尝试将Spring Boot 1.5与Spring 5混合使用。由于它们不兼容,因此不会以这种方式工作。如果你真的需要Spring Framework 5,你应该使用Spring Boot 2.x启动程序父。

根据您的需要,您可以使用下面的pom.xml并将1.5.8.RELEASE更改为2.0.0.M5,或保持不变。

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

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
<build>
    <finalName>azzimov-search</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

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

    </plugins>

    <resources>
        <resource>
            <directory>src/clients</directory>
        </resource>
    </resources>
</build>

请注意,我从依赖项中删除了版本,因此它们来自父启动器。

另外,请删除web.xmlweb-config.xml,因为Spring Boot不需要它们。而是在根包中创建一个类(可能是com.project或com.project.book):

@SpringBootApplication
public class BookApp {

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

答案 2 :(得分:0)

不明确指定spring mvc或核心版本,它将自动采用1.5.8.RELEASE spring boot的兼容版本。