Apache FELIX JAX-RS更改上下文路径

时间:2015-06-04 15:26:07

标签: java rest jax-rs apache-felix

我目前正在开发一个REST Web服务,并在FELIX下以bundle的形式公开它。我使用JAX-RS连接器进行Web服务。 该服务工作正常,但如果我想访问资源,模板URL是

http://IP:PORT/services/path/to/my/resource

目标是更改上下文路径服务以使用URL访问资源 喜欢 http://IP:PORT/path/to/my/resource

我尝试更改管理员配置,因为它已在JAX-RS连接器的常见问题解答中描述,但我仍有问题

ServiceReference configurationAdminReference = bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
if(configurationAdminReference != null) {
    ConfigurationAdmin confAdmin = (ConfigurationAdmin) bundleContext.getService(configurationAdminReference);
    if(confAdmin != null) {
        Configuration configConnector = confAdmin.getConfiguration("com.eclipsesource.jaxrs.connector",null); 
        Dictionary<String, String> props = configConnector.getProperties()
        if (props == null) {
            Dictionary<String, String> props = new Hashtable<String,String>();
         }
        props.put("root","/");    
        configConnector.update(props);
    }
}

我已经看到有人在这个论坛上已经有这个问题,但就我而言,它并没有解决问题 我可以在felix web控制台中看到以下错误消息

错误:PID&#34; com.eclipsesource.jaxrs.connector&#34;绑定到&#34;文件:/ c:/Dev/Tools/Apache/Felix/bundle/mybundle-1.0.0.jar"但实际的托管服务是从&#34;输入流:publisher-4.3.jar&#34;捆绑

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

configConnector.getProperties()返回null时,您的代码会导致NullPointerException,因为您在if子句中声明了一个新变量props,而不是将新的哈希表实例分配给现有变量。

修复此问题后,您可能会遇到此问题:https://github.com/hstaudacher/osgi-jax-rs-connector/issues/114

以下代码有效:

    org.osgi.service.cm.Configuration configuration = configurationAdmin.getConfiguration("com.eclipsesource.jaxrs.connector", null);
    Dictionary props = configuration.getProperties();
    if (props == null) {
        props = new Hashtable();
    }
    props.put("root", "/jaxrs");
    configuration.update(props);