question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resteasy/Guice Unable to find contextual data of type: javax.servlet.ServletConfig

See original GitHub issue

I don’t use the default implementation of resteasy servlet dispatcher. I use filter dispatcher, so i need change how the api’s are read.

Here are how my filter dispatcher works: https://gist.github.com/stickfigure/5ff4b814b8a08e4e1f57

Here the code:

@Path("/swagger.{type:json}")
public class SwaggerResource  extends BaseApiListingResource {

    @Context
    ServletContext context;

    @GET
    @Produces({MediaType.APPLICATION_JSON})
    @ApiOperation(value = "The swagger definition in either JSON", hidden = true)
    public Response getListing(
            @Context Application app,
            @Context ServletConfig sc,
            @Context FilterConfig fc,
            @Context HttpHeaders headers,
            @Context UriInfo uriInfo,
            @PathParam("type") String type) {
        return getListingJsonResponse(app, context, mergeConfig(sc, fc), headers, uriInfo);
    }

    private ServletConfig mergeConfig(ServletConfig sc, FilterConfig fc) {
        try {
            sc.getInitParameter("test");
        } catch (Exception e) {
            return new ServletConfigWrapper(fc);
        }
        return sc;
    }

    private static class ServletConfigWrapper implements ServletConfig {

        private FilterConfig fc;

        public ServletConfigWrapper(FilterConfig fc) {
            this.fc = fc;
        }

        @Override
        public String getServletName() {
            return fc.getFilterName();
        }

        @Override
        public ServletContext getServletContext() {
            return fc.getServletContext();
        }

        @Override
        public String getInitParameter(String name) {
            return fc.getInitParameter(name);
        }

        @Override
        public Enumeration<String> getInitParameterNames() {
            return fc.getInitParameterNames();
        }
    }

}

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kaoskocommented, Apr 21, 2020

Same thing for Tapestry5 that runs as a filter. As an answer to @frantuma, it’ll basically happen in any environment where ServletConfig isn’t available (but injects a proxy for the ServletConfig to ApiListingResource). Figured I’ll do the same as @niom and create my own subclass instead.

0reactions
niomcommented, Mar 16, 2020

I had a similar problem with our project.

We are using Jersey JAX-RS configuration and using pure filter based configuration. When using pure filter based configuration there is no ServletConfig but instead of FilterConfig that can be injected.

Swagger has a class: ApiListingResource that is used as the listing client.

public class ApiListingResource extends BaseApiListingResource {

    @Context
    ServletContext context;

    @GET
    @Produces({MediaType.APPLICATION_JSON, "application/yaml"})
    @ApiOperation(value = "The swagger definition in either JSON or YAML", hidden = true)
    public Response getListing(
            @Context Application app,
            @Context ServletConfig sc, // this is not found!!
            @Context HttpHeaders headers,
            @Context UriInfo uriInfo,
            @PathParam("type") String type) {
        if (StringUtils.isNotBlank(type) && type.trim().equalsIgnoreCase("yaml")) {
            return getListingYamlResponse(app, context, sc, headers, uriInfo);
        } else {
            return getListingJsonResponse(app, context, sc, headers, uriInfo);
        }
    }
...

now with pure filter configuration the injection of the ServletConfig will fail because there is no such thing.

So what would need to happen is that swagger should provide a proper ResourceListing file that would contain FilterConfig as an alternative for the ServletConfig parameter.

So we ended up creating a own FilterApiListingResource that would work something like this:

@javax.ws.rs.Path("/swagger.{type:json|yaml}")
public class FilterApiListingResource extends BaseApiListingResource {

    @GET
    @Produces({"application/json", "application/yaml"})
    @ApiOperation(
        value = "The swagger definition in either JSON or YAML",
        hidden = true
    )
    public Response getListing(@Context Application app, @Context FilterConfig fc, @Context HttpHeaders headers, @Context UriInfo uriInfo, @PathParam("type") String type) {
        return StringUtils.isNotBlank(type) && type.trim().equalsIgnoreCase("yaml") ? this.getListingYamlResponse(app, fc.getServletContext(), null, headers, uriInfo) : this.getListingJsonResponse(app, fc.getServletContext(), null, headers, uriInfo);
    }
}

in this way have the FilterConfig. ServletContext will be null because one does not exist.

what we are not sure is what information is missing when the ServletContext is null in BaseApiListingResource.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to find contextual data of type: javax.servlet.http ...
In my experience the @Context is set on a per method basis. More like: @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.
Read more >
Unable to find contextual data of type: javax.servlet ...
It seems the new io.swagger.jaxrs.listing.ApiListingResource causes RestEasy 3.0.9 to require a ThreadLocal reference to ServletContext which ...
Read more >
Unable to find contextual data of type: javax.ws.rs.ext.Providers
Just using a library doing a ClientBuilder.newClient, the following occurs: org.jboss.resteasy.spi.LoggableFailure: Unable to find contextual data of type: ...
Read more >
Throws "Unable to find contextual data of type: javax.servlet ...
Throws "Unable to find contextual data of type: javax.servlet.ServletContext " Jboss, RestEasy, Swagger.
Read more >
Full text of "resteasy-reference-guide-en-US" - Internet Archive
Text media types and character sets. ... See Chapter 20, JAX-RS Content Negotiation for more details. resteasy. media, ... ServletConfig • javax.servlet.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found