在我的Jax-Rs(Jersey + Jetty)应用程序中,我已经为响应添加了一些自定义标头,以便我可以在客户端使用它们。我正在使用ResponseBuilder来实现这一目标。
@Provider
public class CustomExceptionMapper implements ExceptionMapper<CustomException> {
private static final Logger LOGGER = LoggerFactory.getLogger(CustomExceptionMapper.class);
@Context
private HttpHeaders headers;
@Override
public Response toResponse(CustomException exception) {
String errorKey = null;
String errorArgs[] = null;
// log stack trace
LOGGER.error("An error occurred during the operation.", exception);
if (exception instanceof MetadataServerException) {
errorKey = ExceptionMapperUtil.checkForDBException(exception.getCause());
if (errorKey != null) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(MessageSourceUtils.getMessage(errorKey, errorArgs, ExceptionMapperUtil.getLocale(headers))).type(MediaType.TEXT_PLAIN).build();
}
}
if(ErrorCodes.INTERNAL_SERVER_ERROR.equals(exception.getMessageKey()) &&
(exception.getMessageArguments() == null || exception.getMessageArguments().length==0) ){
exception.setMessageArguments(new Object[]{"while executing the operation"});
}
// prepare response to be sent to client
return Response.status(ResponseCodeMapper.mapToStatusCode(exception)).
entity(exception.getMessage(ExceptionMapperUtil.getLocale(headers))).
type(MediaType.TEXT_PLAIN).header("errorCode", exception.getMessageKey()).
build();
}
}
而且,我已经确认响应对象正在构建正确。但是,由于某种原因,返回的响应不包含我的自定义标头。关于出了什么问题的任何线索?