Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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
Web services 如何使用ApacheCamel、CXF和Spring Boot实现webservice版本网关_Web Services_Spring Boot_Apache Camel_Cxf - Fatal编程技术网

Web services 如何使用ApacheCamel、CXF和Spring Boot实现webservice版本网关

Web services 如何使用ApacheCamel、CXF和Spring Boot实现webservice版本网关,web-services,spring-boot,apache-camel,cxf,Web Services,Spring Boot,Apache Camel,Cxf,我目前正在从事一个项目,在该项目中,我将ApacheCamel、CXF用于我的WebService实现。除此之外,我还使用SpringBoot(嵌入tomcat) 我为每个web服务版本开发了不同的CXF端点,可以按照以下方式访问它们 http://myserver.com/service/V1或http://myserver.com/service/V2。这一切都很好,除非我得到要求,使这些服务在一个url下工作http://myserver.com/service/CommonVersion

我目前正在从事一个项目,在该项目中,我将ApacheCamel、CXF用于我的WebService实现。除此之外,我还使用SpringBoot(嵌入tomcat)

我为每个web服务版本开发了不同的CXF端点,可以按照以下方式访问它们
http://myserver.com/service/V1
http://myserver.com/service/V2
。这一切都很好,除非我得到要求,使这些服务在一个url下工作
http://myserver.com/service/CommonVersion
并基于xpath将其路由到特定版本

我真的不知道如何配置web服务请求,然后将其从一个公共入口点发送到特定于版本的入口点

我想为我的应用程序提供一些设置: cxf-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring" xmlns:cxf="http://camel.apache.org/schema/cxf" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring  http://camel.apache.org/schema/spring/camel-spring.xsd  http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd">

  <cxf:cxfEndpoint id="serviceV1"
                 address="/service/V1" serviceName="s:Service" serviceClass="com.myservice.v1.service" xmlns:s="http://com.myservice/ServiceV1">
   <cxf:properties>
      <entry key="allowStreaming" value="false" />
   </cxf:properties>
  </cxf:cxfEndpoint>

  <cxf:cxfEndpoint id="serviceV2" address="/service/V2" serviceName="s:Service" serviceClass="com.myservice.v2.service" xmlns:s="http://com.myservice/ServiceV2">
   <cxf:properties>
      <entry key="allowStreaming" value="false" />
   </cxf:properties>
  </cxf:cxfEndpoint>
</beans>
我的应用程序配置如下:

@SpringBootApplication
@ImportResource({ "/cxf-context.xml" })
public class Application extends SpringBootServletInitializer {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

  @Override
  protected SpringApplicationBuilder configure(
        SpringApplicationBuilder application) {
    return super.configure(application).sources(Application.class);
  }

  @Bean
  public ServletRegistrationBean servletRegistrationBean() {
    CXFServlet servlet = new CXFServlet();
    return new ServletRegistrationBean(servlet, "/*");
  }
}

您肯定知道,使用CXF和Camel,服务的实现是由路由本身完成的

我将您称之为“ServiceV1”(“extends SpringRouteBuilder”)的名称简单地改名为“Routes”(因为所有的路由都在其中,而不仅仅是一个服务),并像这样实现它——这只是一个使事情变得现实的示例:

@Component
public class MyRoutes extends SpringRouteBuilder {
    public void configure() throws Exception {
        from("cxf:bean:serviceV1")
                .to("direct:v1");

        from("cxf:bean:serviceV2")
                .to("direct:v2");

        from("cxf:bean:serviceCommon")
                .choice()
                    .when(header("version").isEqualTo("v1"))
                        .to("direct:v1")
                    .otherwise()
                        .to("direct:v2")
                .end();

        from("direct:v1")
                .beanRef("paymentservicev1", "buy");

        from("direct:v1")
                .beanRef("paymentservicev2", "buy");

    }
}

在这里,我不切换xpath,而是切换头值——好吧,您明白了。这就是你们想要寻找的吗?

我发现了一篇关于类似问题的博文:但给出的解决方案依赖于jetty,它打开了两个端口。问题描述非常相似,我相信serviceCommon端点不必是cxf。它可以是一个NETY HTTP端点…在理论上是可行的,但是让我们考虑下面的场景:V1和V2是不兼容的,并且您将CXF请求映射到从包V1到另一个包V2的对象。您还希望启用XML验证(至少在QA中)。您应该在serviceCommon cxfEndpoint上使用什么数据格式类型?尽管您正在使用serviceCommon,但如何仍然验证V1和V2的请求?
@Component
public class MyRoutes extends SpringRouteBuilder {
    public void configure() throws Exception {
        from("cxf:bean:serviceV1")
                .to("direct:v1");

        from("cxf:bean:serviceV2")
                .to("direct:v2");

        from("cxf:bean:serviceCommon")
                .choice()
                    .when(header("version").isEqualTo("v1"))
                        .to("direct:v1")
                    .otherwise()
                        .to("direct:v2")
                .end();

        from("direct:v1")
                .beanRef("paymentservicev1", "buy");

        from("direct:v1")
                .beanRef("paymentservicev2", "buy");

    }
}