@context
@Context injects information into Class field or a method parameter.
Provides Access to ServletConfig, ServletContext, HTTPServletRequest, HTTPServletResponse
Method Parameter injection example with HTTPServletRequest
Provides Access to ServletConfig, ServletContext, HTTPServletRequest, HTTPServletResponse
Method Parameter injection example with HTTPServletRequest
import java.util.HashMap; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.fasterxml.jackson.databind.ObjectMapper; @Path("sandbox") public class Sandbox { ObjectMapper mapper = new ObjectMapper(); @POST @Produces(MediaType.TEXT_PLAIN) public Response getIt(@Context HttpServletRequest request) { try { HashMap<String, Object> hm = new HashMap<String, Object>(); hm.put("Content Length", request.getContentLength()); hm.put("Content Type", request.getContentType()); hm.put("Character Encoding", request.getCharacterEncoding()); hm.put("Local Address", request.getLocalAddr()); hm.put("Parameter Map", request.getParameterMap()); return Response.status(Response.Status.OK).entity(mapper.writeValueAsString(hm)).build(); } catch (Exception e) { e.printStackTrace(); return Response.status(Response.Status.BAD_REQUEST) .entity("Error").build(); } } }
Class Field Injection Example with ServletContext
package test; import java.util.Enumeration; import java.util.HashMap; import javax.servlet.ServletContext; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.fasterxml.jackson.databind.ObjectMapper; @Path("sandbox") public class Sandbox { @Context ServletContext contxt; ObjectMapper mapper = new ObjectMapper(); @POST @Produces(MediaType.TEXT_PLAIN) public Response getIt() { try { HashMap<String, Object> hm = new HashMap(<String, Object> ); hm.put("Context Path", contxt.getContextPath()); hm.put("Major Version", contxt.getEffectiveMajorVersion()); hm.put("Servlet Info", contxt.getServerInfo()); Enumeration<String> en = contxt.getInitParameterNames(); while (en.hasMoreElements()) { String name = (String) en.nextElement(); System.out.println(name + "." + contxt.getInitParameter(name)); } return Response.status(Response.Status.OK) .entity(mapper.writeValueAsString(hm)).build(); } catch (Exception e) { e.printStackTrace(); return Response.status(Response.Status.BAD_REQUEST).entity("Error") .build(); } } }
Calss Feild Injection with UriInfo
package test; import java.util.HashMap; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import com.fasterxml.jackson.databind.ObjectMapper; @Path("sandbox") public class Sandbox { @Context UriInfo uriinfo; ObjectMapper mapper = new ObjectMapper(); @POST @Produces(MediaType.TEXT_PLAIN) public Response getIt() { try { HashMap<String, Object> hm = new HashMap<String, Object>(); hm.put("Absolute Path", uriinfo.getAbsolutePath().getPath()); hm.put("Request URI", uriinfo.getRequestUri()); hm.put("Path Segments", uriinfo.getPathSegments()); hm.put("Query Parameters", uriinfo.getQueryParameters()); return Response.status(Response.Status.OK) .entity(mapper.writeValueAsString(hm)).build(); } catch (Exception e) { e.printStackTrace(); return Response.status(Response.Status.BAD_REQUEST).entity("Error") .build(); } } }