ws

JAX-RS support injection

RsService.java pom.xml

Supported injection:

javax.servlet.ServletConfig

javax.servlet.ServletContext

javax.servlet.http.HttpServletRequest

javax.servlet.http.HttpServletResponse

javax.ws.rs.core.HttpHeaders

RsService.java

package com.training.ws;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;

@ApplicationPath("/")
@Path("/")
public class RsService extends Application {

    @Context
    private ServletConfig servletConfig;
    @Context
    private ServletContext servletContext;
    @Context
    private HttpServletRequest httpServletRequest;
    @Context
    private HttpServletResponse httpServletResponse;

    @GET
    public String hello() throws IOException {
        System.out.println("ServletConfig: " + servletConfig);
        System.out.println("ServletContext: " + servletContext);
        System.out.println(httpServletRequest);
        System.out.println(httpServletResponse);

        return "hello";
    }

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.training.rs</groupId>
    <artifactId>jax_rs_support_injection</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>