我使用泽西 + 春天。我有实现 ContainerRequestFilter 的 Jersey 过滤器,我需要在我的 jersey 资源中传输对象。
例如:
@Provider
public class UnmarshalEntityFilter implements ContainerRequestFilter {
private static final Logger LOGGER = LoggerFactory.getLogger(UnmarshalEntityFilter.class);
@Override
public ContainerRequest filter(ContainerRequest containerRequest) {
final String xml = getRequestBody(containerRequest);
// Parse this xml to Object
// How I can add this Object to my request and get from Jersey Resource ?
return containerRequest;
}
private String getRequestBody(ContainerRequest request) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = request.getEntityInputStream();
StringBuilder sb = new StringBuilder();
try {
if (in.available() > 0) {
ReaderWriter.writeTo(in, out);
byte[] requestEntity = out.toByteArray();
sb.append(new String(requestEntity, "UTF-8"));
}
return sb.toString();
} catch (IOException ex) {
throw new ContainerException(ex);
}
}
}