Java JAXB和Guice:如何集成和可视化?
我发现将JAXB与Guice结合使用是可能的,但具有挑战性:这两个库都在“争夺”对对象创建的控制权,您必须小心避免循环依赖关系,而且它可能会与所有JAXBJava JAXB和Guice:如何集成和可视化?,java,jaxb,guice,Java,Jaxb,Guice,我发现将JAXB与Guice结合使用是可能的,但具有挑战性:这两个库都在“争夺”对对象创建的控制权,您必须小心避免循环依赖关系,而且它可能会与所有JAXB适配器和Guice提供程序混为一谈。我的问题是: 您如何处理此配置?可以应用哪些一般策略/经验法则 你能给我指出一个好的教程或编写良好的示例代码吗 如何可视化依赖项(包括适配器和提供程序) 对于一些示例代码,这里完成了一些示例工作: 在“错”的那一行,输入推荐的那一行 我看起来像这样: @Provider public class JAXB
适配器和Guice提供程序混为一谈。我的问题是:
- 您如何处理此配置?可以应用哪些一般策略/经验法则
- 你能给我指出一个好的教程或编写良好的示例代码吗
- 如何可视化依赖项(包括
适配器
和提供程序
)
对于一些示例代码,这里完成了一些示例工作:
在“错”的那一行,输入推荐的那一行
我看起来像这样:
@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);
}
}
@Provider
公共类JAXBContextResolver实现ContextResolver{
私有JAXBContext上下文;
私有类[]类型={UserBasic.Class,UserBasicInformation.Class};
公共JAXBContextResolver()引发异常{
this.context=
新的JSONJAXBContext(
JSONConfiguration.natural().build(),类型);
}
公共JAXBContext getContext(类objectType){
/*
对于(类类型:类型){
如果(type==objectType){
返回上下文;
}
}//对于错误的类型,应该存在某种异常。
*/
返回上下文;
}
}
//我的资源方法:
@得到
@产生({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
公共JAXBElement get(@QueryParam(“userName”)字符串userName){
ObjectFactory ob=新的ObjectFactory();
UserDTO dto=getUserService().getByUsername(用户名);
如果(dto==null)抛出newnotfoundexception();
UserBasic ub=新的UserBasic();
ub.setId(dto.getId());
ub.setEmailAddress(dto.getEmailAddress());
ub.setName(dto.getName());
ub.setPhoneNumber(dto.getPhoneNumber());
返回ob.createUserBasic(ub);
}
//My Guice配置模块:
公共类MyServletModule扩展了ServletModule{
公共静态模块[]getRequiredModules(){
返回新模块[]{
新的MyServletModule(),
新建ServiceModule(),
新CaptchaModule()
};
}
@凌驾
受保护的void configureServlets(){
绑定(UserHttpResource.class);
bind(JAXBContextResolver.class);
与(GuiceContainer.class)一起提供(“/*”);
}
}
我没有jersey集成,只有原始XML,所以我应该如何调整它?