[英]POST form data with Spring RestTemplate
I have to make a HTTP POST request to get notification "upload success" from an external API which upload file(.doc/.jpg, etc).我必须发出 HTTP POST 请求才能从上传文件(.doc/.jpg 等)的外部 API 获得通知“上传成功”。 Currently, I am invoking the API using the below postman command:目前,我正在使用以下 postman 命令调用 API:
This my Controller Class:这是我的 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();
}
} }
But I get internal server error.但我得到内部服务器错误。
[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"])
how to post the HTTP request to upload file(.doc/.jpg, etc) in REST in return i get the notification (JSON) "POST Request successful" in client side.如何在 REST 中发布 HTTP 请求以上传文件(.doc/.jpg 等)作为回报,我在客户端收到通知(JSON)“POST 请求成功”。 Kindly let me know or point me to any reference.请让我知道或指出我的任何参考。
Do you try add produce = MediaType.MULTIPART_FORM_DATA_VALUE?您是否尝试添加produce = MediaType.MULTIPART_FORM_DATA_VALUE? Like that:像那样:
@RequestMapping(value = "/upload/notif", method = RequestMethod.POST produces = MediaType.MULTIPART_FORM_DATA_VALUE)
And you cant add http status 201 or 200 in your response.而且您不能在回复中添加 http 状态 201 或 200。 That's means " Request successful"这意味着“请求成功”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.