0

I created a web service on which I used @Stateless annotation i.e

 @Stateless
 @Path("/boo/too")
 public class RestController {

@Context 
private HttpServletRequest request;

@Context
private ServletContext context;


@GET
@Path("/coo")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public GetResObj getFuncName(
        @HeaderParam("foo") String foo,
        @QueryParam("boo") String boo
        ) throws Exception{ 

    MyClass className =(ClassCast) request.getSession().getAttribute("myClassInstance");
}

Now from some reading i have done and the accurate answer to this question : question

, I understand that a Stateless object is an object which can have variables but it's immutable (can't hold any state). When using the @Stateless annotation the request.getSession() part of code throws a nullPointerException . When i remove the @Stateless annotation the request.getSession() works fine .

Can you see why is this happening ?

Alex Lemesios
  • 522
  • 9
  • 22
  • 1
    Just a guess - you might try to pass the request as context parameter to the method - someting like `@GET @Produces(MediaType.APPLICATION_JSON) @Path("search") public Something searchDbViews( @Context HttpServletRequest req, @Context SecurityContext sec, ... ` – Gyro Gearless Nov 23 '18 at 10:37
  • @GyroGearless .So how should i try to pass it instead of context ? – Alex Lemesios Nov 23 '18 at 10:53

1 Answers1

0

It seems HttpServletRequest instance didn't get injected into request field ever.

Can you check injecting HttpServletRequest into resource method parameter list e.g. final @Context HttpServletRequest request.

Bhogesh
  • 11
  • 2