Making authenticated POST requests with Spring RestTemplate for Android

Ok found the answer. exchange() is the best way. Oddly the HttpEntity class doesn’t have a setBody() method (it has getBody()), but it is still possible to set the request body, via the constructor. // Create the request body as a MultiValueMap MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>(); body.add(“field”, “value”); // Note the body … Read more

spring mvc rest service redirect / forward / proxy

You can mirror/proxy all requests with this: private String server = “localhost”; private int port = 8080; @RequestMapping(“/**”) @ResponseBody public String mirrorRest(@RequestBody String body, HttpMethod method, HttpServletRequest request) throws URISyntaxException { URI uri = new URI(“http”, null, server, port, request.getRequestURI(), request.getQueryString(), null); ResponseEntity<String> responseEntity = restTemplate.exchange(uri, method, new HttpEntity<String>(body), String.class); return responseEntity.getBody(); } This will … Read more

How to autowire RestTemplate using annotations

Errors you’ll see if a RestTemplate isn’t defined Consider defining a bean of type ‘org.springframework.web.client.RestTemplate’ in your configuration. or No qualifying bean of type [org.springframework.web.client.RestTemplate] found How to define a RestTemplate via annotations Depending on which technologies you’re using and what versions will influence how you define a RestTemplate in your @Configuration class. Spring >= … Read more

Using Spring RestTemplate in generic method with generic parameter

No, it is not a bug. It is a result of how the ParameterizedTypeReference hack works. If you look at its implementation, it uses Class#getGenericSuperclass() which states Returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class. If the superclass is a parameterized type, the … Read more

RestClientException: Could not extract response. no suitable HttpMessageConverter found

The main problem here is content type [text/html;charset=iso-8859-1] received from the service, however the real content type should be application/json;charset=iso-8859-1 In order to overcome this you can introduce custom message converter. and register it for all kind of responses (i.e. ignore the response content type header). Just like this List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>(); //Add … Read more

Sending GET request with Authentication headers using restTemplate

You’re not missing anything. RestTemplate#exchange(..) is the appropriate method to use to set request headers. Here’s an example (with POST, but just change that to GET and use the entity you want). Here’s another example. Note that with a GET, your request entity doesn’t have to contain anything (unless your API expects it, but that … Read more

Basic authentication for REST API using spring restTemplate

Taken from the example on this site, I think this would be the most natural way of doing it, by filling in the header value and passing the header to the template. This is to fill in the header Authorization: String plainCreds = “willie:p@ssword”; byte[] plainCredsBytes = plainCreds.getBytes(); byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); String base64Creds = … Read more

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