0

How to read JAR file from src/main/resources folder & use it as payload for REST service call from Spring boot application using RestTemplate

Any code snippet would be helpful

Thanks

Sriram
  • 33
  • 4
  • Jar file is not different from any other file type in rest upload download. Why you want to treat differently? – Red Boy May 31 '18 at 16:53
  • hi Red Boy, Thanks a lot for your input, have updated the question the challenge I face is reading the Jar file from resources folder and using it in the payoad for rest service call – Sriram May 31 '18 at 18:55
  • 1
    Please share what you have tried, its difficult for SO members to write a code for your qs. – Amit K Bist May 31 '18 at 20:43
  • Any Code / Github / add some code here in question. Is that possible? – AMIC MING May 31 '18 at 21:33

1 Answers1

1

You need to use LinkedMultiValueMap to send a file using RestTemplate, Code should be like below:

    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("yourjarfile").getFile());

    LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("file", new FileSystemResource(file));

    HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(map,
                getHeaders());

    ResponseEntity<String> resp = new RestTemplate().exchange(
                    "REST_URL/", HttpMethod.POST, httpEntity,
                    String.class);
Amit K Bist
  • 6,760
  • 1
  • 11
  • 26