我可以在using custom JAXBContext找到Jersey 2文档,但我找不到的是关于重用其JAXBContext的文档。
我希望能够在不发出HTTP请求的情况下编组/解组实体(例如,在类路径上解组某些XML文件,为数据库I / O编组/解组数据等)。
如何获取我的Jersey 2客户端已使用的JAXBContext
实例?
答案 0 :(得分:0)
我仍然没有想出如何获取 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
可用于从数据库等返回字符串或输入流,或用于写入文件系统或数据库。
答案 1 :(得分:0)
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();
}
}
}