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) {
...
}
Related
I'm encountering a truly inexplicable bug when using a Dropwizard server with custom JAX-RS resources registered with a Jersey server. The following are true:
Curling a nonexistent endpoint yields a 404, as expected.
Curling an existing endpoint with the wrong method (e.g. POST instead of GET) yields a 405, as expected.
Curling an existing endpoint with the right method yields the message "Couldn't find a span for the current request".
My endpoint handler code is never touched, as can be verified by setting breakpoints in the code.
Amazingly, if I move the entire path annotation onto my handler method instead of partly on the entire class, I get a 404 instead.
Here's an example of the format that gives the strange error message:
#Produces({MediaType.APPLICATION_JSON})
#Path("application/api/v1/")
public class GetConfigurationResource extends MyResource<Arg, Result> {
#GET
#Path("getConfiguration/{uuid}")
public Response handleHttpRequest(
#Context HttpHeaders headers, #Context UriInfo uriInfo, #PathParam("uuid") String uuid) {
return super.handleHttpRequest(headers, new Arg(), new Result());
}
}
Here's an example that gives a 404:
#Produces({MediaType.APPLICATION_JSON})
public class GetConfigurationResource extends MyResource<Arg, Result> {
#GET
#Path("application/api/v1/getConfiguration/{uuid}")
public Response handleHttpRequest(
#Context HttpHeaders headers, #Context UriInfo uriInfo, #PathParam("uuid") String uuid) {
return super.handleHttpRequest(headers, new Arg(), new Result());
}
}
Here's the curl command:
curl -X GET http://127.0.0.1:8080/application/api/v1/getConfiguration/5
I can reproduce this error even when using toy classes like this in my application. Every single endpoint gives the same result.
A simple Google search of the error message yields absolutely no results. So this seems like a completely novel error. I'm wondering what could possibly cause something like this, and how I could debug this inside Dropwizard/Jersey.
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 have an endpoint as:
#Path("/products")
#Produces({ MediaType.APPLICATION_JSON })
public interface Products {
#PUT
#Path("/{productId}")
....
}
I have a jax-rs client implemented for this service and have it imported in the another service that I am calling this from.
So I am calling the client as below from my second service
public String updateProduct(String productId){
..
return client.target(this.getBaseUrl()).path("products/").path(productId).request(MediaType.APPLICATION_JSON_TYPE).put(Entity.json(""), String.class);
}
If I have a product with slashes say "control/register app" , the service does not seem to take it well. I did encode the productId before making a call to the service and then decoded it once received. But that doesnt seem to work and I get a 404 not found. Any ideas? Thanks in advance
Using #Path("{productId : .+}") should work.
Let's say I have a Jersey JAX-RS api end-point for handling http://<some_path>/foo. Ignore the ....
#Path("foo")
public class FooResource
#GET
#Produces("application/json")
public response getMethod(...)
I want to create POST end-point for foo/{id}/bar, where id is a path parameter and there's a body associated with the HTTP POST.
Example: HTTP POST foo/1/bar with body: { data : "...." }.
How can I add this POST method within the FooResource class? I tried an inner class, but it didn't work when I tested with Postman.
#POST
#Path("{id}/bar")
#Produces("application/json")
public response myPostMethod(...)
You can have path at method level. This will have your post method accessible via /foo/{id}/bar
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) {
//...
}