当前位置: 首页 > 知识库问答 >
问题:

Spring启动-多部分-不支持的媒体类型

袁阿苏
2023-03-14

我想在一个post请求中发送一个文件和一个json模型。

我的请求映射如下所示:

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

我收到的错误:

{
    "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"
}

邮递员要求:http://imgshare.free.fr/uploads/62f4cbf671.jpg

我的网络配置:

    @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());

    }

共有1个答案

叶德运
2023-03-14

你可以试着用它来代替这个

@RequestPart("file") MultipartFile file

用这个

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

确保您将请求类型设置为多部分/表单数据,您可以从headers选项卡中的postman进行设置。

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

  @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();
        }
    }

邮递员请求示例:

 类似资料:
  • 我用了几种不同的方法检查,也下载了一个新的项目,看检查哪里有bug但是我仍然不知道答案。 这是我的RestController 那是我的模型 必要时分级依赖关系 在《邮递员》中,我犯了一个错误 {“时间戳”:1495992553884,“状态”:415,“错误”:“不受支持的媒体类型”,“异常”:“org.springframework.web.httpmediatypenotsupportede

  • 它总是给出415不支持的媒体类型错误从邮递员。Header包含带有边界的多部分/表单数据,如下所示CURL调用中所示。还尝试用RequestBody替换RequestPart但没有成功。 当使用FilePart时,我们是否需要以不同的方式调用Spring5中的多部分文件上传API? 然而,将uploads参数从更改为是有效的。 我们不能对RequestPart使用相同的curl调用吗?

  • 我正在用Spring Boot构建一些API,但是当我试图用Postman查询时,我得到了一些关于Content-Type的错误。 我不明白哪里出了问题。 我注意到错误消失时,我删除了@刚体作为参数的方法。为什么啊? 我只想: 将XML发送到API

  • 问题内容: 我正在尝试使用jQuery 1.6(Jackson 2.1.1 和Spring 3.2.0 )通过JSON通过该方法向数据库中插入和/或更新数据。 JS代码如下。 通过URL映射的Spring控制器内部的方法如下。 服务器按照问题的含义进行响应, 415不支持的媒体类型 标头信息如下所示。 整个文件如下。 当我删除一个方法参数并仅用于接受请求参数时,它可以工作。 问题答案: 浏览器发送

  • 问题内容: 我正在使用Spring 3.2,并尝试使用ajax发布请求来提交json对象数组。如果相关,我转义了所有特殊字符。 我的HTTP状态为415。 我的控制器是: jQuery是: 我发布的数组中的对象之一的示例是以下json: 错误是: 为什么会发生此错误-有人知道吗? 问题答案: 您可以尝试使用。它没有任何问题

  • 我在使用Spring的reactive框架处理文件上传时遇到了一些问题。我想我正在跟踪文档,但无法摆脱/问题。 似乎Spring在将这些转换为时遇到了问题..?我卡住了--任何帮助都是可取的!