Using Servlet Context in Quarkus

Quarkus includes the undertow extension, therefore it’s fully capable of delivering Servlet applications. In this example, we will show how to use the ServletContext in a REST Application. One challenge is that you cannot @Inject the ServletContext directly as you would do it in a Jave EE environment.

Nevertheless you can count on the JAX-RS javax.ws.rs.core.Context annotation to do that:

@Controller
@Path("/greeting/")
@RequestScoped
public class GreetingResource {

    @Context
    ServletContext servletContext;

}

 Or, as an alternative you can also do that:

@Controller
@Path("/rest/")
@RequestScoped
public class HelloResource {

    @GET
    @Path("/hello")
    @Produces({ MediaType.TEXT_PLAIN })
    public Response hello(@Context ServletContext servletContext) {
        // 
    }
}

 

Found the article helpful? if so please follow us on Socials