【问题标题】:How to send Multipart form data and upload PDF with RestTemplate Spring Boot如何使用 RestTemplate Spring Boot 发送 Multipart 表单数据并上传 PDF
【发布时间】:2016-10-14 04:26:23
【问题描述】:

朋友们,你们好, 在我的微服务和 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"]->java.io.FileInputStream["fd"]);嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.FileDescriptor and no properties found to create BeanSerializer(为避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS))(通过参考链:org.springframework。 web.multipart.support.StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"])

请提供任何帮助。 我整天都被困在这上面。

【问题讨论】:

    标签: spring spring-mvc spring-boot jackson resttemplate


    【解决方案1】:

    我已经使用以下方法签名在请求正文中将参数(包括作为字节流的 pdf 文件,即byte[])作为 json 发送:

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public @ResponseBody Long handleFileUpload(@Valid @RequestBody Invoice uploadedInvoice){
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2020-07-23
      • 2017-10-30
      • 2016-11-17
      • 2020-05-10
      • 2019-03-19
      • 2012-12-18
      • 2019-08-03
      • 2021-06-06
      相关资源
      最近更新 更多