user1491636
user1491636

Reputation: 2436

JAX-RS @Context HttpHeaders always null

I have a Spring Boot application using jax-rs with resteasy (3.0.24). I'm trying to get the HttpHeaders for a request as such:

    @DELETE
    @Path("/myendpoint")
    public Response myMethod(@Context HttpHeaders headers, @Context HttpServletRequest request) {
       // headers is always null
    }

The headers param is always null even though I'm making the request with multiple headers. As an alternative, I'm extracting them via the HttpServletRequest.getHeaderNames(), but I'd really like know why headers is not populated.

Upvotes: 0

Views: 3061

Answers (2)

user1491636
user1491636

Reputation: 2436

Found the (embarrassing, although I deflect the blame to the author:)) error. @Context HttpHeaders headers was using Spring's implementation and not that from jax-rs.

Upvotes: 2

sdfbhg
sdfbhg

Reputation: 109

You gotta get the headers using the @Context then check if the one one you want is there.

@Path("/users")
public class UserService {

@GET
@Path("/get")
public Response addUser(@Context HttpHeaders headers) {

    String userAgent = headers.getRequestHeader("user-agent").get(0);

    return Response.status(200)
        .entity("addUser is called, userAgent : " + userAgent)
        .build();

}

}

Upvotes: 0

Related Questions