• 编写Spring项目中可能报的错误


    1.浏览器的错误列表

    1. 404
    • 访问静态资源文件时:
      • 检查浏览器中请求的路径是否正确
      • 检查文件名是否正确
      • 检查文件位置是否正确
      • 如果以上全部正确仍然404 需要重新编译工程(Build->ReBuild)
    • 访问动态资源时:
      • 检查请求的路径是否正确
      • 检查Controller里面RequestMapping注解里面的处理路径是否正确
      • 检查controller包的位置是否在工程自带的包里面
      • 检查@Controller注解是否添加
      • 如果以上全部正确仍然404 需要重新编译工程(Build->ReBuild)
    1. 400错误码 : 客户端给服务器传递参数时参数类型转换错误
    2. 500错误码: 代表服务器业务代码出错,此时需要看idea里面的报错

    2.后端服务器可能报的错误

    2.1使用Mybatis可能报的错误

    在Spring Boot项目中,当需要使用Mybatis时,需要添加相关的依赖:

    1. <dependency>
    2. <groupId>mysqlgroupId>
    3. <artifactId>mysql-connector-javaartifactId>
    4. <scope>runtimescope>
    5. dependency>
    6. <dependency>
    7. <groupId>org.mybatis.spring.bootgroupId>
    8. <artifactId>mybatis-spring-boot-starterartifactId>
    9. <version>2.2.2version>
    10. dependency>

    当添加以上依赖项后,如果启动项目,会提示以下错误:

    1. ***************************
    2. APPLICATION FAILED TO START
    3. ***************************
    4. Description:
    5. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
    6. Reason: Failed to determine a suitable driver class

    因为Spring Boot启动时,如果检测到当前已经添加数据库编程的依赖项,会自动读取连接数据库的配置信息,由于目前尚未配置这些信息,所以,启动会报错!

    所以,需要在application.properties中添加配置:

    1. # 连接数据库的参数
    2. spring.datasource.url=jdbc:mysql://localhost:3306/mall_pms?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    3. spring.datasource.username=root
    4. spring.datasource.password=root

    由于Spring Boot在启动项目时只会读取以上配置并应用,并不会实际的连接数据库,所以,即使以上配置值是错误的,启动项目时并不会报告错误!

    可以在src/test/java的根包下的测试类中进行测试连接:

    1. @SpringBootTest
    2. class CsmallProductApplicationTests {
    3. @Test
    4. void contextLoads() {
    5. }
    6. @Autowired
    7. DataSource dataSource;
    8. @Test
    9. void testConnection() throws Exception {
    10. dataSource.getConnection();
    11. }
    12. }

    当配置的URL错误(含主机名错误、端口号错误),或MySQL未启动时,将出现以下错误:

    1. com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
    2. The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

    如果数据库名称错误,或无此数据库,将出现以下错误:

    java.sql.SQLSyntaxErrorException: Unknown database 'mall_pmsxzxxxxx'
    

    如果用户或密码错误,将出现以下错误:

    java.sql.SQLException: Access denied for user 'rootx'@'localhost' (using password: YES)
    
    java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
    
    java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)

    然后,应该及时测试以上功能是否正确,可以在src/test/java下的根包下创建mapper.AlbumMapperTests测试类,

    1. package cn.tedu.csmall.product.mapper;
    2. import cn.tedu.csmall.product.pojo.entity.Album;
    3. import org.junit.jupiter.api.Test;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.boot.test.context.SpringBootTest;
    6. @SpringBootTest
    7. public class AlbumMapperTests {
    8. @Autowired
    9. AlbumMapper mapper;
    10. @Test
    11. void testInsert() {
    12. Album album = new Album();
    13. album.setName("某电视的相册");
    14. album.setDescription("某电视的相册的描述");
    15. album.setSort(63);
    16. int rows = mapper.insert(album);
    17. System.out.println("rows = " + rows);
    18. }
    19. }

    在执行测试时,如果此前配置的@MapperScan有误,会出现如下错误:

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cn.tedu.csmall.product.mapper.AlbumMapperTests': Unsatisfied dependency expressed through field 'mapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.tedu.csmall.product.mapper.AlbumMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    

    如果在XML文件中,namespace属性值配置有误,或者节点的id属性值配置有误,或者在application.properties中没有正确的配置mybatis.mapper-locations属性,都将出现以下错误:

    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.tedu.csmall.product.mapper.AlbumMapper.insert

     

    如果既没有配置resultType又没有配置resultMap,将会出现以下错误:

    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'c

     

    当在上使用了resultMap,取值错误时(例如取值为类型的全限定名),将出现以下错误:

    java.lang.IllegalArgumentException: Result Maps collection does not contain value for cn.tedu.csmall.product.pojo.vo.BrandStandardVO

     

  • 相关阅读:
    基于PHP企业公司网站系统设计与实现 开题报告
    输入重启gerrit容器命令后,gerrit起不来了什么原因
    【软考软件评测师】第十四章 白盒测试基础
    最详细STM32,cubeMX 按键点亮 led
    ubuntu设置sudo免密
    selenium框架操作stealth.min.js文件隐藏浏览器指纹特征
    406. 根据身高重建队列
    Composition API 前提
    下载git
    WoShop跨境电商源码支持多语言和多货币吗?
  • 原文地址:https://blog.csdn.net/TheNewSystrm/article/details/126108139