Nullpointer exception thrown when running Sprint Boot with Spring MVC

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

I am building spring mvc project with spring boot but I am unable to start my web application . I have followed the couple of tutorials but cant figure it out. I could start my application with spring boot but when i add Spring mvc, it starts to throw an error. I am getting a following error

java.lang.NullPointerException: null at org.springframework.core.BridgeMethodResolver.findBridgedMethod(BridgeMethodResolver.java:60) at org.springframework.beans.GenericTypeAwarePropertyDescriptor.(GenericTypeAwarePropertyDescriptor.java:70) at org.springframework.beans.CachedIntrospectionResults.buildGenericTypeAwarePropertyDescriptor(CachedIntrospectionResults.java:366) at org.springframework.beans.CachedIntrospectionResults.(CachedIntrospectionResults.java:302) at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:189) at org.springframework.beans.BeanUtils.getPropertyDescriptors(BeanUtils.java:354) at org.springframework.boot.bind.PropertiesConfigurationFactory.getNames(PropertiesConfigurationFactory.java:288) at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:269) at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240) at org.springframework.boot.context.config.ConfigFileApplicationListener.bindToSpringApplication(ConfigFileApplicationListener.java:241) at org.springframework.boot.context.config.ConfigFileApplicationListener.postProcessEnvironment(ConfigFileApplicationListener.java:197) at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:182) at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:168) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74) at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:325) at org.springframework.boot.SpringApplication.run(SpringApplication.java:296) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) at 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) at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:527) at java.lang.Thread.run(Thread.java:745) [WARNING] 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) at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:527) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.NullPointerException at org.springframework.core.BridgeMethodResolver.findBridgedMethod(BridgeMethodResolver.java:60) at org.springframework.beans.GenericTypeAwarePropertyDescriptor.(GenericTypeAwarePropertyDescriptor.java:70) at org.springframework.beans.CachedIntrospectionResults.buildGenericTypeAwarePropertyDescriptor(CachedIntrospectionResults.java:366) at org.springframework.beans.CachedIntrospectionResults.(CachedIntrospectionResults.java:302) at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:189) at org.springframework.beans.BeanUtils.getPropertyDescriptors(BeanUtils.java:354) at org.springframework.boot.bind.PropertiesConfigurationFactory.getNames(PropertiesConfigurationFactory.java:288) at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:269) at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240) at org.springframework.boot.context.config.ConfigFileApplicationListener.bindToSpringApplication(ConfigFileApplicationListener.java:241) at org.springframework.boot.context.config.ConfigFileApplicationListener.postProcessEnvironment(ConfigFileApplicationListener.java:197) at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:182) at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:168) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74) at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:325) at org.springframework.boot.SpringApplication.run(SpringApplication.java:296) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) at com.azzimov.search.BootStarter.StartApplication.main(StartApplication.java:17) ... 6 more

My pom.xml is

<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> 

My web.xml is

<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> 

My web-config.xml is

<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> 

回答1:

From the exception, its apparently a dependency issue. Can you try using a latest version of spring-boot instead of using 1.5.8.RELEASE?



回答2:

You are trying to mix Spring Boot 1.5 with Spring 5. This won't work this way as they are not compatible. If you really need Spring Framework 5 you should use Spring Boot 2.x starter parent.

Depending on your needs you can take the pom.xml below and change 1.5.8.RELEASE to 2.0.0.M5, or leave it as is.

<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> 

Note that I removed versions from your dependencies so they are taken from parent starter.

Also, remove your web.xml and web-config.xml as they are not necessary for Spring Boot. Instead create a class in your root package (presumably com.project, or com.project.book):

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


回答3:

Do not explicitly specify spring mvc or core versions, it will automatically take the compatible version for 1.5.8.RELEASE spring boot.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!