Java 从url传递头/参数名称时,apache camel RouteBuilder不工作

Java 从url传递头/参数名称时,apache camel RouteBuilder不工作,java,apache-camel,dsl,servlet-3.0,Java,Apache Camel,Dsl,Servlet 3.0,我正在观看一个关于Servlet的camel示例,它与xml camel定义(camel context.xml)一起工作: 如果uri为/camel,我会得到“向uri添加名称参数,例如?name=yourname”,但是 使用“/camel?name=foo”(而不是“嗨,我是xxx,你好,foo,你好吗?***”)也会发生同样的情况 怎么了?webapplication同时使用(camel-config.xml和CamelRoutingInitializer类) 谢谢 罗比我想你的问题出在

我正在观看一个关于Servlet的camel示例,它与xml camel定义(camel context.xml)一起工作:

如果uri为/camel,我会得到“向uri添加名称参数,例如?name=yourname”,但是 使用“/camel?name=foo”(而不是“嗨,我是xxx,你好,foo,你好吗?***”)也会发生同样的情况

怎么了?webapplication同时使用(camel-config.xml和CamelRoutingInitializer类)

谢谢


罗比

我想你的问题出在
标题(“name”)上。isEqualTo(“name”)
。它期望头的值等于文本
“name”
。您应该简单地声明
标题(“名称”)
,就像在xml dsl中一样。

我认为您的问题在
标题(“名称”)。isEqualTo(“名称”)
。它期望头的值等于文本
“name”
。您应该简单地声明
标题(“名称”)
,就像在XMLDSL中一样

 <camelContext xmlns="http://camel.apache.org/schema/spring">

    <route id="helloRoute">
      <!-- incoming requests from the servlet is routed -->
      <from uri="servlet:hello"/>
      <choice>
        <when>
          <!-- is there a header with the key name? -->
          <header>name</header>
          <!-- yes so return back a message to the user -->
          <transform>
            <simple>Hi I am ${sysenv.HOSTNAME}. Hello ${header.name} how are you today? ****</simple>
          </transform>
        </when>
        <otherwise>
          <!-- if no name parameter then output a syntax to the user -->
          <transform>
            <constant>Add a name parameter to uri, eg ?name=foo</constant>
          </transform>
        </otherwise>
      </choice>
    </route>
@WebListener
public class CamelRoutingInitializer implements ServletContextListener {

    DefaultCamelContext camctx;
    ServletContext ctx;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ctx = sce.getServletContext();
        RouteBuilder builder = new RouteBuilder() {
            /**
             *like camel-config.xml but with Java DSL syntax 
             * @see http://camel.apache.org/java-dsl.html
             */
            @Override
            public void configure() throws Exception {
                from("servlet:camel")
                .choice()
                .when(header("name").isEqualTo("name"))
                .transform(body().append("Hi I am ${sysenv.HOSTNAME}. Hello ${header.name} how are you today? ****"))
                .otherwise()
                .transform(body().append("Add a name parameter to uri, eg ?name=yourname"));    
                ctx.log("** Route config ok");
            }
        };
        DefaultCamelContext camctx = new DefaultCamelContext();
        try {
            camctx.addRoutes(builder);
            camctx.start();
            System.out.println("** CAMEL STARTED...");
            ctx.log("** CAMEL STARTED...");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        if (camctx!=null && camctx.isStarted()){
            try {
                camctx.stop();
                ctx.log("** CAMEL STOPPED...");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }