Get part of JSON response object with Spring RestTemplate - spring-boot

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.

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.

Spring boot: Using Rest template make post call

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

Web API ignores non-model body parameters; should throw error

In Web API v2 when you supply data in the POST body that are not part of the model, they are ignored by the framework. This is fine in most cases, but I need to check this and return an error response, so the user doesn't get unexpected results (he expects these wrong parameters to do something...).
So how do I check for this? The model will be null, but when the framework has parsed the data and returned a null-model, I can no longer access the body through Request.Content. So what options are there?
One way is to derive your DTO class from DynamicObject. Check out my blog post: http://lbadri.wordpress.com/2014/01/28/detecting-extra-fields-in-asp-net-web-api-request/

Getting information for rest template

I have seen a lot of code that uses this getForObject() method that takes a URI and replaces things in {} with the numbers at the end of the method.
My question is: what exactly IS http://localhost:8080/spring-rest/foos? Is it a file of some sort? What would it look like in this context.
In your example:
Foo foo = restTemplate.getForObject(URI, Foo.class, "1");
is actually trying to GET a REST resource with an HTTP request.
Under the covers:
The URL template is rendered to http://localhost:8080/sping-rest/foos/1
The HTTP client issues a GET request to http://localhost:8080/sping-rest/foos/1
The REST service can respond with a JSON representation of that resource, such as {"id":1,"content":"Hello, World!"}
The HTTP client converts that JSON response into an instance of Foo.class (using message converters)
You get an instance of Foo!
The spring.io website lists a lot of guides, you should definitely check them out! There's even a "Consuming a RESTful web service" guide.

How to send MultiPart HTTP request using Spring RestTemplate

I am new to Spring framework and i am learning.
My web applicaiton based out of Spring MVC needs to call a vendor service through RESTful interface.
I have current implementation for POST / GET for non multipart.
However i have a requirement to POST multipart form data consisting of JSON and Bytes
I am trying to see some smaple implementation online for reference but could not get one.
I need some idea about possible approach i can take to implement this using RestTemplate.
Thanks for reading.
if we want to send multipart form data in post request and with that if you also want some information in json format then you can create your REST call according to this. here #Consumes will say that this call will accept only MULTIPART_FORM_DATA, #Transactional is for starting new transaction.
Here i am accepting three path parameters which are
1) String jsonObj, this is a string or you can say json, in this JSON you can ask UI for required information like some tags, labels, etc
2) FormDataContentDisposition fileDetail, This will contain very basic detail of file, like fileName, contentType, etc
3) InputStream uploadedInputStream, this will contain some binary data, like image, videos, or any kind of files in byte format.
* Example *
#POST
#Path("/xyz")
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public VObject postMultiPartFormData(
#FormDataParam("jsonObj") String jsonObj,
#FormDataParam("mmFile") FormDataContentDisposition fileDetail,
#FormDataParam("mmFile") InputStream uploadedInputStream) {
return new VObject();
}
I hope this will help you.

Resources