Spring boot: Using Rest template make post call - spring-boot

I need to make a post call using following details using rest template:
I need to send json with 4/5 parameters, need to set headers for content type and accept, and method with be post.
Can you please help me with sample code to create the json object and set the headers and make the post call

You can use postForObject method directly. You can give java object and for accept a response class.
SomeRequestObject obj1 = new SomeRequestObject();
SomeResponseObject response = restTemplate.postForObject("url-to-service", obj1, SomeResponseObject.class);

Related

How to Get request body in spring cloud gateway

Is there a way I can extract the request body (JSON) from the spring cloud gateway?. I went through a few approaches like RequestBodyrewrite and all, but as far as I understood it is used for modifying the response body. In my case, I wanted to get the request body and do some logic there and send the data via headers to the underlying microservices. Right now I am getting the request body with the attribute "cachedRequestBodyObject" with the help of the request body predicator.
readBody(SomeClass.class, s-> true)
I don't think I am not fully making use of the request body predicator here. We would also get this as part of the getRequestBody() method but it has a data buffer as a return type, so not sure how to get the JSON details from the data buffer. Is there a way better approach to do this ?..
Thanks in Advance.

Read Write headers Rest Controller

Is/how possible to access the HttpRequest headers in a Rest controler method and can that header be directly written? I am sending the subscriber choice off a select to the a REST method from an Ajax call, just cant interpret or find any example on how to access in the method.
Yes, you can access the incoming headers. Simply use
#RequestHeader("the-header-name") TheHeaderType theHeader
as a parameter in the controller method, or
#RequestHeader MultiValueMap<String, String> headers
to get many headers at once.
However, I'm not sure what this is asking:
can that header be directly written?
To my chagrin found the answer here. It is in the Ajax call as of 1.5.

Get part of JSON response object with Spring RestTemplate

I am trying to consume this, by following the tutorial from http://spring.io/guides/gs/consuming-rest/.
And I am interested in the "feeds" part of the response only.
My question is:
Is there any other way without using the toString() method in the tutorial. As, I want to access the some parts of response body and display it on a webpage.
Please, provide me with the details of the model and controller.

temporarily change $http.defaults.transformResponse

How can I customize $http in angularjs such that it will accept strings as its response in a $http.post call? Right now, when I do a $http.post call, my response is in string but angularjs by default uses JSON therefore I get an error. Right now I have something along the lines of
function getResponseURL(response) {
//this will convert the response to string
return response;
}
$http.defaults.transformResponse = [];
$http.defaults.transformResponse.unshift(getResponseURL);
However if I use the code above, any $http.post calls after that call uses string. I want it to use the original default JSON format. How can I go about into just temporarily changing the response to string for this one call but the rest stay as JSON type as a response?
Why not only register that transform for ONLY that request?
Angular js $http docs
If you wish
override the request/response transformations only for a single
request then provide transformRequest and/or transformResponse
properties on the configuration object passed into $http.

Set default WebAPI formatter

We are using WebAPI to mimic the handling of a legacy system. As a result, we would like the default response formatter to be the XmlFormatter and not the JsonFormatter. The reason is that some of the existing calls to the service do not supply the Accept: HTTP header field.
I can achieve this by removing the JsonFormatter from the Formatters collection and then re-adding it, forcing it to be at the end of the chain.
This then result in the default format response using the XmlFormatter. Although it works, it just doesn't feel correct, and although I am moving Json to the back of the collection, there is no guarantee that the XmlFormatter is at the front of the collection.
Ideas/thoughts?
Just add formatters in the right order. If ASP.NET Web API finds two formatters for the same content type, it will pick the first one, so it is very important to add formatters in the right order.
//somewhere in Web Api config
config.Formatters.Clear();
config.Formatters.Add(new XmlMediaTypeFormatter());
config.Formatters.Add(new JsonMediaTypeFormatter());
So the default will be XML, the first formatter, but the API still supports JSON if the request asks for it (with appropriate HTTP header).
Finally, another different approach, is to use a custom IContentNegotiator. It will allow you to select the most appropriate MediaTypeFormatter for a given request.
//somewhere in Web Api config
config.Services.Replace(typeof(IContentNegotiator), new MyCustomContentNegotiator());
An example is available here.
This is returned to automatically serialise and return json when content type is json.
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
((DefaultContractResolver)config.Formatters.JsonFormatter.SerializerSettings.ContractResolver).IgnoreSerializableAttribute = true;

Resources