404 when calling post from resttemplate - spring

I am attemtping to call a service using rest template and I am receiving a 404 error.
It's a POST. The signature of method is
String sendScreenAsPostcard(#RequestBody MultiValueMap<String, Object> params)
I am attenpting to call this from the resttemplate with the following code.
restTemplate.exchange(
"http://localhost:8080/sendScreeenAsPostCard",
HttpMethod.POST,
new HttpEntity<MultiValueMap<String, Object>>(parameters, headers),
String.class
).getBody()
Please advice what I am doing wrong. Thanks.

404 means the resource isn't found. If everything is fairly straightforward, it means there's an HTTP server running at localhost:8080, but no resource available at /sendScreenAsPostCard.

Related

Spring RestTemplate exchange returns 204 while trying to download text/csv from GET endpoint

I am trying to download text/csv file from one of the Salesforce GET endpoint using Spring RestTemplate.
I am getting 204 no-content when trying from code. While I am trying to hit the same endpoint with from Postman (Send and download) its working fine.
I am using the below code :
HttpHeaders headers=new HttpHeaders();
headers.setAccept(Mediatype.APPLICATION_OCTET_STREAM);
headers.add("Authorization","");
HttpEntity<String> entity=new HttpEntity<>(headers);
ResponseEntity<byte[]>
response=template.build.exchange(url,HttpMethod.GET,entity,byte.class);
System.out.println(response);
To my surprise, when I am debugging the application and giving a debug point on the line where the GET call is made, the response is coming properly.
Looks like some buffering is required while calling the GET endpoint.
I have tried to use BufferingClientHttpRequestFactory like below :
ClientHttpRequestFactory factory=new BufferingClientHttpRequestFactory(new
HttpComponentsClientHttpRequestFactory);
RestTemplate template=new RestTemplate (factory);
Even with the above am getting the same 204 content.

How to Pass a RequestBody in DeleteMethod in spring boot

I need to pass a request body to make a delete request in order to call a third-party API's delete Request. Is it possible?
The specification RFC 7231 doesn't prevent from accepting a RequestBody in DELETE method.
A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.
You can accept Request Body if the underlying web server is configured to parse the body for DELETE method.
So I have found the way that I was searching for. The problem can be solved using Rest template
RestTemplate restTemplate = new RestTemplate();
HttpEntity<Model> request = new HttpEntity<>(new Model("data"));
restTemplate.exchange(url, HttpMethod.DELETE, request, null);

How to pass header values using RestTemplate?

I am new to Spring. A project of mine needs to call an Autotask API which takes Api integration code, username and secret (password) in the headers. How do I integrate that into the Rest Template?
Below is a screenshot of the API that I am testing on swagger. Screenshot of API Call using Swagger
The additional parameters you need to send are all headers. You can set the headers as follows:
HttpHeaders headers = new HttpHeaders();
headers.set("ApiIntegrationCode", "HUCXSL...");
headers.set("UserName", "fdfsk...");
headers.set("Secret", "yR*42...");
Then you add the headers to a new HttpEntity instance and perform the request using RestTemplate:
HttpEntity entity = new HttpEntity(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
A very similar question has been asked here: HTTP get with headers using RestTemplate.

Getting Error message as NULL while invoking a service in one spring boot application from another spring boot application using RestTemplate

AM trying to invoke a spring boot microservice from another application.
Here is my Server code, if any exception occurs
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.body(new com.test.models.ResponseEntity("Tenant details
not found"));
Here is my client code,
ResponseEntity<Object> uploadResponse =
restTemplate.exchange("http://TESTAPPLICATION/v1/object",
HttpMethod.POST, requestEntity, Object.class);
Am getting a response as
Caused by: org.springframework.web.client.HttpClientErrorException: 403 null
If I hit my endpoint from PostMan, am getting proper response message.
But from my client-side application am getting response message as null. Please let me know what is going wrong.
UPDATE:
Screenshot.
Client Responds with INTERNAL_SERVER_ERROR. But from my server iam responding 403.

RestTemplate does not follow redirects even when GET is used

I am using the RestTemplate class to get to some page, which will require redirect ( redirect to oauth 2 provider, but that probably doesnt matter ).
I am doing:
ResponseEntity<String> forEntity = oAuth2RestTemplate.getForEntity(url, String.class, Collections.emptyMap());
with standard OAuth2RestTemplate, I haven't done any custom creation of this bean, so it is just injected from Spring.
As a response I get a ResponseEntity with location header and 302 status, but it is not followed. Judging by many other sources RestTemplate should follow redirects by default if all I do is GET - similar issue here: Follow 302 redirect using Spring restTemplate?
This is not a case, no redirect happens, am I missing something? Do I have to manually grab this location and redirect to it manually?
update:
OK, OAuth2RestTemplate is using OAuth2AccessTokenSupport as a request factory. This class configures factory to NOT follow redirects. Too bad it is so hard to find.
But using = new RestTemplate() doesnt help either ( note new keyword, if you are going to autowire it, then Spring will autowire oauth2RestTemplate ).

Resources