如何在Impl处于远程状态时对Jersey POJO进行注释



我有两个JVM。

JettyJVM运行http请求,并有一个接口CarFacade,该接口使用RmiProxy FactoryBean支持到CoreJVM 中运行的CarFacadeImpl

<bean class="org.springframework.remoting.rmi.RmiProxyFactoryBeanFactory">
  <property name="serviceInterface" value="org.foo.CarFacade"/>
  <property name="serviceUrl" value="rmi://#{HOST}:1099/CarFacade"/>
</bean>

CoreJVM在spring容器中运行核心业务逻辑,并具有CarFacadeImpl

<bean id="carFacade" class="org.foo.impl.CarFacadeImpl"></bean>
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">  
  <property name="service" ref="carFacade"></property>  
  <property name="serviceInterface" value="org.foo.CarFacade"></property>  
  <property name="serviceName" value="CarFacade"></property>  
  <property name="replaceExistingBinding" value="true"></property>  
  <property name="registryPort" value="1099"></property>  
</bean> 

这个设置目前适用于flex/blazds,我的服务也很好地公开了。

有什么办法我也可以通过泽西岛揭露这件事吗?

我尝试了Impl上的注释(首选),但组件扫描没有找到注释(很明显,因为接口没有注释)所以我尝试在接口上添加注释,但jersey说它无法实例化接口。

// CarFacadeImpl.java - when I had the annotations on the class in the CoreJVM
@Path("car")
public class CarFacadeImpl implements CarFacade {
  @GET
  public String getName() {
    return "CarFacade";
  }
}
// CarFacade.java - When I had the annotations on the interface in JettyJVM
@Path("car")
public class CarFacade {
  @GET
  String getName();
}

我真的不想写一个额外的层只是通过休息暴露

我试过这里的例子http://www.webappsolution.com/wordpress/2012/03/23/one-java-service-pojo-for-amfxmljson-with-spring-blazeds-jersey-jax-rs/并且它们在没有RMI调用的情况下工作。

我找到了答案,至少泽西岛2.16。它确实需要在接口上添加注释,但这比创建一个全新的层要好得多

覆盖默认路径扫描注册并使用类似的东西注册:

// Jersey ResourceConfig
final ResourceConfig rc = new ResourceConfig();
// Get my Spring context
final ApplicationContext context = new ClassPathXmlApplicationContext("clientContext.xml");
// Use Springs class path scanner to find @Path annotated classes
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false) {
    @Override
    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
        return beanDefinition.getMetadata().isIndependent();
    }
};
scanner.addIncludeFilter(new AnnotationTypeFilter(Path.class));
// For each class found in package (and sub packages)
for (BeanDefinition bd : scanner.findCandidateComponents("example")) {
    try {
        // Get the class
        Class clazz = HttpServer.class.getClassLoader().loadClass(bd.getBeanClassName());
        // Get the proxy
        Object bean = context.getBean(clazz);
        if (bean != null) {
            // Register the proxy with the interface
            rc.register(bean, clazz);
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

最新更新