How to handle empty response in Spring RestTemplate

Use String instead of AuthenticationResponse. If you get an empty response, the String value will be empty. ResponseEntity<String> authenticateResponse = restTemplate.getForEntity(authenticateUrl, String.class); UPDATE: Check this link. I think this will fix your issue. ResponseEntity<Void> response = restTemplate.getForEntity(authenticateUrl,Void.class); Void body = response.getBody();

What are the advantages and disadvantages of using feign over RestTemplate

Feign allows you to abstract the mechanics of calling a REST service. Once you configure and annotate the Feign interface, you can call a REST service by making a simple Java function call. The actual implementation of making a REST call is handled at runtime by Feign. This means that the implementation can be configured … Read more

Spring RestTemplate and generic types ParameterizedTypeReference collections like List

I worked around this using the following generic method: public <T> List<T> exchangeAsList(String uri, ParameterizedTypeReference<List<T>> responseType) { return restTemplate.exchange(uri, HttpMethod.GET, null, responseType).getBody(); } Then I could call: List<MyDto> dtoList = this.exchangeAsList(“http://my/url”, new ParameterizedTypeReference<List<MyDto>>() {}); This did burden my callers with having to specify the ParameterizedTypeReference when calling, but meant that I did not have to … Read more

Resttemplate getForEntity – Pass headers

You can use .exchange: ResponseEntity<YourResponseObj> entity = new TestRestTemplate().exchange( “http://localhost:” + port + “/youruri”, HttpMethod.GET, new HttpEntity<Object>(headers), YourResponseObj.class); Full Junit sample: @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class ReferenceTablesControllerTests { @LocalServerPort private int port; @Test public void getXxxx() throws Exception { MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); headers.add(“Content-Type”, “application/json”); headers.add(“Authorization”, “tokenxxx”); ResponseEntity<YourResponseObj> entity = new TestRestTemplate().exchange( … Read more

Access Https Rest Service using Spring RestTemplate

KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(new FileInputStream(new File(keyStoreFile)), keyStorePassword.toCharArray()); SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()) .loadKeyMaterial(keyStore, keyStorePassword.toCharArray()) .build(), NoopHostnameVerifier.INSTANCE); HttpClient httpClient = HttpClients.custom().setSSLSocketFactory( socketFactory).build(); ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); RestTemplate restTemplate = new RestTemplate(requestFactory); MyRecord record = restTemplate.getForObject(uri, MyRecord.class); LOG.debug(record.toString());

Multipart File Upload Using Spring Rest Template + Spring Web MVC

The Multipart File Upload worked after following code modification to Upload using RestTemplate LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); map.add(“file”, new ClassPathResource(file)); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>( map, headers); ResponseEntity<String> result = template.get().exchange( contextPath.get() + path, HttpMethod.POST, requestEntity, String.class); And adding MultipartFilter to web.xml <filter> <filter-name>multipartFilter</filter-name> <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class> … Read more

RestTemplate PATCH request

I solved this problem just adding a new HttpRequestFactory to my restTemplate instance. Like this RestTemplate restTemplate = new RestTemplate(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); requestFactory.setConnectTimeout(TIMEOUT); requestFactory.setReadTimeout(TIMEOUT); restTemplate.setRequestFactory(requestFactory); For TestRestTemplate, add @Autowired private TestRestTemplate restTemplate; @Before public void setup() { restTemplate.getRestTemplate().setRequestFactory(new HttpComponentsClientHttpRequestFactory()); } PS: You will need add httpClient component in your project <dependency> <groupId>org.apache.httpcomponents</groupId> … Read more

Using RestTemplate in Spring. Exception- Not enough variables available to expand

The root cause is that RestTemplate considers curly braces {…} in the given URL as a placeholder for URI variables and tries to replace them based on their name. For example {pageSize} would try to get a URI variable called pageSize. These URI variables are specified with some of the other overloaded getForObject methods. You … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)