如何使用RestTemplate Spring Boot发送Multipart表单数据并上传PDF [英] How to send Multipart form data and upload PDF with RestTemplate Spring Boot
问题描述
Good day Pals,
在我的微服务和spring-boot应用程序中,我有一个前端员工微服务,它消耗了另一个带有文件上传端点的微服务。
调用服务基于Spring rest控制器,我试图在Spring Boot应用程序中使用RestTemplate来使用File-Upload端点。简而言之,尝试上传PDF文件。
Good day Pals, In my microservice and spring-boot app, I have a frontend employee microservice which consumes another microservice with file upload endpoint. The calling service is based on spring rest controller and I am trying to consume a File-Upload endpoint using RestTemplate in a Spring Boot application. In a nutshell, trying to upload a PDF file.
我已经探索了以下SO帖子,但它不适用于我:
I have explored the following SO post, but its not working for me:
jackson disable fail_on_empty_beans
我在postman中测试这个并收到以下错误:
I am testing this in postman and getting the following error:
org.springframework.http.converter.HttpMessageNotWritableException:无法写入内容:没有找到类java.io.FileDescriptor的序列化器,也没有发现创建BeanSerializer的属性。
org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer.
任何帮助都将不胜感激....
Any help will be appreciated pleasee ....
以下是主要组成部分 -
Below are the main components -
@RestController
@RequestMapping(path = "/employee")
public class EmployeeController {
private EmployeeService empService;
@RequestMapping(value ="/emp/load", method = RequestMethod.POST)
public
@ResponseBody
ResponseEntity<byte[]> handleFileUpload(
@RequestParam("file") MultipartFile file, @RequestParam String a, @RequestParam String b, @RequestParam String c, @RequestParam String d, @RequestParam String e) throws Exception {
return empService.handleFileUpload(file, a, b, c, d, e);
}
}
服务实施
@Service
public class EmployeeServiceImpl implements EmployeeService{
@Value("${emp.base.url}")
private String EMP_BASE_URI;
public ResponseEntity<byte[]>handleFileUpload(MultipartFile file, String a, String b, String c, String d, String e) {
final String uri = EMP_BASE_URI + "/upload";
RestTemplate restTemplate = getMappedRestTemplate();
MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>();
params.add("file", file);
params.add("a", a);
params.add("b", b);
params.add("c", c);
params.add("d", d);
params.add("e", e);
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "multipart/form-data");
ResponseEntity<byte[]> response = restTemplate.exchange(uri, HttpMethod.POST, new HttpEntity<>(params, headers), byte[].class);
return new ResponseEntity<>(response.getBody(), response.getStatusCode());
}
private RestTemplate getMappedRestTemplate(){
RestTemplate restTemplate = new RestTemplate();
ObjectMapper newObjectMapper = new ObjectMapper();
newObjectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
newObjectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter=new MappingJackson2HttpMessageConverter();
FormHttpMessageConverter formConvertor = new FormHttpMessageConverter();
restTemplate.getMessageConverters().add(formConvertor);
restTemplate.getMessageConverters().add(mappingJacksonHttpMessageConverter);
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
return restTemplate;
}
}
我收到以下错误:
无法编写HTTP消息:org.springframework.http.converter.HttpMessageNotWritableException:无法写入内容:没有找到类java.io.FileDescriptor的序列化程序,也没有发现创建BeanSerializer的属性(为了避免异常,请禁用SerializationFeature。 FAIL_ON_EMPTY_BEANS))(通过引用链:org.springframework.web.multipart.support.StandardMultipartFile [inputStream] - > java.io.FileInputStream [fd]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:没有为类java.io.FileDescriptor找到序列化器,也没有发现创建BeanSerializer的属性(为了避免异常,禁用SerializationFeature.FAIL_ON_EMPTY_BEANS))(通过引用链:org.springframework。 web.multipart.support.StandardMultipartFile [inputStream] - > java.io.FileInputStream [fd])
I am getting the following error:
Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.web.multipart.support.StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: org.springframework.web.multipart.support.StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"])
请提供任何帮助,不胜感激。
我整天都被困在这一天。
Please, any help with be appreciated. I have been stuck on this all day.
推荐答案
我已经去了发送参数(包括pdf文件)作为字节流,即 byte []
)作为请求体中的json使用以下方法签名:
I have gone for sending the params (including the pdf file as a byte stream i.e. byte[]
) as json in the request body using the following method signature:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody Long handleFileUpload(@Valid @RequestBody Invoice uploadedInvoice){
...
}
这篇关于如何使用RestTemplate Spring Boot发送Multipart表单数据并上传PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!