springrestspring-bootresttemplateasyncresttemplate

REST Service call with JAR as payload


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


Solution

  • 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);