I have a webservice set up using CXF, JAX-RS and Spring. I have the following method:
#GET
#Path("/getPayload")
#Produces("application/XML")
public Response makePayload(){
Payload payload = new Payload();
payload.setUsersOnline(new Long(200));
return Response.ok().entity(payload).build();
}
How can I get access to the HttpRequest object in my makePayload()?
Will a call to this method generate a Session, and if so, can I get a handle to it and will that session be persistent for all subsequent requests from the same client?
#Context can be used to obtain contextual Java types related to the request or response:
#GET
#Path("/getPayload")
#Produces("application/XML")
public Response makePayload(#Context Request request) {
//...
}
Related
I am to maintain a Spring Cloud Gateway project and totally new to it. I am to add a special org.springframework.cloud.gateway.filter.GlobalFilter which analyzes request and response data to implement some additional feature.
However what I get from the interface is org.springframework.http.server.reactive.ServerHttpRequest and corresponding response object, which does not have a getInputStream method like their servlet counterpart.
What I want to achieve is roughly like the following:
public class CustomGlobalFilter implements GlobalFilter {
#Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
//TODO: modify the two objects or wrap them in wrapper class...
Mono<Void> result = chain.filter(exchange);
byte[] requestData = getRequestData(request);//TODO: get request data from request object
byte[] responseData = getResponseData(response);//TODO: get response data from response object
//TODO: custom logic
return result;
}
}
Maybe caching the content in memory is defeating the purpose of this framework however use of servlet api is not an option for me.
Please anyone give me some advices about how to achieve this purpose?
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();
I am using REST API with spring.is it possible that request arrives from UI/CURL to following API with request parameter null?
#GET
#Path(/abc)
#Produces({ "application/xml", "application/json" })
Public Users getUsers(#Context HttpServletRequest request)
{
someOtherClassMethod(request);
}
should I put null check for request here or request would always be not null if its arrived here.
#Context can be used to inject 12 object instances related to the context of HTTP requests.
It behaves just like the #Inject and #Autowired annotations in Java EE and Spring respectively.
#Context HttpServletRequest request
Here bean is created , and so can never be null
No need of null check
Hope this answers
I'm at the beginning of creating a Webservice with Java.
I want to POST a XML-Request to a Restful Webservice and the Response should be a modified XML. So actually just the Root-Element of the Request should be changed and it should be added another element.
Request:
<Request>
<name>name</name>
</Request>
Response:
<Response>
<name>name</name>
<status>created</status>
</Response>
Currently I'm only returning the Request.
Which is the best/easiest way to modify the Request? Can I do it with JAXB?
public class Resource {
#POST
#Produces
#Consumes
public Request request(Request r) {
return r;
}
}
It seems you are using Jersey to expose the rest api. As RedFive mentioned there are plenty of examples available to build rest api using jersey(jax-rs implementation) on internet. I did a small POC while learning jersey. You can find a sample POST API implementation here. I am passing request bean(as Person class object). The request json/xml is unmarshalled to Person object. Instead of returning object of Request type, you have to return Response object. Hope this example helps you in learning jersey.
#POST
#Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
#Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response makeRequest(Request req) {
Response res = new Response();
res.setName(req.getName());
return Response.entity(res).status(Response.Status.CREATED).build();
}
One thing I want to point out that you may not return the status in the response body. You return 201(CREATED) http status code which resembles the same thing.
I am using Jeresy Jax-RS to build a web service. Now I need to get the url of the request with the port # if one exist.
So if my service runs on http://www.somelocation.com/web/services I want to capture the www.somelocation.com
How can I do this ?
You can add a UriInfo parameter to your operation. From there you can access the URL:
#POST
#Consumes({"application/xml", "application/json"})
public Response create(#Context UriInfo uriInfo, Customer customer) {
...
}