java报错总结!!springboot3+vue2项目web服务层报错总结

java入门学习,随手记录一下开发过程中产生的报错,有些错误是网上搜索再加上自己尝试,随手引用了一些其他人的记录,也是留给自己看的,或是希望能对其他初学者有帮助

会持续更新!!!!!

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<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操作数据库,然后使用复杂查询条件中的关联查询。这里的字段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拦截器中

  1. 要写传参

  2. post对应的传参是data, get对应的传参是params

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SpringBoot+Vue2项目是一种前后端分离的开发模式,其中SpringBoot用于开发后端接口,Vue2用于开发前端页面。在这个项目中,可以使用SpringBootApplication注解来标识启动类,并通过@RestController注解来标识控制器类。\[1\] 在配置数据库时,可以在application.properties文件中添加相关配置,包括数据库驱动、URL、用户名和密码等信息。\[2\] 如果需要解决前后端跨域问题,可以在后端设置跨域配置,并将前端请求的baseURL属性值改为后台地址。这样就可以实现前后端的数据交互。\[3\] 总的来说,SpringBoot+Vue2项目是一种灵活、高效的开发模式,可以实现前后端的分离开发,并通过跨域配置实现数据的交互。 #### 引用[.reference_title] - *1* *2* [SDU项目实训——后台搭建——SpringBoot+Vue学习(二)](https://blog.csdn.net/m0_55633961/article/details/123504324)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [SpringBoot+Vue2项目解决前后端跨域方案](https://blog.csdn.net/zl5186888/article/details/126865950)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值