2

Is there an option to specify a request header once in spring web RestController instead of doing it on every request?

e.q.

@RestController("workflowController")
public class MyClass{

public Value list(@RequestHeader(USER_ID_HEADER_PARAM) String user) {
    ...some code
}

public Workflow create(@RequestBody Workflow workflow, @RequestHeader(USER_ID_HEADER_PARAM) String user) {
    ... some code
}

}

the @RequestHeader(USER_ID_HEADER_PARAM) will be repeated in every request. is there a way to specity it in the @RestCotroller level or the class level?

Thanks

user1927865
  • 135
  • 1
  • 1
  • 8
  • Let's assume if you are able to do it at class level, where will you capture that header? It is not possible. because you need a variable to hold this header. That is exactly why RequestHeader has `@Target(ElementType.PARAMETER)` which meany you can use it only at parameter level. – pvpkiran Jun 20 '18 at 10:02
  • I think this has to be custom. There are some good ideas in the answer in this question: https://stackoverflow.com/q/36295095/4161471 – aSemy May 11 '22 at 10:14

1 Answers1

0

Use some kind of filter class that can be configured to wrap around your requests in your servlets based on the URL path.

Here is info about the generic Servlet API filter API: https://www.oracle.com/technetwork/java/filters-137243.html

If you're using Spring, there's another way to do it:

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#filters

https://www.baeldung.com/intercepting-filter-pattern-in-java

Charlie Reitzel
  • 809
  • 8
  • 13