springboot2.0项目启动失败

springboot2.0项目启动失败

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

2020-12-25 16:21:20.175 [main] ERROR org.springframework.boot.SpringApplication - Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: Unable to locate the default servlet for serving static content. Please set the 'defaultServletName' property explicitly.

at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)

at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1179)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:571)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531)

at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:923)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588)

at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144)

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767)

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)

at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:326)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298)

at com.imooc.o2o.O2oApplication.main(O2oApplication.java:10)

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: Unable to locate the default servlet for serving static content. Please set the 'defaultServletName' property explicitly.

at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)

at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)

... 20 common frames omitted

Caused by: java.lang.IllegalStateException: Unable to locate the default servlet for serving static content. Please set the 'defaultServletName' property explicitly.

at org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler.setServletContext(DefaultServletHttpRequestHandler.java:111)

at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.enable(DefaultServletHandlerConfigurer.java:85)

at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.enable(DefaultServletHandlerConfigurer.java:71)

at com.imooc.o2o.config.web.MvcConfiguration.configureDefaultServletHandling(MvcConfiguration.java:54)

at org.springframework.web.servlet.config.annotation.WebMvcConfigurerComposite.configureDefaultServletHandling(WebMvcConfigurerComposite.java:74)

at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration.configureDefaultServletHandling(DelegatingWebMvcConfiguration.java:73)

at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:619)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)

... 21 common frames omitted



老师,项目启动失败,这是什么错呀?我web代码配置有什么问题吗?帮我看一下

package com.imooc.o2o.config.web;


import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import org.springframework.web.servlet.ViewResolver;

import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import org.springframework.web.servlet.view.InternalResourceViewResolver;


/**

 * 开启Mvc,自动注入spring容器。 WebMvcConfigurerAdapter:配置视图解析器

 * 当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean

 * 

 * @author xiangze

 *

 */

@SuppressWarnings("deprecation")

@Configuration

// 等价于<mvc:annotation-driven/>

@EnableWebMvc

public class MvcConfiguration extends WebMvcConfigurerAdapter implements ApplicationContextAware {

// Spring容器

private ApplicationContext applicationContext;


@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

this.applicationContext = applicationContext;

}


/**

* 静态资源配置

* @param registry

*/

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/");

// registry.addResourceHandler("/upload/**").addResourceLocations("file:/Users/baidu/work/image/upload/");

// registry.addResourceHandler("/upload/**")

//                .addResourceLocations("file:/D:/projectdev/image/upload/");

}


/**

* 定义默认的请求处理器

*/

@Override

public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

configurer.enable();

}


/**

* 创建viewResolver

* @return

*/

@Bean(name = "viewResolver")

public ViewResolver createViewResolver() {

InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

// 设置Spring 容器

viewResolver.setApplicationContext(this.applicationContext);

// 取消缓存

viewResolver.setCache(false);

// 设置解析的前缀

viewResolver.setPrefix("/WEB-INF/html/");

// 设置试图解析的后缀

viewResolver.setSuffix(".html");

return viewResolver;

}


/**

* 文件上传解析器

* @return

*/

@Bean(name = "multipartResolver")

public CommonsMultipartResolver createMultipartResolver() {

CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();

multipartResolver.setDefaultEncoding("utf-8");

// 1024 * 1024 * 20 = 20M

multipartResolver.setMaxUploadSize(20971520);

multipartResolver.setMaxInMemorySize(20971520);

return multipartResolver;

}


}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕阿满 2020-12-25 18:24:11

同学你好,问一下同学使用的是Spring boot2.x的版本吗?项目中的代码使用的Spring Boot 1.x的版本。Spring Boot 2.x和1.x之间有一些区别,这里建议同学先使用Spring boot 1.x,将项目完成后,再考虑项目升级。

另外从同学的另一个问答中看到同学已经启动项目了,问一下同学问题已经解决了吗?

祝学习愉快~

  • 提问者 霸气的少年 #1

    http://img1.sycdn.imooc.com//climg/5fe5e127095bb99c07600159.jpg

    问题解决了,在配置文件中加这段代码就行了,springboot2.0以上的都需要加上的

    2020-12-25 20:55:49
  • 好帮手慕阿满 回复 提问者 霸气的少年 #2

    同学解决了问题,棒棒哒。继续加油。祝学习愉快~

    2020-12-26 10:27:59
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师