In my spring boot Application, I need to pass String value along with MultipartFile as Requestparam
.
Controller is below where I'm converting MultipartFile to java.io.File
and then passing it to DAO controller with help of restTemplate
.
Request from Angular will hit Upload Controller first then UploadController is client(Java) and it will be calling server svs-ba-dao Controller with Base URL.
csvUpload
contains base 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<String, Object> data = new LinkedMultiValueMap<>();
data.add("parentPkId", parentPkId);
data.add("file", new FileSystemResource(cnvFile));
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(data, requestHeaders);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> 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);
}
I have included these properties into the application.properties file of my application:
spring.servlet.multipart.maxFileSize=1MB spring.servlet.multipart.maxRequestSize=1MB
So I start my application and call the /csvUpload
that generate the POST request.
i'm getting below error.
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
http://localhost:8082/svs-ba-dao/csvUpload?parentPkId=&file=multipartFile
honestly i haven't worked on this before.
Where i'm doing wrong, any leads or corrections welcomed. Thanks in advance