会持续更新!!!!!
1、Autowired members must be defined in valid Spring bean
自动注入对象必须定义在有效的spring bean内,也就是说只有本身作为bean的类才能注入其他对象。
2、‘PageRequest(int, int, org.springframework.data.domain.Sort)’ has protected access in ‘org.springframework.data.domain.PageRequest’
使用Pageable page = new PageRequest(1, 1, Sort.Direction.ASC) 报错
由于springboot2.2.1以上版本PageRequest不能在实例化,改用
Pageable page = PageRequest.of(1, 1, Sort.Direction.ASC)
如何写java单元测试
引入了spring-boot-starter-test的话,就包含了mockito的依赖了。
3、Junit测试Gradle项目时报错:No tests found for given includes: xxxx.xxxxTest**
gradle设置的run test running改为IDEA
4.postman测试接口报错:For queries with named parameters you need to provide names for method parameters.
gradle设置的build and run using 设置为gradle
5.java Cannot resolve constructor 不能解析构造函数
检查构造函数传参的顺序和数量是否正确
6.SpringBoot项目启动失败报错Annotation-specified bean name ‘xx‘ for bean class [xxx] conflicts with existing
有同名的文件,搜索找到同名文件,无用删除,有用的话就改名。找不到的话可能是缓存,清除idea缓存。
7.post上传文件功能,接口保存数据时报错:could not execute statement; SQL [n/a]; constraint [null];
接口方法没有@RequestBody
解决:
添加@RequestBody
8.post上传文件,使用postman测试报错:Data truncation: Data too long for column ‘xxx’ at row 1
由于数据库建立时候没有分配好字符的大小
查看数据库中xxx字段的长度,增加该长度
9.post上传文件,使用postman测试报错:Content type ‘multipart/form-data;boundary=----------0467042;charset=UTF-8‘ not supported
不支持’multipart/form-data;boundary=-------------------------036764477110441760467042;charset=UTF-8’请求类型。
注解 支持的类型 支持的请求类型 支持的Content-Type 请求示例
@PathVariable url GET 所有 /test/{id}
@RequestParam url GET 所有 /test?id=1
Body POST/PUT/DELETE/PATCH form-data或x-www.form-urlencoded id:1
@RequestBody Body POST/PUT/DELETE/PATCH json {"id":1}
由于传递formData类型数据,删除@RequestBody,改成@RequestParam
@RequestBody
@RequestBody用来接收前端传递给后端的json字符串中的数据,GET方式的请求一般通过URL中携带key-value参数,而@RequestBody接收的是请求体中的数据(json格式的数据,只有请求体中能保存json),所以使用@RequestBody接收数据的时候必须是POST方式等方式。
@RequestBody与@RequestParam()可以同时使用,但@RequestBody最多只能有一个,而@RequestParam()可以多个。
@RequestParam
@RequestParam用来处理 Content-Type 为 application/x-www-form-urlencoded 编码的内容,Content-Type默认为该属性。
可以用于接收URL中的参数并捆绑到方法的参数中,也可以接受post请求体中的Content-Type 为 application/x-www-form-urlencoded的数据。(post比较常用的是json格式数据)
@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””)
value:参数的key
required:是否为必须,请求中必须包含该参数,如果不包含就报错。
defaultValue:代替的默认参数值,设置后required将自动置false
@PostMapping("/upload")
public ReturnResult upload(
@RequestHeader(value = REQUEST_HEADER_TOKEN) String token,
@Validated @RequestBody UploadDto uploadDto,
BindingResult bindingResult) {
BindingParamUtil.checkParam(bindingResult);
TokenProperties tokenProperties = jwtTokenProvider.parseToken(token);
ReturnResult upload = fileService.upload(uploadDto, tokenProperties);
return upload;
}
改成
@PostMapping("/upload")
public ReturnResult upload(
@RequestHeader(value = REQUEST_HEADER_TOKEN) String token,
@RequestParam("file") MultipartFile file,
@RequestParam("fileType") String fileType) {
TokenProperties tokenProperties = jwtTokenProvider.parseToken(token);
UploadDto uploadDto = new UploadDto(file, fileType);
ReturnResult upload = fileService.upload(uploadDto, tokenProperties);
return upload;
}
10.Executing an update/delete query
在方法上添加注解@Modifying,并且需要在类或是方法上加上事务注解@Transactional(rollbackFor = Exception.class)
4.根据请求头不同进行不同的逻辑操作
@PutMapping("/devices/{id}")
public Response
11.Unable to locate Attribute with the the given name [x] on this ManagedType
在Spring中使用JPA操作数据库,然后使用复杂查询条件中的关联查询。这里的字段userOpenId是WeekSummary实体中的字段,在User表中,并不是叫这个,所以在查询的时候就找不到这个字段。把userOpenId字段改成联表中的字段名称
12.ailed to parse multipart servlet request
配置路径未生效或不存在,选择一个已经存在的路径
1、配置地址为服务器肯定会存在的路径
2、程序中配置路径前首先检查有没有此路径
fileUtil.saveToFile(file, filepath)
如果是相对路径,存放在项目的路径下的文件夹下
13.Required request body is missing: public
检查vue的request.js拦截器中
要写传参
post对应的传参是data, get对应的传参是params