【问题标题】:Camel Restlet - Context Path AmbiquityCamel Restlet - 上下文路径歧义
【发布时间】:2018-08-07 17:14:18
【问题描述】:

我有 Spring Boot Camel 应用程序,其中使用 camel-restlet 公开了其余 api

示例路线

@Component
public class AppRoute extends RouteBuilder{
    public void configure(CamelContext context){
       from("restlet:employee?restletMethods=GET").log("${body}");
    }
}

应用程序运行完美 (spring-boot:run)。但我无法找到 API 暴露在哪个路径下。日志没有信息。

我点击的每个 API 返回 404。日志显示路由已启动。它在哪条路径下运行。我该如何改变它?

注意:请不要建议任何基于 XML 的配置。我可以放在@Configuration 下的任何内容都是完美的

【问题讨论】:

    标签: rest servlets path apache-camel restlet


    【解决方案1】:

    我会使用像这样的 camel-restlet 组件支持的 Rest DSL

    restConfiguration().component("restlet").port(8080);
    
    rest("/rest")
       .get("employee")
       .route().log("${body}")
       .endRest();
    

    而且这条路由会监听下面的url http://localhost:8080/rest/employee

    编辑: 我想你可以在不使用 Rest DSL 的情况下做类似的事情

        String host = InetAddress.getLocalHost().getHostName();
        from("restlet:http://" + host + contextPath + "/employee?restletMethods=GET").log("${body}")
    

    端口和上下文路径可通过以下属性进行配置

    camel.component.restlet.port=8686
    server.servlet.context-path=/my-path
    

    上下文路径可以在routeBuilder中注入

    @Value("${server.servlet.context-path}")
    private String contextPath;
    

    【讨论】:

    • 我已经做出了选择。我现在改不了了。 reslet 给了我更好的控制
    • 是的,我知道,我在使用 Rest DSL 和 camel-servlet 时遇到过同样的情况。如果您使用裸组件,它会更强大,但更难配置 查看我的编辑以获取第二个选项
    【解决方案2】:

    根据the documentation,restlet端点定义中的URI格式应该如下:

    restlet:restletUrl[?options]
    

    其中restletUrl 应具有以下格式:

    protocol://hostname[:port][/resourcePattern]
    

    因此,在您的情况下,您可以通过以下方式定义 URI:

    from("restlet:http://localhost/employee?restletMethods=GET")
    

    这应该使端点在以下 URL 下可用:

    http://localhost/employee
    

    您可以测试的内容,例如在网络浏览器中。

    【讨论】:

    • 这是一种基本的方式。我不能在所有 API 中硬编码主机名和端口名称应该有一种方法可以全局注册 url 上下文。配置可能是
    【解决方案3】:

    使用此处描述的三种配置方法中的第一种: https://restlet.com/open-source/documentation/javadocs/2.0/jee/ext/org/restlet/ext/servlet/ServerServlet.html

    您应该能够使用组件对其进行自定义: https://restlet.com/open-source/documentation/javadocs/2.0/jee/api/org/restlet/Component.html?is-external=true

    具体参见 setServers() 方法(或 XML 等效方法)以更改主机名和端口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      相关资源
      最近更新 更多