解决 Resolved [org.springframework.web.HttpMediaTypeNotSupportedException

报错如下:

在这里插入图片描述


使用场景:

上传文件同时要传递参数用实体类接收


原因分析:

@RequestParam和@RequestBody一起使用了

解决办法:
采用@RequestParam接收就行,前端和后台数据对应
(小编使用的是element ui 上传组件)

前端如图:
在这里插入图片描述
后台接收-如图:
在这里插入图片描述
MyVo的代码:
在这里插入图片描述
这样上传图片并且能使用实体类接收参数

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'xxx' not supported] 这个异常表示Spring MVC在处理请求时不支持指定的内容类型。 在Spring MVC中,我们可以通过@RequestMapping注解来指定接受的内容类型,例如: ```java @RestController @RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_VALUE) public class ApiController { @GetMapping(value = "/data", consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<String> getData(@RequestBody DataRequest requestData) { // 处理数据请求 return ResponseEntity.ok("success"); } } ``` 在上面的代码中,使用了@RequestMapping注解来指定接受和返回的内容类型。produces属性用于指定返回的内容类型,consumes属性用于指定接受的内容类型。在这个例子中,我们接受的内容类型是APPLICATION_JSON_VALUE,返回的内容类型也是APPLICATION_JSON_VALUE。 当客户端发送一个请求时,如果请求的内容类型不支持,就会抛出HttpMediaTypeNotSupportedException异常。我们需要对这个异常进行处理,下面是一个解决方法: ```java @RestControllerAdvice public class CustomExceptionHandler { @ExceptionHandler(HttpMediaTypeNotSupportedException.class) public ResponseEntity<String> handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException ex) { return ResponseEntity.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE).body("不支持的内容类型"); } } ``` 我们可以通过编写一个全局异常处理类,使用@RestControllerAdvice和@ExceptionHandler注解来处理抛出的异常。在上面的代码中,handleHttpMediaTypeNotSupportedException方法用于处理HttpMediaTypeNotSupportedException异常,返回一个表示不支持内容类型的响应实体。 通过上述方法,我们可以解决[org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'xxx' not supported]异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值