Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grizzly HttpServer: Set permanent header for every response

Tags:

java

cors

grizzly

I am using a Grizzly HttpServer and i want to add a specific header in every response. Specifically, i want to avoid CORS problems by adding an 'Access-Control-Allow-Origin' header.

So, ideally, i want something like this:

HttpServer server = GrizzlyServerFactory.createHttpServer(uri, crc);
server.setHeader("Access-Control-Allow-Origin" , "*");

Generally, i am looking for a solution that does not require that i have to manually insert this header in every request-response action.

Is there any way to do this?

like image 531
Mario Avatar asked Sep 09 '26 20:09

Mario


1 Answers

As @alexey said, there is no way (from the current Grizzly Server version) to do this. If anyone finds something else that works, i will gladly confirm it as an accepted answer.

The best alternative that works quite well is to extend the 'ContainerResponseFilter' class and override the 'filter' method.

Here is an example for 1.x API

Here is an example for 2.x API (minor changes)

like image 164
Mario Avatar answered Sep 11 '26 11:09

Mario



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!