我有两个端点 api,它们是/uploadand /重定向
/upload是我直接上传文件的地方。/重定向是我接收文件并将其传递给上传并获取 JSON 响应的地方/upload.所以下面是我的代码:
package com.example;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
@RestController
public class UserLogsController {
@Autowired
@Qualifier("restTemplateUserRegitration")
private RestTemplate restTemplateUserRegitration;
@Bean
public RestTemplate restTemplateUserRegitration() {
RestTemplateBuilder builderUserRegitration = new RestTemplateBuilder();
RestTemplate buildUserRegitration = builderUserRegitration.build();
return buildUserRegitration;
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<Map<String, Object>> handleFileUpload(
@RequestParam("file") MultipartFile file) {
String name = file.getName();
System.out.println(name);
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File("D:\\myfile.csv")));
stream.write(bytes);
stream.close();
JwtAuthenticationErrorResponse FeedBackResponse = new JwtAuthenticationErrorResponse();
FeedBackResponse.setCode(100);
FeedBackResponse.setMessage("Successfully Updated Batch User List");
Map<String, Object> FeedBackStatus = new HashMap<String, Object>();
FeedBackStatus.put("status", FeedBackResponse);
return ResponseEntity.ok(FeedBackStatus);
} catch (Exception e) {
JwtAuthenticationErrorResponse FeedBackResponse = new JwtAuthenticationErrorResponse();
FeedBackResponse.setCode(100);
FeedBackResponse.setMessage(e.getMessage());
Map<String, Object> FeedBackStatus = new HashMap<String, Object>();
FeedBackStatus.put("status", FeedBackResponse);
return ResponseEntity.ok(FeedBackStatus);
}
} else {
JwtAuthenticationErrorResponse FeedBackResponse = new JwtAuthenticationErrorResponse();
FeedBackResponse.setCode(100);
FeedBackResponse.setMessage("Successfully Updated Batch User List");
Map<String, Object> FeedBackStatus = new HashMap<String, Object>();
FeedBackStatus.put("status", FeedBackResponse);
return ResponseEntity.ok(FeedBackStatus);
}
}
@RequestMapping(value = "/redirect", produces = { MediaType.APPLICATION_JSON_VALUE }, method = RequestMethod.POST)
public ResponseEntity<?> registerBatchUser(@RequestParam("file") MultipartFile file) {
Map<String, Object> FeedBackStatus = new HashMap<String, Object>();
FeedBackStatus = restTemplateUserRegitration.postForObject("http://localhost:8080/upload", file, Map.class);
return ResponseEntity.ok(FeedBackStatus);
}
}
端点 /upload 工作得很好。但是当我调用 /redirect 时,它会抛出一个错误
“例外”:
“org.springframework.http.converter.HttpMessageNotWritableException”,
"message": "无法写入 JSON 文档:找不到序列化器
类 java.io.FileDescriptor 且未发现要创建的属性
BeanSerializer(为了避免异常,禁用
SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链:
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"]);
嵌套异常是
com.fasterxml.jackson.databind.JsonMappingException:没有序列化器
找到了类 java.io.FileDescriptor 并且没有发现任何属性
创建 BeanSerializer (为了避免异常,禁用
SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链:
org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"])",
我不确定为什么会发生这种情况。如有任何帮助,我们将不胜感激。