java報錯之springboot3+vue2項目web服務層報錯總結
java報錯之springboot3+vue2項目web服務層報錯
會持續(xù)更新?。。。?!
1、Autowired members must be defined in valid Spring bean
自動注入對象必須定義在有效的spring bean內(nèi),也就是說只有本身作為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 不能解析構造函數(shù)
檢查構造函數(shù)傳參的順序和數(shù)量是否正確
6.SpringBoot項目啟動失敗報錯
Annotation-specified bean name ‘xx‘ for bean class [xxx] conflicts with existing
有同名的文件,搜索找到同名文件,無用刪除,有用的話就改名。找不到的話可能是緩存,清除idea緩存。
7.post上傳文件功能,接口保存數(shù)據(jù)時報錯
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
由于數(shù)據(jù)庫建立時候沒有分配好字符的大小
查看數(shù)據(jù)庫中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類型數(shù)據(jù),刪除@RequestBody,改成@RequestParam
@RequestBody
@RequestBody用來接收前端傳遞給后端的json字符串中的數(shù)據(jù),GET方式的請求一般通過URL中攜帶key-value參數(shù),而@RequestBody接收的是請求體中的數(shù)據(jù)(json格式的數(shù)據(jù),只有請求體中能保存json),所以使用@RequestBody接收數(shù)據(jù)的時候必須是POST方式等方式。
@RequestBody與@RequestParam()可以同時使用,但@RequestBody最多只能有一個,而@RequestParam()可以多個。
@RequestParam
@RequestParam用來處理 Content-Type 為 application/x-www-form-urlencoded 編碼的內(nèi)容,Content-Type默認為該屬性。
可以用于接收URL中的參數(shù)并捆綁到方法的參數(shù)中,也可以接受post請求體中的Content-Type 為 application/x-www-form-urlencoded的數(shù)據(jù)。(post比較常用的是json格式數(shù)據(jù))
@RequestParam(value=”參數(shù)名”,required=”true/false”,defaultValue=””)
value:參數(shù)的keyrequired:是否為必須,請求中必須包含該參數(shù),如果不包含就報錯。defaultValue:代替的默認參數(shù)值,設置后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)
根據(jù)請求頭不同進行不同的邏輯操作
@PutMapping("/devices/{id}")
public Response<Object> getList(@PathVariable Integer id, @RequestHeader(value = "Modify-Content", require = false) String modifyMark, HttpServletRequest request) {
String info = "info";
if(Object.equals(modifyMark, info)) {
DeviceInfoModifyVO.setDeviceName(request.getParameter("deviceName"))
}else {
}
}11.Unable to locate Attribute with the the given name [x] on this ManagedType
在Spring中使用JPA操作數(shù)據(jù)庫,然后使用復雜查詢條件中的關聯(lián)查詢。這里的字段userOpenId是WeekSummary實體中的字段,在User表中,并不是叫這個,所以在查詢的時候就找不到這個字段。
把userOpenId字段改成聯(lián)表中的字段名稱
12.ailed to parse multipart servlet request
配置路徑未生效或不存在,選擇一個已經(jīng)存在的路徑
- 配置地址為服務器肯定會存在的路徑
- 程序中配置路徑前首先檢查有沒有此路徑
fileUtil.saveToFile(file, filepath)
如果是相對路徑,存放在項目的路徑下的文件夾下
13.Required request body is missing: public
檢查vue的request.js攔截器中
- 要寫傳參
- post對應的傳參是data, get對應的傳參是params
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot定制三種錯誤頁面及錯誤數(shù)據(jù)方法示例
Spring Boot提供的默認異常處理機制通常并不一定適合我們實際的業(yè)務場景,因此,我們通常會根據(jù)自身的需要對Spring Boot全局異常進行統(tǒng)一定制,例如定制錯誤頁面,定制錯誤數(shù)據(jù)等。本文主要介紹了SpringBoot三種自定義錯誤頁面的實現(xiàn),快來學習吧2021-12-12

