我用泽西春天.我有Jersey过滤器,它实现了ContainerRequestFilter,我需要在我的球衣资源中传输对象.
例如:
@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);
}
}
}