Skip to main content

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?

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