10、用户登录报空指针异常
报错信息:java.lang.NullPointerException: null
at com.bobooy.ems.controller.UserController.login(UserController.java:25)。。。。。
分析:报错原因,用户名填写错误会导致从数据库中通过用户名查找用户信息会查不到这个用户,所以在user.getUserName()就会报空指针异常
解决办法:在Controller中添加一个对用户是否为空的判断
11、Mybatis 多对一 结果映射失败
报错:ERROR 25928 --- [io-8080-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'department' of 'class com.bobooy.ems.pojo.Employee' with value 'Department(id=101, departmentName=技术部)' Cause: java.lang.IllegalArgumentException: argument type mismatch] with root cause
大概意思:在结果映射时属性和结果的类型不匹配,无法进行结果集映射
解决方式:修改Department这个关联属性的类型
原类型:
修改为:Department类型
12、localhost 将您重定向的次数过多
网页报错:该网页无法正常运作 localhost 将您重定向的次数过多。尝试清除 Cookie ERR_TOO_MANY_REDIRECTS
报错起因:设置了登录拦截之后,登录发生上述异常。
报错根本原因:在设置了登录拦截器并且判断可以放行的时候再次使用了重定向到主页面,导致登录拦截有一次重定向到主页,正常登录也有一次重定向到主页,所以两者发生了冲突,代码如下:
- 登陆拦截器
- 正常登录(UserController)
解决方式:删除登录拦截的重定向代码即可
13、查询Department 可以获取id 不能获取 部门名称 departmentName
测试查看:
分析原因:实体类中Department的属性部门名为 departmentName
而数据库中部门名字段为department_Name
,这就会导致结果映射不成功
解决方式:
方式一:查询语句改为
select `id`,`department_name` as departmentName from department;
方式二:开启 Mybatis 驼峰命名映射 在application.yaml文件中配置 注意空格
configuration: map-underscore-to-camel-case: true #开启驼峰命名
14、前端传给后端数据类型是对象的时候,会自动转为字符串String类型
报错:
WARN 25360 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved
[org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1
errors<EOL>Field error in object 'employee' on field 'department': rejected value [101]; codes
[typeMismatch.employee.department,typeMismatch.department,typeMismatch.com.bobooy.ems.pojo.Department,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes
[employee.department,department]; arguments []; default message [department]]; default message [Failed to convert
property value of type 'java.lang.String' to required type 'com.bobooy.ems.pojo.Department' for property
'department'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String'
to required type 'com.bobooy.ems.pojo.Department' for property 'department': no matching editors or conversion
strategy found]]
Cannot convert value of type 'java.lang.String' to required type 'com.bobooy.ems.pojo.Department' for property 'department'
大致意思:不能将字符类型转换为Department类型
错误来源:
数据库表多对一进行 添加员工 的时候,前端传department给后端,但后端报错类型不匹配
- 前端如下:
- 后端接收
解决方式:前端传一个int类型的值,将 department 换成 department.id,这样这个数据传入到后端,数据类型是String通过Mybatis将参数自动转换为int类型,就不会出现类型不匹配的问题
补充:后端可以将复杂的数据类型(如:数组,对象,集合)传给前端并完好的接收,但前端传给后端的只能是字符串(这里抛开使用JSON进行前后端数据交互)
15、使用PostMapping和DeleteMapping处理删除删除数据的请求报错405
报错:网页报错 405(There was an unexpected error (type=Method Not Allowed, status=405).)
代码:
- 前端:
- 后端
分析原因:
1.通过网上查阅资料,SpringBoot2.0以上版本没有启用HiddenHttpMethodFilter
过滤器,不能将post转换为delete请求,需要在主配置文件中开启过滤器
2.但本次核心错误原因是前端使用a标签href链接发送的是Get请求,所以后端处理请求只能使用GetMapping或者RequestMapping
第一个问题的解决方式:配置文件中添加如下配置
spring.mvc.hiddenmethod.filter.enabled=true
第二个问题的解决方式(本次核心问题):
将controller使用GetMapping或者RequestMapping来处理请求