1

I'm making tests with a REST service and I need to get a HttpServletResponse object from it.

When I call the function like this:

http://localhost:8080/wsService/api/servicetest/testify

The HttpServletResponse that I get is always null

@Path("/testify")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response Testify(HttpServletResponse response) throws Exception {
    try {
        return Utilities.getSvc(true, "OK");

    } catch (Exception e) {
        return Utilities.getSvc(false, "ERROR");
    }
}

Is there something I'm missing?

CyborgNinja23
  • 290
  • 12
  • 33
  • 1
    [This](https://stackoverflow.com/questions/20937362/what-objects-can-i-inject-using-the-context-annotation) might help. – Andrew S Dec 14 '17 at 17:07

1 Answers1

2

I finally did it, just needed to put the @Context annotation to the HttpServletResponse object and add a HttpServletRequest parameter:

@Path("/testify")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response Testify(@Context HttpServletResponse response, 
                        @Context HttpServletRequest request) throws Exception {
    try {
        return Utilities.getSvc(true, "OK");

    } catch (Exception e) {
        return Utilities.getSvc(false, "ERROR");
    }
}
CyborgNinja23
  • 290
  • 12
  • 33