就像本教程(https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-edition/content/en/part1/chapter6/custom_marshalling.html)一样,您可能可以通过readFrom
从如下接口实现自定义解组器MessageBodyReader
:
Object readFrom(Class<Object>, Type genericType,
Annotation annotations[], MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream entityStream)
throws IOException, WebApplicationException {
try {
JAXBContext ctx = JAXBContext.newInstance(type);
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();
// replace all special characters
theString = theString.replaceAll("[\u0000-\u001f]", "");
return ctx.createUnmarshaller().unmarshal(theString);
} catch (JAXBException ex) {
throw new RuntimeException(ex);
}
}