好日子好朋友, 在我的微服务和spring-boot应用程序中,我有一个前端员工微服务,它消耗了另一个带有文件上传端点的微服务。 调用服务基于Spring rest控制器,我试图在Spring Boot应用程序中使用RestTemplate来使用File-Upload端点。简而言之,尝试上传PDF文件。
我已经探索了以下SO帖子,但它不适合我:
jackson disable fail_on_empty_beans
我在邮递员中测试这个并收到以下错误:
org.springframework.http.converter.HttpMessageNotWritableException:无法写入内容:找不到类java.io.FileDescriptor的序列化程序,也没有发现创建BeanSerializer的属性。
任何帮助将不胜感激....
以下是主要组成部分 -
休息控制器############ @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”] - &gt; 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”] - &GT; java.io.FileInputStream中[ “FD”])
请,任何帮助,不胜感激。 我整天都被困在这一天。
答案 0 :(得分:0)
我已经使用以下方法签名将params(包括pdf文件作为字节流,即byte[]
)作为json发送到请求正文中:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody Long handleFileUpload(@Valid @RequestBody Invoice uploadedInvoice){
...
}