【问题标题】:How to annotate Jersey POJO when Impl is remote?当 Impl 远程时如何注释 Jersey POJO?
【发布时间】:2015-03-02 06:49:11
【问题描述】:

我有 2 个 JVM。

JettyJVM 运行 http 请求并具有使用 RmiProxyFactoryBean 支持到在 CoreJVM 中运行的 CarFacadeImpl 的接口 CarFacade

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

核心JVM 在 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,并且我的服务可以很好地公开。

有什么方法我也可以通过 Jersey 公开这个吗?

我尝试使用 Impl 上的注释(首选),但组件扫描没有找到注释(显然因为接口没有它们) 所以我尝试使用接口上的注释,但球衣说它无法实例化接口。

// 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();
}

我真的很想不必为了通过 rest 公开而编写额外的层。

我已经尝试过http://www.webappsolution.com/wordpress/2012/03/23/one-java-service-pojo-for-amfxmljson-with-spring-blazeds-jersey-jax-rs/ 此处的示例,它们之间无需 RMI 调用即可工作。

【问题讨论】:

  • 您好!你用什么作为编组器?无论如何,我认为无论您使用什么,都需要在 JettyJVM 中实现该类(最后,您想获得一个对象!)。如果你使用例如gson 你不需要添加 getter 和 setter,只需要一个构造函数。在其他一些例如你至少需要二传手和二传手。
  • @lrnzcig 我们只是使用Jersey 的默认JSON 编组器用@XmlRootElement 注释类肯定有使用RmiProxyFactoryBean 的方法吗?我不介意是否需要对 Jersey 实施扩展。

标签: spring rest jersey jax-rs rmi


【解决方案1】:

我找到了答案,至少是 Jersey 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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    相关资源
    最近更新 更多