JAXB 和 Guice:如何集成和可视化?

发布于 2024-12-06 10:34:00 字数 286 浏览 0 评论 0 原文

我发现将 JAXB 与 Guice 一起使用是可能的,但具有挑战性:这两个库都在“争夺”对象创建的控制权,您必须小心避免循环依赖,并且它可能会与所有 JAXB 适配器变得混乱和 Guice Providers 之类的东西。我的问题是:

  • 你如何处理这个配置?可以应用哪些一般策略/经验法则?
  • 您能给我指出一个好的教程或写得好的示例代码吗?
  • 如何可视化依赖关系(包括AdaptersProviders)?

I find using JAXB together with Guice possible, but challenging: Both libraries "fight" for control over object creation, you have to be careful to avoid cyclic dependencies, and it can get messy with all the JAXB Adapters and Guice Providers and stuff. My questions are:

  • How do you deal with this configuration? What general strategies / rules of thumb can be applied?
  • Can you point me to a good tutorial or well written sample code?
  • How to visualize the dependencies (including the Adapters and Providers)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2024-12-13 10:34:00

对于一些示例代码,这里完成了一些示例工作: http://jersey.576304.n2.nabble.com/Injecting-JAXBContextProvider-Contextprovider-lt-JAXBContext-gt-with-Guice-td5183058.html

在“错误”的行?”,放入推荐行。

我看起来像这样:

@Provider 
public class JAXBContextResolver implements ContextResolver<JAXBContext> { 

    private JAXBContext context; 
    private Class[] types = { UserBasic.class, UserBasicInformation.class }; 

    public JAXBContextResolver() throws Exception { 
         this.context = 
       new JSONJAXBContext( 
         JSONConfiguration.natural().build(), types); 
     } 

    public JAXBContext getContext(Class<?> objectType) { 
        /* 
        for (Class type : types) { 
            if (type == objectType) { 
                return context; 
            } 
        } // There should be some kind of exception for the wrong type.
        */ 
        return context; 
    } 
} 

//My resource method: 

    @GET 
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 
    public JAXBElement<UserBasic> get(@QueryParam("userName") String userName) { 
        ObjectFactory ob = new ObjectFactory(); 
        UserDTO dto = getUserService().getByUsername(userName); 
        if(dto==null) throw new NotFoundException(); 
        UserBasic ub = new UserBasic(); 
        ub.setId(dto.getId()); 
        ub.setEmailAddress(dto.getEmailAddress()); 
        ub.setName(dto.getName()); 
        ub.setPhoneNumber(dto.getPhoneNumber()); 
        return ob.createUserBasic(ub); 
    } 

//My Guice configuration module: 

public class MyServletModule extends ServletModule { 


    public static Module[] getRequiredModules() { 
        return new Module[] { 
                new MyServletModule(), 
                new ServiceModule(), 
                new CaptchaModule() 
         }; 
    } 


    @Override 
    protected void configureServlets() { 
        bind(UserHttpResource.class); 
        bind(JAXBContextResolver.class);
        serve("/*").with(GuiceContainer.class); 
    } 
} 

For some sample code, some example work was done here: http://jersey.576304.n2.nabble.com/Injecting-JAXBContextProvider-Contextprovider-lt-JAXBContext-gt-with-Guice-td5183058.html

At the line that says "Wrong?", put in the recommended line.

I looks like this:

@Provider 
public class JAXBContextResolver implements ContextResolver<JAXBContext> { 

    private JAXBContext context; 
    private Class[] types = { UserBasic.class, UserBasicInformation.class }; 

    public JAXBContextResolver() throws Exception { 
         this.context = 
       new JSONJAXBContext( 
         JSONConfiguration.natural().build(), types); 
     } 

    public JAXBContext getContext(Class<?> objectType) { 
        /* 
        for (Class type : types) { 
            if (type == objectType) { 
                return context; 
            } 
        } // There should be some kind of exception for the wrong type.
        */ 
        return context; 
    } 
} 

//My resource method: 

    @GET 
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) 
    public JAXBElement<UserBasic> get(@QueryParam("userName") String userName) { 
        ObjectFactory ob = new ObjectFactory(); 
        UserDTO dto = getUserService().getByUsername(userName); 
        if(dto==null) throw new NotFoundException(); 
        UserBasic ub = new UserBasic(); 
        ub.setId(dto.getId()); 
        ub.setEmailAddress(dto.getEmailAddress()); 
        ub.setName(dto.getName()); 
        ub.setPhoneNumber(dto.getPhoneNumber()); 
        return ob.createUserBasic(ub); 
    } 

//My Guice configuration module: 

public class MyServletModule extends ServletModule { 


    public static Module[] getRequiredModules() { 
        return new Module[] { 
                new MyServletModule(), 
                new ServiceModule(), 
                new CaptchaModule() 
         }; 
    } 


    @Override 
    protected void configureServlets() { 
        bind(UserHttpResource.class); 
        bind(JAXBContextResolver.class);
        serve("/*").with(GuiceContainer.class); 
    } 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文