Solved

Set custom header in REST response

  • 10 January 2022
  • 2 replies
  • 81 views

In a REST service built in the Service Definer, I’d like to dynamically set a custom HTTP response header.

I can specify the header in the server’s response headers, but can’t figure out how to set its value in the REST service method Java code.

Anyone have any ideas?

icon

Best answer by Gert.K 10 January 2022, 08:44

View original

2 replies

Userlevel 1

Goodmorning Marc,

When you create an instance of a ResponseBuilder you can add headers to it. However make sure that you return the Response and not the ResponseBuilder. You can do that by calling the build() method of the ResponseBuilder like the last line below.

jakarta.ws.rs.core.Response.ResponseBuilder respBuilder = Response.status(200);
respBuilder = respBuilder.header("MyHeader", "MyHeaderValue");
respBuilder = respBuilder.entity("Happy new year");
return respBuilder.build();

Hope this is what you are looking for.

Thanks @Gert.K , I’ll give this a try!

Reply