REST call invoking Another REST call in jax rs and Spring - java

I should call one Rest call with some json payload after this it should invoke one more end point with the same payload (POST) call and it should give some response. I should capture that response .
How can I do that using Jax rs and Spring?

Related

Rest Service Post Calls Not working with Spring Webclient

I need to invoke a rest service asynchronously and I thought of using spring reactive's webclient instead of the AsyncRestTemplate. However my url is not getting invoked at all with the below code.
Mono<Test> asyncResponse = webClientBuilder.build().post().uri(url).contentType(MediaType.APPLICATION_JSON)
.header("h1", h1).header("h2", h2)
.body(BodyInserters.fromObject(request))
.retrieve().bodyToMono(Test.class);
However if I do the same synchronously everything works fine.
webClientBuilder.build().post().uri(url).contentType(MediaType.APPLICATION_JSON)
.header("h1", h1).header("h2", h2)
.body(BodyInserters.fromObject(request))
.exchange();`
What am I doing wrong?
exchange doesn't mean synchronous. It responds Mono. You need to subscribe() or block() your stream somewhere.
Difference with exchange and retrieve is : They differ in return types; the exchange method provides a ClientResponse along with its status, headers while the retrieve method is the shortest path to fetching a body directly. you can refer this

Why post request accept query string parameters?

Spring framework and java servlet accept query string parameters in the post request. How can we accept parameter only in form data while making the REST API with POST method?

Invoking web service from a MultivaluedMap jax-rs

I have a web service that takes a client request and sends it to a second web service. It takes the response of second web service and sends it to the client. Actually it's a gateway. Type of request is "form urlencoded". The gateway takes the request from client as below:
#WebMethod
#POST
#Path("/send")
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
String send(MultivaluedMap<String, String> encodedRequest, #Context HttpServletRequest httpServletRequest);
Now I have a MultivaluedMap and I want to invoke the second web service with this MultivaluedMap and without performing any process on it. The second web service consumes "application/x-www-form-urlencoded" too. Is there any way to invoke the second web service without performing any process on this MultivaluedMap?
To send a POST request using JAX-RS Client, you call buildPost(Entity<?> entity), where entity is the POST content.
The Entity has many useful helper methods, e.g. form(MultivaluedMap<String,String> formData):
Create an "application/x-www-form-urlencoded" form entity.
So, you write something like this:
Future<Response> response = client.target("http://example.com/foo")
.request()
.buildPost(Entity.form(encodedRequest))
.submit();

Resteasy delegate route to another route

I was wondering if there was a way to delegate to another route in RestEasy.
I.e., something along the lines of, begin in a method inside an RS:
#Path("/api")
public class Foo {
#POST
#Path("/foo")
public Response foo() {
return RestEasy.delegate("GET", "/api/bar");
}
}
Where delegate would return the exact same response as if I had made an HTTP GET request to api/bar, that is, will fall through the proper RS that handles that route, ideally refilling all the necessary request information (headers, params, payload).
I don't want an HTTP redirect as I want it to be transparent to the api user.
I see from the docs/source that org.jboss.resteasy.spi.HttpRequest interface you have access to has a forward method.
It takes a string which would be the path to your other endpoint, but it doesn't let you change the method type (post to a get). But then again neither does the RequestDispatcher forward method you have access to. You aren't allowed to modify the request or the response.
See here:
So really all you can do is directly call your service method or, use an HTTP client to call other REST endpoint inside foo and then stream that back to the client.

How can I trace out entire GET request from jersey api client call

I have the following code that calls a http GET request where I map the json result to a specific class of mine called MySpecificJsnoMappedResult.
MySpecificJsonMappedResult myResult =
jerseyWebResourceClient.path("stuff)
.queryParam(“param”, “stuff”)
<lots more query params here>
.get(MySpecificJsonMappedResult.class);
is there an easy way via the jersey client so that can I trace out what the actual http GET call is (with params) in the case (since I'm not using a "ClientResponse" method?
You can configure the LoggingFilter on the client.
client.addFilter(new LoggingFilter());
You're mentioning ClientResponse, so I'm guessing you're using Jersey 1. If you're using Jersey 2, you'd use
client.register(new LoggingFilter());

Categories