java - Camel cxf REST 请求未分隔查询参数

标签 java rest apache-camel cxfrs

我正在尝试通过本地 RESTful Web 服务发送一些消息。 URL 允许包含查询参数。我的 REST 方法处理器没有看到这些查询参数。有人看到我做错了什么吗?

我使用的是 Camel 2.16.0。

我的蓝图路由 XML 是:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel-cxf="http://camel.apache.org/schema/blueprint/cxf"
       xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0
                           http://www.osgi.org/xmlns/1.0.0/blueprint.xsd
                           http://camel.apache.org/schema/blueprint/cxf
                           http://camel.apache.org/schema/cxf/camel-cxf-blueprint.xsd">

  <bean id="issPreprocessor" class="com.rockwellcollins.railwaynet.messagerouter.routing.IssPreprocessor"/>
  <bean id="webServiceProcessor" class="com.rockwellcollins.railwaynet.messagerouter.routing.WebServiceProcessor"/>
  <bean id="packageWebServiceReplyForIss" class="com.rockwellcollins.railwaynet.messagerouter.routing.PackageWebServiceReplyForIss"/>

  <!-- These two cooperate to send web service requests to the mocked
       out web service substitute. The crewMock bean handles requests sent
       to the jetty service stand-in; the webService rsClient sends the
       request.
  -->
  <bean id="steve" class="messagerouter.EmployeeInfo">
    <argument value="Stephen"/>
    <argument value="D."/>
    <argument value="Huston"/>
    <argument value="1234"/>
  </bean>
  <bean id="crewMock" class="messagerouter.CrewServiceMock">
    <property name="info">
      <map>
        <entry key="rraa">
          <map>
            <entry key="steve" value-ref="steve"/>
          </map>
        </entry>
      </map>
    </property>
  </bean>
  <camel-cxf:rsClient id="webService" address="http://localhost:9000" serviceClass="com.rockwellcollins.railwaynet.messagerouter.routing.WebService" loggingFeatureEnabled="true"/>

  <camelContext id="rraaCamelContext" xmlns="http://camel.apache.org/schema/blueprint">
    <dataFormats>

      <json id="IssRequest" library="Jackson"/>
    </dataFormats>

    <!-- Mocked out web service -->
    <route id="CrewMock">
      <from uri="jetty:http://localhost:9000?matchOnUriPrefix=true"/>
      <to uri="cxfbean:crewMock"/>
    </route>

    <route id="rraaIss">
      <from uri="seda:from_rraa"/>
      <process ref="issPreprocessor"/>
      <unmarshal ref="IssRequest"/>
      <process ref="webServiceProcessor"/>
      <to uri="cxfrs:bean:webService"/>
      <process ref="packageWebServiceReplyForIss"/>
      <to uri="seda:to_rraa"/>
    </route>
  </camelContext>

</blueprint>

我设置请求的WebService类是:

public class WebServiceProcessor implements Processor {

    @Override
    public void process(Exchange exchange) throws Exception {
        exchange.setPattern(ExchangePattern.InOut);
        Message in = exchange.getIn();
        // Using proxy client API; needs the op name.
        in.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.FALSE);
        in.setHeader(CxfConstants.OPERATION_NAME, "verifyEmployee");
        VerifyEmployeeRequest req = in.getBody(VerifyEmployeeRequest.class);
        MessageContentsList params = new MessageContentsList();
        params.add(req.getHeader().getScac());
        params.add(req.getBody().getEmployeeID());
        params.add(req.getBody().getPin());
        params.add(req.getBody().getReason());
        in.setBody(params);
    }
}

我的服务调用接口(interface)类是:

@Path(value="/rest")
public interface WebService {

    @GET
    @Path(value="/authentication/{scac}/employees/{id}?pin={pin}&reason={reason}")
    @Produces({ MediaType.APPLICATION_JSON })
    public String verifyEmployee(@PathParam("scac") String scac,
                                 @PathParam("id") String id,
                                 @PathParam("pin") String pin,
                                 @PathParam("reason") String reason);
}

处理请求的类是:

@Path("/rest")
public class CrewServiceMock {

    // scac -> { id -> employeeInfo }
    private Map<String, Map<String, EmployeeInfo> > info;

    public Map<String, Map<String, EmployeeInfo> > getInfo()
    { return info; }
    public void setInfo(Map<String, Map<String, EmployeeInfo> > info)
    { this.info = info; }

    @GET
    @Path("/authentication/{scac}/employees/{id}")
        public Response getAuth(@PathParam("scac") String scac,
                                @PathParam("id") String id,
                                @QueryParam("reason") String reason,
                                @QueryParam("pin") String pin) {
        Map<String, EmployeeInfo> employees = info.get(scac);
        if (employees == null) {
            return Response.status(404).build();
        }
        System.out.println(employees);
        System.out.println("id is " + id);
        System.out.println("scac is " + scac);
        System.out.println("reason is " + reason);
        EmployeeInfo emp = (EmployeeInfo)employees.get(id);
        if (emp == null) {
            return Response.status(404).entity("{ message: \"id not found\" }").build();
        }
        return Response.status(200).entity("{ \"employeeID\": " + id + ", \"employeeName\": { \"first\": \"" + emp.getFirstName() + "\", \"middle\": \"" + emp.getMiddleName() + "\", \"last\": \"" + emp.getLastName() + "\" } }").build();
    }
}

当我运行此命令时,我在测试输出中看到以下内容:

[Camel (rraaCamelContext) 线程 #0 - seda://from_rraa] 信息 org.apache.cxf.interceptor.LoggingOutInterceptor - 出站消息

ID:1 地址:http://localhost:9000/rest/authentication/rraa/employees/steve%3Fpin=1234&reason=INIT Http 方法:GET 内容类型:application/xml

header :{OriginalHeader=[{name=VerifyEmployeeRequest, version=1, scac=rraa, timeSent=null, uuid=abcd-1234}],Content-Type=[application/xml],Accept=[application/json ]}

{史蒂夫=messagerouter.EmployeeInfo@1dd62581} id 是 steve?pin=1234&reason=INIT scac是rraa 原因为空

因此,查询参数显然没有被解析出来 - 它们附加到最后一个路径参数。我做错了什么吗?

最佳答案

您的接口(interface)有 4 个路径参数,而不是 2 个路径和 2 个查询参数,它应该是:

@Path(value="/rest")
public interface WebService {

    @GET
    @Path(value="/authentication/{scac}/employees/{id}")
    @Produces({ MediaType.APPLICATION_JSON })
    public String verifyEmployee(@PathParam("scac") String scac,
                             @PathParam("id") String id,
                             @QueryParam("pin") String pin,
                             @QueryParam("reason") String reason);
}

关于java - Camel cxf REST 请求未分隔查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33154583/

相关文章:

java - 文件指针的偏移对于多种编程语言是否可靠?

spring - “没有端点适配器”异常 - 带有 spring-boot 和 spring-ws 的 apache-camel

java - Camel : how to route data from process method to another pipeline?

Javascript:REST API Mongodb 未在 Postman 上连接

apache-camel - Apache Camel CxfRsEndpoint performInvocation 设置触发两次调用

java - 如果消除了对多重继承的限制,那么 Java 中的接口(interface)和抽象类有什么区别?

java - 如何从链表堆栈中推送或弹出

java - 在 Java-swing 中的菜单项上使用鼠标监听器

web-services - Spring RestTemplate Client - 连接被拒绝异常

php - 如何开始为php MVC自创框架写一个SDK