post表单数据与springrestemplate

li9yvcax  于 2021-07-15  发布在  Java
关注(0)|答案(1)|浏览(549)

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

这是我的控制器类:

@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 request successful”。请让我知道或指向我的任何参考。

gorkyyrv

gorkyyrv1#

是否尝试添加product=mediatype.multipart\ form\ data\ value?就像这样:

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

您不能在响应中添加http状态201或200。意思是“请求成功”

相关问题