Spring Boot-多部分-不支持的媒体类型 [英] Spring Boot - Multipart - Unsupported Media Type

查看:106
本文介绍了Spring Boot-多部分-不支持的媒体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个帖子请求中发送一个文件和一个json模型.

我的请求映射如下:

  @PostMapping("{id}/files")公共MyOutput创建(@PathVariable字符串ID,@ RequestPart(请求")MyInput输入,@ RequestPart(文件")MultipartFile文件){//...} 

我收到的错误:

  {时间戳记":"2019年2月7日,下午3:18:50",状态":415,错误":不支持的媒体类型","message":不支持内容类型'application/octet-stream'","trace":"org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型'application/octet-stream'...,路径":"/tests/12345/文件"} 

邮递员要求:

如果需要使用多部分文件发送另一个对象,则可以将其作为字符串发送,然后可以将其转换为后端的对象.

  @PostMapping("/upload")public void uploadFile(@Nullable @RequestParam(value ="file",required = false)MultipartFile文件,@RequestParam(值=输入",必填=假)字符串st){om =新的ObjectMapper();MyInput输入= null;尝试 {输入= om.readValue(st,MyInput.class);//string st->MyInput输入} catch(IOException e){e.printStackTrace();}} 

邮递员请求示例:

I want to send a file and a json model at one post request.

My Request Mapping looks like that:

    @PostMapping("{id}/files")
    public MyOutput create(@PathVariable String id, @RequestPart("request") MyInput input, @RequestPart("file") MultipartFile file) {
    // ...
    }

The error I receive:

{
    "timestamp": "Feb 7, 2019, 3:18:50 PM",
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type 'application/octet-stream' not supported",
    "trace": "org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported...,
    "path": "/tests/12345/files"
}

Postman request: http://imgshare.free.fr/uploads/62f4cbf671.jpg

My WebConfig:

    @Override
    public void configureMessageConverters(final List<HttpMessageConverter<?>> converters) {

        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.setPrettyPrinting().create();

        final GsonHttpMessageConverter msgConverter = new GsonHttpMessageConverter();
        msgConverter.setGson(gson);
        msgConverter.setDefaultCharset(StandardCharsets.UTF_8);
        converters.add(msgConverter);

        converters.add(new StringHttpMessageConverter());

        //
        converters.add(new ByteArrayHttpMessageConverter());
        converters.add(new FormHttpMessageConverter());
        converters.add(new ResourceHttpMessageConverter());

    }

解决方案

You can try to use instead of this

@RequestPart("file") MultipartFile file

use this

@RequestParam(value = "file",required = false) MultipartFile file

And be sure you set the request type as multipart/form-data You can set it from postman in the headers tab.

If another object you need to send with multipart file,you can send it as a string and then you can convert it to object at the backend side.

  @PostMapping("/upload")
    public void uploadFile(@Nullable @RequestParam(value = "file",required = false) MultipartFile file,
                                            @RequestParam(value = "input", required = false) String st)
    {
        ObjectMapper om = new ObjectMapper();
        MyInput input = null;
        try {
            input = om.readValue(st, MyInput.class);   //string st -> MyInput input
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Postman request example :

这篇关于Spring Boot-多部分-不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆