零、前言
本次记录主要是项目的前后端对接时遇到的问题的处理,是比较常见的但是很重要的错误,故在此记录解决方案。
一、跨域错误
错误描述:
Access to fetch at ‘http://localhost:9090/user/list’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.
如何解决?
- 跨域问题通常在前端与后端在不同域名或端口间通信时发生。浏览器出于安全考虑,默认禁止这种跨域请求。
- @CrossOrigin:这是springboot提供的跨域请求注解
- 使用 CORS (跨域资源共享)
CORS 是一种允许服务器指定哪些域名可以访问资源的机制。你可以在服务器端配置 CORS 头来允许跨域请求。
二、Mybatis官网示例
https://mybatis.net.cn/getting-started.html
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springboot.mapper.UserMapper">
<insert id="save">
insert into user(name,username,age,sex,phone,address)
values(#{name},#{username},#{age},#{sex},#{phone},#{address})
</insert>
</mapper>
错误:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.springboot.mapper.UserMapper.listUsers
解决:配置mapper.xml
mybatis:
mapper-locations: classpath:mapper/*.xml
三、axios接口传递错误
Network Error at
XMLHttpRequest.handleError
(webpack-internal:///./node_module
错误原因:
“/user/delete”+id改为"/user/delete/“+id
具体解释就是将请求参数放在url的话要在”/"之后
查看错误原因代码:
this.catch((err) => {
console.log(err)
})
四、常见的空引用错误
java.lang.IllegalArgumentException: Source must not be null
错误原因:写代码时没考虑异常情况;