java報錯之springboot3+vue2項目web服務層報錯總結
java報錯之springboot3+vue2項目web服務層報錯
會持續(xù)更新!?。。?!
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
:參數的keyrequired
:是否為必須,請求中必須包含該參數,如果不包含就報錯。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)
根據請求頭不同進行不同的邏輯操作
@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操作數據庫,然后使用復雜查詢條件中的關聯(lián)查詢。這里的字段userOpenId是WeekSummary實體中的字段,在User表中,并不是叫這個,所以在查詢的時候就找不到這個字段。
把userOpenId字段改成聯(lián)表中的字段名稱
12.ailed to parse multipart servlet request
配置路徑未生效或不存在,選擇一個已經存在的路徑
- 配置地址為服務器肯定會存在的路徑
- 程序中配置路徑前首先檢查有沒有此路徑
fileUtil.saveToFile(file, filepath)
如果是相對路徑,存放在項目的路徑下的文件夾下
13.Required request body is missing: public
檢查vue的request.js攔截器中
- 要寫傳參
- post對應的傳參是data, get對應的傳參是params
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。