2017-10-11 27 views
1

我將Spring Framework 4.3.9.RELEASE更新爲5.0.0.RELEASE,令我驚訝的是,自從5.0版本以來,WebMvcConfigurerAdapter類已被標記爲不推薦使用。正如文檔所述:將Spring遷移到5.0.0替換爲不推薦使用的WebMvcConfigurerAdapter是錯誤的

Deprecated.

as of 5.0 WebMvcConfigurer has default methods (made possible by a Java 8 baseline) and can be implemented directly without the need for this adapter

我試圖改變類定義來實現WebMvcConfigurer

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages = "net.nichar.app") 
public class WebConfiguration extends WebMvcConfigurerAdapter { 

    @Override 
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 
     configurer.enable(); 
    } 

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

    @Override 
    public void addCorsMappings(CorsRegistry registry) { 
     registry.addMapping("/**") 
       .allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH"); 
    } 
} 

我導入使用Maven以下文物,這些都是同一版本:

  • 彈簧核心
  • 彈簧網
  • 彈簧webmvc
  • 彈簧ORM

雖然我也跟着春天doumention,我必須做錯事,因爲一旦我嘗試運行應用程序,就會拋出一個錯誤:

2017-10-11 22:42:03 [localhost-startStop-1] WARN Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException

Oct 11, 2017 10:42:03 ODP. org.apache.catalina.core.StandardContext listenerStart SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException at ......

你能幫我解決嗎?隨意需要更多的細節。

回答

2

此問題與WebMvcConfigurer無關(您應該從問題中刪除該部分,因爲它會誤導他人)。

的異常點,傑克遜,你最有可能使用不正確的傑克遜版本:

java.lang.NoClassDefFoundError: 
com/fasterxml/jackson/databind/exc/InvalidDefinitionException 

Spring框架5,要求傑克遜2.9+,as stated in the migration documentation

+0

謝謝,我會試試看。我對WebMvcConfigurer的假設基於WebMvcConfigurerAdapter棄用。這就是我的困惑已經開始。 –

相關問題