java - 如何接收请求正文数据以及多部分图像文件?
问题描述
我想接收带有请求正文数据的多部分图像文件,但无法弄清楚,为什么它会抛出org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream'
不支持的异常
下面是我的实现
public ResponseEntity<GlobalResponse> createUser(@Valid @RequestPart("json") UserDTO userDTO ,@RequestPart(value ="file", required=false)MultipartFile file) throws IOException {
//Calling some service
return new ResponseEntity<>( HttpStatus.OK);
}
编辑:这是我的邮递员配置
解决方案
由于您以表单数据的形式发送数据,它可以以键值对的形式发送数据。不在,RequestBody
所以你需要像这样修改你的端点:
@PostMapping(value = "/createUser")
public ResponseEntity createUser(@RequestParam("json") String json, @RequestParam("file") MultipartFile file) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
UserDTO userDTO = objectMapper.readValue(json, UserDTO.class);
// Do something
return new ResponseEntity<>(HttpStatus.OK);
}
您需要以表示形式接收UserDTO
对象,String
然后将其映射到UserDTO
using ObjectMapper
。这将允许您接收MultipartFile
和UserDTO
使用表单数据。
推荐阅读
- c++ - C++ Autoclicker 不退出循环
- c++ - 放置 new 和 uninitialized_fill() 的行为
- pygame - 如何解决精灵移动速度过快的问题
- apache-flink - 在独立 flink 集群(Apache flink 1.9.0)上运行 apache Beam 字数统计示例时出错
- spring - 获取 org.springframework.beans.factory.BeanCreationException:使用名称创建 bean 时出错
- python - 利用类中的相同属性
- react-leaflet - 无法使用 JSON 变量设置 React-Leafletlet Maps CRS
- typo3 - TYPO3:如何显示页面树外文件夹中的记录
- python - 将列名添加到 Pandas DataFrame 时 Python 引发值错误
- javascript - Select2 分页在 Vue 组件中不起作用