繁体   English   中英

使用 Spring RestTemplate POST 表单数据

[英]POST form data with Spring RestTemplate

我必须发出 HTTP POST 请求才能从上传文件(.doc/.jpg 等)的外部 API 获得通知“上传成功”。 目前,我正在使用以下 postman 命令调用 API:

邮递员命令

这是我的 Controller Class:

@RestController
public class PushNotif {

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

    @Autowired
    RestTemplate restTemplate;

    private static Logger logger = LogManager.getLogger(PushNotif.class.getName());


   @RequestMapping(value = "/upload/notif", method = RequestMethod.POST)
   public String NotifStatus(@RequestParam("file") MultipartFile file) {
     final String url = "http://localhost:8080/upload";

     HttpHeaders headers = new HttpHeaders();
     headers.setAccept(Arrays.asList(MediaType.MULTIPART_FORM_DATA));
     HttpEntity<MultipartFile> entity = new HttpEntity<MultipartFile>(file, headers);

     return restTemplate.exchange(url, HttpMethod.POST, entity, 
            String.class).getBody();

}

}

但我得到内部服务器错误。

 [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class java.io.FileDescriptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 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.StandardMultipartHttpServletRequest$StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"])] with root cause

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 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.StandardMultipartHttpServletRequest$StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"])

如何在 REST 中发布 HTTP 请求以上传文件(.doc/.jpg 等)作为回报,我在客户端收到通知(JSON)“POST 请求成功”。 请让我知道或指出我的任何参考。

您是否尝试添加produce = MediaType.MULTIPART_FORM_DATA_VALUE? 像那样:

 @RequestMapping(value = "/upload/notif", method = RequestMethod.POST produces = MediaType.MULTIPART_FORM_DATA_VALUE)

而且您不能在回复中添加 http 状态 201 或 200。 这意味着“请求成功”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM