Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 当Impl是远程的时,如何注释Jersey POJO?_Spring_Rest_Jersey_Jax Rs_Rmi - Fatal编程技术网

Spring 当Impl是远程的时,如何注释Jersey POJO?

Spring 当Impl是远程的时,如何注释Jersey POJO?,spring,rest,jersey,jax-rs,rmi,Spring,Rest,Jersey,Jax Rs,Rmi,我有两个JVM JettyJVM 运行http请求,并具有一个接口CarFacade,该接口使用RmiProxyFactoryBean备份到CoreJVM中运行的CarFacadeImpl <bean class="org.springframework.remoting.rmi.RmiProxyFactoryBeanFactory"> <property name="serviceInterface" value="org.foo.CarFacade"/> &l

我有两个JVM

JettyJVM 运行http请求,并具有一个接口CarFacade,该接口使用RmiProxyFactoryBean备份到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>
我真的不想写一个额外的层仅仅为了通过rest暴露


我已经尝试了这里的例子,它们在没有RMI调用的情况下工作。

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

你好你用什么做元帅?无论如何,我认为无论您使用什么,您都需要在JettyJVM中实现该类(最后,您希望获得一个对象!)。如果使用例如gson,则不需要添加getter和setter,只需添加构造函数即可。在其他一些情况下,例如moxy,您至少需要setter和setter。@lrnzcig我们只是使用Jersey的默认JSON封送器,用
@XmlRootElement
注释类。肯定有办法使用RmiProxyFactoryBean吗?我不介意是否需要对泽西岛进行扩展。
// 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();
}
// 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();
    }
}