2

Adding a response header to a controller is easyPeasy enough like:

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test(HttpServletResponse response)
{
 response.setHeader("specialheader", "Special Header");
 return "ok";
}

But that's not the case when the user tries to reach static content on the server (like a .css file), where I obviously don't want to create end points for each files.

So I tried to use a new class (@ControllerAdvice) which implements ResponseBodyAdvice, and overwriting the 'beforeBodyWrite' function I got the expected result for each queries which have end points.

The idea came from this link.

But when I try to reach a simple .css file on the server, the ControllerAdvice is not called, therefore the file opens in the browser without the expected headers set.

How can I add a general response header to every response going out of the server, including static files' responses?

iehrlich
  • 3,524
  • 4
  • 30
  • 42
Macskasztorik
  • 558
  • 1
  • 5
  • 16

1 Answers1

1

You have to apply a filter. This will apply to every HTTP Request's response.

You can find the exact answer here: How to add a filter class in Spring Boot?

Community
  • 1
  • 1
Mr.Fireman
  • 494
  • 1
  • 5
  • 16