springBoot异常处理

1.status=404 Whitelabel Error Page

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Nov 13 16:37:27 CST 2017
There was an unexpected error (type=Not Found, status=404).
No message available

刚开始学习,按照别人写的HelloWorld进行配置后,运行报出以上错误,检查Path后没有问题。

最后搜索发现问题出在 @SpringBootApplication的扫描范围:

 @SpringBootApplication的同级包及其子包会被扫描,因此Application需要放在整个系统的主路径下。

同时@RequestMapping(value="/listPort",method=RequestMethod.POST)这个也要写对,我将 value变为了name导致一直扫描不到。

同时不用使用项目名: http://127.0.0.1:8080/[annonationPath]

如果要加,则配置文件中添加 server.context-path=/项目名

  如果使用thymeleaf的templates,出现错误则注意pom.xml中是否有加入thymeleaf依赖:

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency>

 

2. @SpringBootApplication、SpringApplication、ModelAndView、EmbeddedServletContainerCustomizer多个类无法引入问题

刚开始使用springBoot的2.0.0 M6测试版,但引入有有些类或注解加载不进去(EmbeddedServletContainerCustomizer),可能大版本中有些类注解有变化所致,最后返回到1.5.8当前正式版后,出现以上错误,怎么Maven Update Project 都无法解决。最后网上有人说是jar包冲突导致。

直接删除掉Maven仓库中的org.springFramework,从新更新工程,让从新下载即可。

3. springboot 报错     没有主清单属性

      打包成jar文件直接运行报错。缺少主驱动程序,

  参照:https://blog.csdn.net/u010429286/article/details/79085212

         添加 maven-plugin驱动。

      <plugins>
        <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.jetty.mac.MacJetty.App</mainClass>
                </configuration>
                 <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
        </plugin>
      </plugins>

然后重新 

>mvn  clean package

>java -jar   ****.jar 

4.元素类型 "link" 必须由匹配的结束标记 "</link>" 终止

springboot 中引入thymeleaf,但是在页面上<link>没有结束标签,导致。

添加依赖:

<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.22</version>
</dependency>

在application.properties中修改:

spring.thymeleaf.mode=HTML5
->
spring.thymeleaf.mode=LEGACYHTML5

5. static静态文件访问不到404

 在配置文件application.properties中添加static的资源访问

spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static/

 6.org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'timestamp' cannot be found on object of type 'java.util.HashMap' - maybe not public?

在上传文件时,前台显示404,后端报错。

对于springboot的404并非一定是404没有找到资源地址,而是出错后不知怎么处理,找不到对应的error page 而报出来的404.

在上传文件时,springboot默认是1Mb的上限。同时如果是自己设置的临时路径,需要设置mkdir,要不然找不到文件路径。以上都有可能抛出此类异常

# file
spring.http.multipart.enabled=true
spring.http.multipart.location=file/tmp
spring.http.multipart.resolve-lazily=false
spring.http.multipart.max-file-size=8Mb
    /**
     * 临时文件存放地址设置
     * @return
     */
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        String location = System.getProperty("user.dir") + "/file/tmp";
        File tmpFile = new File(location);
        if (!tmpFile.exists()) {
            tmpFile.mkdirs();
        }
        factory.setLocation(location);
        return factory.createMultipartConfig();
    }

文件上传部分代码:

    /**
     * 文件读入
     */
    @RequestMapping("/file/upload")
    @ResponseBody
    public RespResult uploadFile(@RequestParam("file")MultipartFile file,Model model) {

 7. Caused by: org.xml.sax.SAXParseException: 前言中不允许有内容

  springboot+mybatis,运行启动时,注意查看于mybatis相关的配置文件和java文件。如果格式都正确,则需要查看application.properties,因为db的信息就在这里。

@PropertySource(value="classpath:./applicationTest.properties",encoding="UTF-8")

8. SpingBoot:Unregistering JMX-exposed beans on shutdown

 原因为:SpringBoot内置Tomcat没有正常启动,在pom.xml 中添加:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
</dependency>

 另一种情况是有jar下载失败

参见:https://www.cnblogs.com/QQ931697811/p/6707740.html

 9. Failed to start component [StandardEngine[Tomcat]]

因mvn依赖中的tomcat和servlet-api发生冲突导致,需要移除其中一个,或者在依赖中剔除掉Web 依赖。

注意在servelt-api3.0版本后变为  javax.servlet-api.同时在多个项目关联时,有时在项目互相引用时会出现冲突,需要引用jar包解决。即常见的 项目中运行报错,打包后正常。

<exclusions>
    <exclusion>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
    </exclusion>
</exclusions>

 9. AopProxyUtils.getSingletonTarget(Ljava/lang/Object;)Ljava/lang/Object;

springmvc和springboot包之间的冲突,两者只能选择一个。

 

原文地址:https://www.cnblogs.com/DennyZhao/p/7832193.html