在我的春季启动应用程序中,我需要将String值与MultipartFile一起作为Requestparam传递.
控制器位于我将MultipartFile转换为java.io.File的位置,然后在restTemplate的帮助下将其传递给DAO控制器.
来自Angular的请求将首先命中上传控制器,然后上传控制器是客户端(Java),它将使用基本URL调用服务器svs-ba-dao控制器.
csvUpload包含基本URL:http://localhost:8082/svs-ba-dao/csvUpload?parentPkId=&file=multipartFile
@CrossOrigin
@RestController
public class UploadController {
private static final Logger log = LogManager.getLogger(UploadController.class);
@Value(“${salama.dao.csvUpload.rest.url}”)
private String csvUpload;
@Autowired
UploadHelperService uploadHelperService;
@PostMapping(value = “/csvUpload”)
public String csvUpload(@QueryParam(“parentPkId”) String parentPkId, @RequestParam(“file”) MultipartFile file ) throws IllegalStateException, IOException{
log.info(“Calling csvUpload :” + csvUpload);
final HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
File cnvFile = uploadHelperService.multipartToFile(file,file.getOriginalFilename());
System.out.println(“Converted File Name is –>”+cnvFile.getName());
final MultiValueMap data = new LinkedMultiValueMap<>();
data.add(“parentPkId”, parentPkId);
data.add(“file”, new FileSystemResource(cnvFile));
HttpEntity> requestEntity = new HttpEntity>(data, requestHeaders);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity obj =restTemplate.exchange(csvUpload, HttpMethod.POST,
requestEntity, String.class);
return obj.getBody();
}
}
In svs-ba-dao Controller catching it as below
@RequestMapping(value=”/csvUpload”, method=RequestMethod.POST)
public String csvUpload(@RequestParam String parentPkId, @RequestParam MultipartFile file) throws IOException {
log.info(“Entered method csvUpload() of svs-ba-dao.class”);
return uploadService.csvUpload(parentPkId,file);
}
我已将这些属性包含到我的应用程序的application.properties文件中:
spring.servlet.multipart.maxFileSize = 1MB
spring.servlet.multipart.maxRequestSize = 1MB
所以我启动我的应用程序并调用生成POST请求的/ csvUpload.
我低于错误.
Converted File Name is –>testInput_TxnMpMotSlv.csv
2019-01-24T15:12:41,217 ERROR [[localhost].[/svs-ba-dao].[dispatcherServlet]] [http-nio-8085-exec-2] Servlet.service() for servlet [dispatcherServlet] in context with path [/svs-ba-dao] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpServerErrorException: 500 null] with root cause
org.springframework.web.client.HttpServerErrorException: 500 null
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:97) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:766) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:724) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:600) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at com.svs-ba-dao.controller.UploadController.csvUpload(UploadController.java:59) ~[classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_192]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_192]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_192]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_192]
Error is in resttemplate as it is taking null, but syntactically way of calling RestTemplate is correct as i’m not getting any error.
am I calling Base URLproperly?by passing requestparam values. i.e
parentPkId=?&file=multipartFile
解决方法:
在这里,我发现我错了
csvUpload will hold the URL which should be passed in rest template
@Value(“${salama.dao.csvUpload.rest.url}”)
private String csvUpload;
我找到了基本URL:即csvUpload
HTTP://本地主机:8082 / SVS-BA-DAO / csvUpload parentPkId =安培;文件= multipartFile
从UploadController发送是错误的.
无需为URL中的mediaType指定类似multipartFile的内容.它会自动捡起来.
但是对于某些URL,如果发送JSON对象,需要指定?mediaType = json.
这是我更新的基本URL:http:// localhost:8082 / svs-ba-dao / csvUpload?parentPkId =& file =
标签:java,spring-mvc,spring,spring-boot-2,resttemplate
来源: https://codeday.me/bug/20190522/1152420.html