I'm trying to call an endpoint/Resource from another resource
I guess I should use #Context and ResourceContext but not sure exactly how
here's a sample code
#Path("/A")
public class ClassA {
#Context
ResourceContext resourceContext;
#GET
public void functionA(){
// call classB.functionB:
// Response response = resourceContext.getResource(ClassB.class).functionB();
}
#Path("/B")
public class ClassB {
#Context
ResourceContext resourceContext;
#GET
public void functionB(){
// do something
}
}
looking at this post looks like I should call the second endpoint (from classA) with
Response response = resourceContext.getResource(ClassB.class).functionB();
But since resourceContext is null it throws an error.
so how should I initialize it or is there anything else I'm missing here?
Thanks in advance
Related
I have a JAX-RS rest client with following definition.
#PUT
#Path("/payments/{paymentId}/operation")
void call(#PathParam("paymentId") String paymentId, Object request);
I would like to intercept this outgoing request and read the paymentId value in a ClientRequestFilter.
#Provider
public class TracingInterceptor implements ClientRequestFilter {
public static final String PAYMENT_ID = "paymentId";
#Context
UriInfo info;
#Override
public void filter(ClientRequestContext requestContext) {
// read paymentId
}
}
I tried with UriInfo which works for ContainerRequestFilter but the context is not available in a ClientRequestFilter.
How can I read a specific path parameter in a ClientRequestFilter?
Use ClientRequestContext#getUri: https://docs.oracle.com/javaee/7/api/javax/ws/rs/client/ClientRequestContext.html#getUri.
Is it possible to access the Request object in a REST method under JAX-RS?
I just found out
#Context Request request;
On JAX-RS you must annotate a Request parameter with #Context:
#GET
public Response foo(#Context Request request) {
}
Optionally you can also inject:
UriInfo
HttpHeaders
SecurityContext
HttpServletRequest
To elaborate on #dfa's answer for alternatives, I find this to be simpler than specifying the variable on each resource method signature:
public class MyResource {
#Context
private HttpServletRequest httpRequest;
#GET
public Response foo() {
httpRequest.getContentType(); //or whatever else you want to do with it
}
}
In embedded Jersey I can register a Binder to put in some resources that I can eventually access using #Context
However, those things I put in are more global and not on a per request level. I do know I can do it with some property mapping, but I would rather do it through #Context with class like Response foo(#Context HttpServletRequest)
I tried the setRequestScopedInitializer() but it does not put them in as expected and following their example with the Ref gives me a null pointer exception
Is there any way of doing this?
Here's how I eventually did it, but I don't like that I used a named property
RoutingContext was the type I wanted to inject
public class RoutingContextFactory implements
Supplier<RoutingContext> {
#Inject
private ContainerRequest request;
#Override
public RoutingContext get() {
return (RoutingContext) request.getProperty(RoutingContext.class.getName());
}
}
My binder
public class MyBinder extends AbstractBinder {
#Override
protected void configure() {
bindFactory(RoutingContextFactory.class)
.to(RoutingContext.class)
.proxy(true)
.proxyForSameScope(false)
.in(RequestScoped.class);
}
}
Initialized by
final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(applicationClass);
resourceConfig.register(new MyBinder());
Loaded by
final ContainerRequest request = new ContainerRequest(...
request.setProperty(RoutingContext.class.getName(), routingContext);
Used by
#GET
#Produces(MediaType.TEXT_PLAIN)
public String hello(
#Context final RoutingContext routingContext) {
return "Hello"
+ routingContext;
}
I still wish there was a way for me to just go request.register(routingContext). I opened up https://github.com/jersey/jersey/issues/3682 for this.
Let's say I have this code:
#ApplicationPath("...")
public class MyApp extends ResourceConfig {
public SalesLayerApplication() {
this.register(HeaderInjecterFilter.class);
this.register(Test.class);
}
}
#PreMatching
public class HeaderInjecterFilter implements ContainerRequestFilter {
#Override
public void filter(final ContainerRequestContext crc) throws IOException {
crc.getHeaders().add("foo", "bar");
}
}
#Path("/test")
public class Test {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String dump(#Context final HttpServletRequest request) {
return request.getHeader("foo");
}
}
I was expecting to call the rest entry point /test and to retrieve the string bar.
But all I see is null
If I use #HeaderParam("foo") I correctly retrieve the variable, but I need to access throug the #Context HttpServletRequest.
Why would you expect that adding headers to the ContainerRequestContext would also add it to the HttpServletRequest? These are completely unrelated entities. Try injecting HttpHeaders or you can also inject the ContainerRequestContext directly.
I have a resource for rest API which uses a service.
This service has a constructor with parameters.
I want to test this resource and to mock this service.
This Question: How to pass parameters to REST resource using Jersey 2.5
wasn't helpful because they used #Inject and I cannot use it.
Any suggestions?
The second question is how do I pass parameter to test this resouce:
My code is:
#Path("/2/{subversion: [0-3]}/users")
public class UserResource {
Logger log = Logger.getLogger(UserResource.class);
private MyService service;
public void setService(Service ser) {
this.service = ser;
}
#Context HttpServletRequest currentRequest;
#GET
#Produces("application/json")
public Response getUsers(#Context HttpHeaders httpHeaders, #Context UriInfo
uriInfo) {
// my function
}
}
How can I pass "httpHeaders" and "UriInfo".
My test looks like this:
Response response = target("/2/0/users/").request().get();
Users users = response.readEntity(Users.class);
assertNotNull(users);
For the service, it's good practice to either inject through the constructor or setter. This makes it easy to mock and pass in during unit testing. As for the mocking, you should use a framework like Mockito. Then you can do stuff like
MyService service = Mockito.mock(MyService.class);
when(service.getObject()).thenReturn(new Object());
HttpHeaders headers = Mockito.mock(HttpHeaders.class);
when(headers.getHeaderString("X-Header")).thenReturn("blah");
UriInfo uriInfo = Mockito.mock(UriInfo.class);
when(uriInfo.getRequestUri()).thenReturn(URI.create("http://localhost"));
Then you can just pass all these mocks to your resource class when UNIT testing.
For INTEGRATION testing you won't need to mock the headers or uriinfo. The actual ones will get passed in. But you can still mock the service if you want. Here's an example
public class MockServiceTest extends JerseyTest {
public static interface Service {
String getMessage(String name);
}
#Path("message")
public static class MessageResource {
private final Service service;
public MessageResource(Service service) {
this.service = service;
}
#GET
public String get(#QueryParam("name") String name,
#Context HttpHeaders headers,
#Context UriInfo uriInfo) {
String nameQuery = uriInfo.getQueryParameters().getFirst("name");
String header = headers.getHeaderString("X-Header");
assertNotNull(nameQuery);
assertNotNull(header);
return service.getMessage(name);
}
}
private Service service;
#Override
public ResourceConfig configure() {
service = Mockito.mock(Service.class);
return new ResourceConfig().register(new MessageResource(service));
}
#Test
public void testIt() {
Mockito.when(service.getMessage("peeskillet")).thenReturn("Hello peeskillet");
Response response = target("message").queryParam("name", "peeskillet").request()
.header("X-Header", "blah")
.get();
assertEquals(200, response.getStatus());
assertEquals("Hello peeskillet", response.readEntity(String.class));
}
}