I'm working on a chain of microservice in Java where A microservice calls B and B in turn calls C(some external service). I need to call a method of C which accepts Multipart File and upload it to a location.
Now in A microservice, I can have multiple files along with some tags. Each file can have their own tags. A microservice just redirects the request to B microservice and nothing else.
Below is how I made my AController.java
, AService.java
and ARequest.java
classes.
AController.java
@PostMapping(value="/document/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<List<Response>> uploadDocuments(@RequestParam(required = true) Integer clientId, @ModelAttribute("files") List<ARequest> aRequestList) throws IOException {
return aService.uploadDocuments(clientId, aRequestList);
}
AService.java
public ResponseEntity<List<Response>> uploadDocuments(Integer clientId, List<ARequest> aRequestList)
throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA_VALUE);
HttpEntity<ARequest> requestPayload = new HttpEntity<ARequest>(aRequestList, headers);
ResponseEntity<List<A>> responseEResponsentity = restTemplate.exchange("SomeUrl", HttpMethod.POST,
requestPayload, new ParameterizedTypeReference<ResponseEntity<List<Response>>>());
}
ARequest.java
public class ARequest implements Serializable {
private static final long serialVersionUID = 9156893005763135311L;
private String documentId;
private String tag;
private String displayName;
private String description;
private MultipartFile fileContent;
}
My B Microservice has same controller method as AController. BService will save some data in DB which is coming in from the BRequest object and Cresponse object and return list of saved DB values for a given clientId.
Here is my Code for -
BController.java
@PostMapping(value="/document/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<List<Response>> uploadDocuments(@RequestParam(required = true) Integer clientId, @ModelAttribute("files") List<BRequest> bRequestList) throws IOException {
return bService.uploadDocuments(clientId, bRequestList);
}
BService.java
public ResponseEntity<List<Response>> uploadDocuments(Integer clientId, List<BRequest> bRequestList)
throws IOException {
for (BRequest request : bRequestList) {
// Call C service with request.getFileContent() and get response back
// Save in DB (clientID, tag, displayName, description, documentId(coming from response of C)
}
return Response;
}
Response is a JPA Entity in B Microservice and is just a DTO in A microservice.
1.Can someone guide me if my approach is correct? 2. How can i test it using postman 3. If i change tweak code to get input as single Request instead of file to test it from postman, i'm getting below error when i call B Microservice from A.
No HttpMessageConverter for java.util.ArrayList and content type "multipart/form-data"
If i change some other thing, i get 400 Bad Request
Now i'm so confused that i cant think in any direction. Please help me