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ò)
會(huì)持續(xù)更新?。。。?!
1、Autowired members must be defined in valid Spring bean
自動(dòng)注入對(duì)象必須定義在有效的spring bean內(nèi),也就是說(shuō)只有本身作為bean的類(lèi)才能注入其他對(duì)象。
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)
如何寫(xiě)java單元測(cè)試
引入了spring-boot-starter-test的話(huà),就包含了mockito的依賴(lài)了。
3、Junit測(cè)試Gradle項(xiàng)目時(shí)報(bào)錯(cuò)
No tests found for given includes: xxxx.xxxxTest**
gradle設(shè)置的run test running改為IDEA
4.postman測(cè)試接口報(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
有同名的文件,搜索找到同名文件,無(wú)用刪除,有用的話(huà)就改名。找不到的話(huà)可能是緩存,清除idea緩存。
7.post上傳文件功能,接口保存數(shù)據(jù)時(shí)報(bào)錯(cuò)
could not execute statement; SQL [n/a]; constraint [null];
接口方法沒(méi)有@RequestBody
- 解決:
- 添加@RequestBody
8.post上傳文件,使用postman測(cè)試報(bào)錯(cuò)
Data truncation: Data too long for column ‘xxx’ at row 1
由于數(shù)據(jù)庫(kù)建立時(shí)候沒(méi)有分配好字符的大小
查看數(shù)據(jù)庫(kù)中xxx字段的長(zhǎng)度,增加該長(zhǎng)度
9.post上傳文件,使用postman測(cè)試報(bào)錯(cuò)
Content type ‘multipart/form-data;boundary=----------0467042;charset=UTF-8‘ not supported
不支持’multipart/form-data;boundary=-------------------------036764477110441760467042;charset=UTF-8’請(qǐng)求類(lèi)型。
注解 支持的類(lèi)型 支持的請(qǐng)求類(lèi)型 支持的Content-Type 請(qǐng)求示例
@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類(lèi)型數(shù)據(jù),刪除@RequestBody,改成@RequestParam
@RequestBody
@RequestBody用來(lái)接收前端傳遞給后端的json字符串中的數(shù)據(jù),GET方式的請(qǐng)求一般通過(guò)URL中攜帶key-value參數(shù),而@RequestBody接收的是請(qǐng)求體中的數(shù)據(jù)(json格式的數(shù)據(jù),只有請(qǐng)求體中能保存json),所以使用@RequestBody接收數(shù)據(jù)的時(shí)候必須是POST方式等方式。
@RequestBody與@RequestParam()可以同時(shí)使用,但@RequestBody最多只能有一個(gè),而@RequestParam()可以多個(gè)。
@RequestParam
@RequestParam用來(lái)處理 Content-Type 為 application/x-www-form-urlencoded 編碼的內(nèi)容,Content-Type默認(rèn)為該屬性。
可以用于接收URL中的參數(shù)并捆綁到方法的參數(shù)中,也可以接受post請(qǐng)求體中的Content-Type 為 application/x-www-form-urlencoded的數(shù)據(jù)。(post比較常用的是json格式數(shù)據(jù))
@RequestParam(value=”參數(shù)名”,required=”true/false”,defaultValue=””)
value:參數(shù)的keyrequired:是否為必須,請(qǐng)求中必須包含該參數(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,并且需要在類(lèi)或是方法上加上事務(wù)注解
@Transactional(rollbackFor = Exception.class)
根據(jù)請(qǐng)求頭不同進(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ù)庫(kù),然后使用復(fù)雜查詢(xún)條件中的關(guān)聯(lián)查詢(xún)。這里的字段userOpenId是WeekSummary實(shí)體中的字段,在User表中,并不是叫這個(gè),所以在查詢(xún)的時(shí)候就找不到這個(gè)字段。
把userOpenId字段改成聯(lián)表中的字段名稱(chēng)
12.ailed to parse multipart servlet request
配置路徑未生效或不存在,選擇一個(gè)已經(jīng)存在的路徑
- 配置地址為服務(wù)器肯定會(huì)存在的路徑
- 程序中配置路徑前首先檢查有沒(méi)有此路徑
fileUtil.saveToFile(file, filepath)
如果是相對(duì)路徑,存放在項(xiàng)目的路徑下的文件夾下
13.Required request body is missing: public
檢查vue的request.js攔截器中
- 要寫(xiě)傳參
- post對(duì)應(yīng)的傳參是data, get對(duì)應(yīng)的傳參是params
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot接口如何多次獲取request中的body內(nèi)容
這篇文章主要介紹了springboot接口多次獲取request中的body內(nèi)容的過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring根據(jù)XML配置文件 p名稱(chēng)空間注入屬性的實(shí)例
下面小編就為大家分享一篇Spring根據(jù)XML配置文件 p名稱(chēng)空間注入屬性的實(shí)例,具有很好的參考價(jià)值。希望對(duì)大家有所幫助2017-11-11
Spring如何基于Proxy及cglib實(shí)現(xiàn)動(dòng)態(tài)代理
這篇文章主要介紹了Spring如何基于Proxy及cglib實(shí)現(xiàn)動(dòng)態(tài)代理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Mybatis嵌套子查詢(xún)動(dòng)態(tài)SQL編寫(xiě)實(shí)踐
這篇文章主要介紹了Mybatis嵌套子查詢(xún)動(dòng)態(tài)SQL編寫(xiě)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05
Java?事務(wù)注解@Transactional回滾(try?catch、嵌套)問(wèn)題
這篇文章主要介紹了Java?@Transactional回滾(try?catch、嵌套)問(wèn)題,Spring?事務(wù)注解?@Transactional?本來(lái)可以保證原子性,如果事務(wù)內(nèi)有報(bào)錯(cuò)的話(huà),整個(gè)事務(wù)可以保證回滾,但是加上try?catch或者事務(wù)嵌套,可能會(huì)導(dǎo)致事務(wù)回滾失敗2022-08-08

