Spring Boot使用@RequestPart上传文件时引发异常。

huangapple go评论65阅读模式
英文:

spring boot upload file with the other parameter by using @RequestPart cause Excption

问题

当在Spring Boot中上传文件并带有参数时,您可以使用@RequestPart注解来处理参数。问题出现在当一个参数是String类型,另一个参数是Integer类型,且它们都被@RequestPart注解标记时会导致异常。

异常信息显示为:

2023-02-13 22:51:40,319 WARN  [http-nio-8099-exec-2] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver: Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported]

如果所有其他参数都是String类型且被@RequestPart注解标记,方法将正常工作并打印信息。

问题可能在于程序将StringInteger参数都视为Stream类型,但为什么String类型没有问题呢?

您还尝试了更改@PostMapping的内容,但问题仍然存在。

请提供了一个请求信息的链接,但您未提供具体的问题或要求。

需要更多的上下文来确定问题的确切原因。如果您有特定的问题或需要更多的帮助,请提供更多信息。

英文:

when using spring boot upload file with parameter, I used @RequestPart for all parameter.
Here the code:

> spring boot: 2.7.8

when one of them is String and the other is Integer and both of them annotated by @RequestPart will be cause Exception

package com.interfaces.anti.mage.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.interfaces.anti.mage.model.Address;
import com.interfaces.anti.mage.model.Order;
import com.interfaces.anti.mage.service.OrderService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * @author dengbojing
 */
@RestController
@RequestMapping("/order")
public class OrderApi {

    @PostMapping("/upload")
    public String upload(@RequestPart("file")MultipartFile file, @RequestPart("id") String id, @RequestPart("number") Integer number) {
        System.out.println(id);
        System.out.println(number);
        return "success";

    }
}

Exception info:

2023-02-13 22:51:40,319 WARN  [http-nio-8099-exec-2] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver: Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported]

when all the other parameter type is String and annotated by @RequestPart, the method will be worked and print the info.

 @PostMapping("/upload")
    public String upload(@RequestPart("file")MultipartFile file, @RequestPart("id") String id, @RequestPart("number") String number) {
        System.out.println(id);
        System.out.println(number);
        return "success";

    }

So why this? the exception means the program treat the String and Integer paramter as Stream? but why all String will be fine?
ps: even change to this @PostMapping(value = "/upload", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_JSON_VALUE}) also got the same problem

ps: this is the request info:

Spring Boot使用@RequestPart上传文件时引发异常。

答案1

得分: 1

I take the chance of this post because it pointed me to the right direction anyway so, for sake of anyone using Postman, receiving the error above here is my hint, learnt with pain and solved thanks to the hints above.

Postman中,当你填写一个form-data请求体时,默认情况下,表格中缺少CONTENT TYPE列。你可以通过右侧的按钮添加这一列,但它会显示为Auto,根据你的参数,你应该将其更改为正确的类型。

Spring Boot使用@RequestPart上传文件时引发异常。

希望这对其他人有所帮助 Spring Boot使用@RequestPart上传文件时引发异常。
问候

英文:

I take the chance of this post because it pointed me to the right direction anyway so, for sake of anyone using Postman, receiving the error above here is my hint, learnt with pain and solved thanks to the hints above.

In Postman, when you fill a form-data body, the table misses, by default I believe, the CONTENT TYPE column.
You can add the column the using the button on the right side, but then it will show Auto consequently, depending from your parameters, you should change it to the proper type.

Spring Boot使用@RequestPart上传文件时引发异常。

I hope it may help others Spring Boot使用@RequestPart上传文件时引发异常。
Regards

huangapple
  • 本文由 发表于 2023年2月13日 22:58:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定