java報(bào)錯(cuò)之springboot3+vue2項(xiàng)目web服務(wù)層報(bào)錯(cuò)總結(jié)
java報(bào)錯(cuò)之springboot3+vue2項(xiàng)目web服務(wù)層報(bào)錯(cuò)
會持續(xù)更新!?。。。?/h3>
1、Autowired members must be defined in valid Spring bean
自動(dòng)注入對象必須定義在有效的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) 報(bào)錯(cuò)
由于springboot2.2.1以上版本PageRequest不能在實(shí)例化,改用
Pageable page = PageRequest.of(1, 1, Sort.Direction.ASC)
如何寫java單元測試
引入了spring-boot-starter-test的話,就包含了mockito的依賴了。
3、Junit測試Gradle項(xiàng)目時(shí)報(bào)錯(cuò)
No tests found for given includes: xxxx.xxxxTest**
gradle設(shè)置的run test running改為IDEA
4.postman測試接口報(bào)錯(cuò)
For queries with named parameters you need to provide names for method parameters.
gradle設(shè)置的build and run using 設(shè)置為gradle
5.java Cannot resolve constructor 不能解析構(gòu)造函數(shù)
檢查構(gòu)造函數(shù)傳參的順序和數(shù)量是否正確
6.SpringBoot項(xiàng)目啟動(dòng)失敗報(bào)錯(cuò)
Annotation-specified bean name ‘xx‘ for bean class [xxx] conflicts with existing
有同名的文件,搜索找到同名文件,無用刪除,有用的話就改名。找不到的話可能是緩存,清除idea緩存。
7.post上傳文件功能,接口保存數(shù)據(jù)時(shí)報(bào)錯(cuò)
could not execute statement; SQL [n/a]; constraint [null];
接口方法沒有@RequestBody
- 解決:
- 添加@RequestBody
8.post上傳文件,使用postman測試報(bào)錯(cuò)
Data truncation: Data too long for column ‘xxx’ at row 1
由于數(shù)據(jù)庫建立時(shí)候沒有分配好字符的大小
查看數(shù)據(jù)庫中xxx字段的長度,增加該長度
9.post上傳文件,使用postman測試報(bào)錯(cuò)
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ù)的時(shí)候必須是POST方式等方式。
@RequestBody與@RequestParam()可以同時(shí)使用,但@RequestBody最多只能有一個(gè),而@RequestParam()可以多個(gè)。
@RequestParam
@RequestParam用來處理 Content-Type 為 application/x-www-form-urlencoded 編碼的內(nèi)容,Content-Type默認(rèn)為該屬性。
可以用于接收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ù),如果不包含就報(bào)錯(cuò)。defaultValue
:代替的默認(rèn)參數(shù)值,設(shè)置后required將自動(dòng)置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,并且需要在類或是方法上加上事務(wù)注解
@Transactional(rollbackFor = Exception.class)
根據(jù)請求頭不同進(jìn)行不同的邏輯操作
@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ù)庫,然后使用復(fù)雜查詢條件中的關(guān)聯(lián)查詢。這里的字段userOpenId是WeekSummary實(shí)體中的字段,在User表中,并不是叫這個(gè),所以在查詢的時(shí)候就找不到這個(gè)字段。
把userOpenId字段改成聯(lián)表中的字段名稱
12.ailed to parse multipart servlet request
配置路徑未生效或不存在,選擇一個(gè)已經(jīng)存在的路徑
- 配置地址為服務(wù)器肯定會存在的路徑
- 程序中配置路徑前首先檢查有沒有此路徑
fileUtil.saveToFile(file, filepath)
如果是相對路徑,存放在項(xiàng)目的路徑下的文件夾下
13.Required request body is missing: public
檢查vue的request.js攔截器中
- 要寫傳參
- post對應(yīng)的傳參是data, get對應(yīng)的傳參是params
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot定制三種錯(cuò)誤頁面及錯(cuò)誤數(shù)據(jù)方法示例
Spring Boot提供的默認(rèn)異常處理機(jī)制通常并不一定適合我們實(shí)際的業(yè)務(wù)場景,因此,我們通常會根據(jù)自身的需要對Spring Boot全局異常進(jìn)行統(tǒng)一定制,例如定制錯(cuò)誤頁面,定制錯(cuò)誤數(shù)據(jù)等。本文主要介紹了SpringBoot三種自定義錯(cuò)誤頁面的實(shí)現(xiàn),快來學(xué)習(xí)吧2021-12-12Mybatis詳解在注解sql時(shí)報(bào)錯(cuò)的解決方法
MyBatis-Plus 是一個(gè) Mybatis 增強(qiáng)版工具,在 MyBatis 上擴(kuò)充了其他功能沒有改變其基本功能,為了簡化開發(fā)提交效率而存在,本篇文章帶你看看在注解sql時(shí)所報(bào)出的錯(cuò)誤解決2022-03-03