英文:
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注解标记,方法将正常工作并打印信息。
问题可能在于程序将String
和Integer
参数都视为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:
答案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,根据你的参数,你应该将其更改为正确的类型。
希望这对其他人有所帮助
问候
英文:
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.
I hope it may help others
Regards
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论