Jersey 2 Client - 如何获取JAXBContext(Jersey 2 Client - how to get JAXBContext)
我可以找到有关使用自定义JAXBContext的 Jersey 2文档,但我找不到有关重用其JAXBContext的文档。
我希望能够在不发出HTTP请求的情况下编组/解组实体(例如,在类路径上解组一些XML文件,为数据库I / O编组/解组数据等)。
如何获取Jersey 2 Client已经使用的JAXBContext实例?
I can find Jersey 2 documentation on using custom JAXBContext but what I can't find is documentation on reusing its JAXBContext.
I want to be able to marshal/unmarshal entities without making an HTTP request (e.g. unmarshal some XML files on the classpath, marshal/unmarshal data for database I/O, etc.).
How can I get the JAXBContext instance that my Jersey 2 Client is already using?
最满意答案
我还没弄明白如何获取 JAXBContext但我已经想出如何重用它:
client.target("file:///path/to/resource") .register((ClientRequestFilter) requestContext -> requestContext.abortWith(Response.ok() .entity(new FileInputStream(Paths.get(requestContext.getUri()) .toFile())) .build())) .request() .get(Planet.class);类似的ClientRequestFilter可用于从数据库等返回字符串或输入流,或用于写入文件系统或数据库。
import org.glassfish.jersey.jaxb.internal.AbstractJaxbProvider; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; public class JaxbContextStore { private JaxbProvider jaxbProvider = new JaxbProvider(); public JAXBContext getStoredJaxbContext(Class type) throws JAXBException { return jaxbProvider.getStoredJaxbContext(type); } private static class JaxbProvider extends AbstractJaxbProvider { public JaxbProvider() { super(null); } @Override public JAXBContext getStoredJaxbContext(Class type) throws JAXBException { return super.getStoredJaxbContext(type); } @Override public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { throw new UnsupportedOperationException(); } @Override public Object readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { throw new UnsupportedOperationException(); } @Override public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { throw new UnsupportedOperationException(); } @Override public void writeTo(Object o, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { throw new UnsupportedOperationException(); } } }
更多推荐
发布评论