简体   繁体   中英

Springboot access header in Controller method

I have this controller method:

@GET
@Path("/watch/{groupId}")
public Response watch(@PathParam("groupId") UUID groupId, @RequestHeader("X-Session-ID") String sessionId) {

    LOGGER.info("Header passed: " + sessionId);
    
    /* Other stuff */
}

My expectation is that in my watch() method, I'll have the group ID (passed as a URL segment) as a UUID and the session ID (passed as a header) as a String in the corresponding variables. And while the group ID is passed, the header value isn't. This is my log excerpt:

2020-08-27 10:04:34.376 DEBUG 3228 --- [nio-9443-exec-9] o.a.coyote.http11.Http11InputBuffer      : Received [GET /watch/8de05f82-2565-4f16-a050-a211f3ce1c1b HTTP/1.1^M
X-Session-ID: 81DC3E39D65236CA09DDF97B3C9C7958075450D0051A9DFD856140530A18C447^M
Host: test.example.com:9443^M
Connection: Keep-Alive^M
Accept-Encoding: gzip^M
User-Agent: okhttp/4.8.1^M
^M
]
2020-08-27 10:04:34.377 DEBUG 3228 --- [nio-9443-exec-9] o.a.c.authenticator.AuthenticatorBase    : Security checking request GET /watch/8de05f82-2565-4f16-a050-a211f3ce1c1b
2020-08-27 10:04:34.377 DEBUG 3228 --- [nio-9443-exec-9] org.apache.catalina.realm.RealmBase      :   No applicable constraints defined
2020-08-27 10:04:34.377 DEBUG 3228 --- [nio-9443-exec-9] o.a.c.authenticator.AuthenticatorBase    : Not subject to any constraint
2020-08-27 10:04:34.385  INFO 3228 --- [nio-9443-exec-9] i.r.frontend.watcher.WatchController     : Header passed:
2020-08-27 10:04:34.392 DEBUG 3228 --- [nio-9443-exec-9] stomAnnotationTransactionAttributeSource : Adding transactional method 'findAll' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly

As you can see, the log entry for the session ID is not showing anything.

I am out of ideas as to why this is happening. Can someone help?

OK, I have figured out the problem here.

@GET is javax.ws.rs.GET.
@Path is javax.ws.rs.Path.
@PathParam is javax.ws.rs.PathParam.

But @RequestHeader is org.springframework.web.bind.annotation.RequestHeader.

That is, I was mixing Spring's REST and JAX-RS's. I replaced @RequestHeader (Spring) with @HeaderParam (JAX-RS) and now I get the HTTP header's value in my code.

Which is better, which one should I use etc. I'll figure out later. Right now the problem is solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM