背景
最近在学习搭建项目时,出现一个问题,就是在给项目添加上下文名称时,启动报错。但是不添加时,启动成功。最后发现问题很简单,随手记录一下。
代码配置
报错信息
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘tomcatServletWebServerFactory’ defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ContextPath must start with ‘/’ and not end with ‘/’
问题原因
日志报错已经明确说明 ContextPath must start with ‘/’ and not end with ‘/’ 上下文必须已’/‘开始,不能已’/‘结束。 所以在上下文前面添加’/’,就解决了