Skip to content

Jersey HttpServletResponse Injection #62

@scottwinkler

Description

@scottwinkler

From the docs, I can inject an HttpServletRequest using the following:

ResourceConfig app = new ResourceConfig()
    .packages("com.amazonaws.serverless.proxy.test.jersey")
    .register(new AbstractBinder() {
        @Override
        protected void configure() {
            bindFactory(AwsProxyServletRequestFactory.class)
                .to(HttpServletRequest.class)
                .in(RequestScoped.class);
            bindFactory(AwsProxyServletContextFactory.class)
                .to(ServletContext.class)
                .in(RequestScoped.class);
        }
    });

And I can access the HttpServletRequest using:

@Path("/my-servlet") @GET
public String echoServletHeaders(@Context HttpServletRequest context) {
    Enumeration<String> headerNames = context.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = headerNames.nextElement();
    }
    return "servlet";
}

However, I have not found a way to inject the HttpServletResponse. It would be nice to bind a factory method like:

bindFactory(AwsProxyServletResponseFactory.class)
                .to(HttpServletResponse.class)
                .in(RequestScoped.class);

So that I can access the HttpServletResponse using the @context object, e.g. something like:

@Path("/my-servlet") @GET
public String echoServletHeaders(@Context HttpServletRequest request, @Context HttpServletResponse) {
//stuff
}

If this is not possible, is there another way to inject the HttpServletResponse? I tried using the @context above the @path, but the HttpServletResponse always returns null. Here is a snippet of what I tried:

@Context private ServletContext myContext;
    @Context private HttpServletResponse httpResponse;
    @Path("/saml")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response processSAML(@Context HttpServletRequest httpRequest) {
//httpResponse is null

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions