【问题标题】:HttpServletRequest casting fails when running Camel outside of Intellij在 Intellij 之外运行 Camel 时,HttpServletRequest 强制转换失败
【发布时间】:2016-12-15 15:19:39
【问题描述】:

我需要在 Camel 中实现一个简单的 Http-Proxy,来记录传入 WebService 请求的远程 IP 地址。

所以我已经定义了我的路线:

from("jetty:http://0.0.0.0:" + 8081 + "?matchOnUriPrefix=true&optionsEnabled=true")
  .streamCaching()
  .process(wiresharkInboundLogger)
  .to("jetty:http://localhost:" + 8080 + "?bridgeEndpoint=true&throwExceptionOnFailure=false");

我有我的处理器“wiresharkInboundLogger”:

@Override
public void process(Exchange exchange) throws Exception {

  // HttpServletRequest
  HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);

  if (request == null) {
    LOG.warn("No HttpServletRequest available!");
  } else {
    LOG.info("Client IP: " + request.getRemoteAddr());
  }
}

在 Inttelij 中运行时,这就像一个魅力。一旦我通过控制台命令(“java -jar my-camel-app.jar”)在 Intellij 之外运行相同的应用程序,HttpServletRequest 的转换就会返回“null”,当由相同的触发时来自 SOAP UI 的请求。

我已经使用以下 maven-plugin 打包了 jar:

<plugins>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <mainClass>my.wireshark.Wireshark</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.0</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>
</plugins>

有什么可能导致这种奇怪行为的想法吗?

【问题讨论】:

  • 在制作 uber JAR 时要小心,因为这样会搞砸。请参阅此常见问题解答:camel.apache.org/how-do-i-use-a-big-uber-jar.html
  • 切换到 maven-shade-plugin。没变。 HttpServerletRequest 的转换仍然在 Intellij 之外返回 null。

标签: java maven intellij-idea apache-camel jetty


【解决方案1】:

Shade 插件需要合并 META-INF 条目。如果不只使用一个类型转换器注册表,而是需要所有类型转换器。

【讨论】:

    【解决方案2】:

    这里有两种可能的解决方案:

    1) 需要一个额外的转换器来生成正确的清单:

    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer">
    </transformer>
    

    2) 改用 appassembler-plugin,但这会生成相当“胖”的应用程序: http://www.mojohaus.org/appassembler/appassembler-maven-plugin/usage-program-scripts.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多