I want to add custom header in spring boot JAX-RS application. I know couple of ways to add headers but my use case is not able to use these use cases.
My use case is that I want to create a random string on the one of the class and then add it to the header at the same time and move on.
These are some ways to add response header.
1.
`@Produces(MediaType.APPLICATION_JSON)
public UserClass getValues(@Context HttpHeaders header, @Context HttpServletResponse response) {
response.setHeader("yourheadername", "yourheadervalue");
... }`
2.
`@GET
@Produces({ MediaType.APPLICATION_JSON })
@Path("/values")
public Response getValues(String body) {
//Prepare your entity
Response response = Response.status(200).
entity(yourEntity).
header("yourHeaderName", "yourHeaderValue").build();
return response;
}`
But none of this solves my use case.
Let's say in my class I generated one string and wanted to add in the response header like this
@Component
public class AsyncPublisher{
public void publishLogs(Object request, Object response, Object serviceBin, long elapsedTime ){
String headerValue = "headerValue";
*// get response header list here and add header like this*
// responseHeaders.add("Custom-Header", headerValue)
}
}
Anyone know how to do this Cause above all three method does not solve this purpose.