Jersey 2 Client

编程入门 行业动态 更新时间:2024-10-04 19:23:15
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();
        }
    }
}

                    
                     
          

更多推荐

本文发布于:2023-07-09 10:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085603.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Jersey   Client

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!