同一WAR中的多个JAX-RS应用程序 [英] Multiple JAX-RS applications in the same WAR

查看:182
本文介绍了同一WAR中的多个JAX-RS应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

@ApplicationPath("/resourcesP")
public class RestfulPrediction extends Application {
    @Override
    public Set<Class<?>> getClasses() {
    Set<Class<?>> set = new HashSet<Class<?>>();
        set.add(PredictionsRS.class);
        return set;
    }
}

@ApplicationPath("/resourcesA")
public class RestfulAdage extends Application {
    @Override
    public Set<Class<?>> getClasses() {
    Set<Class<?>> set = new HashSet<Class<?>>();
        set.add(Adages.class);
        return set;
    }
}

两个不同的ApplicationPath和类如下。

Two different ApplicationPath and the class are as follows.

@Path("/")
public class service.Adages {}

@Path("/")
public class webservices.PredictionsRS {}

它们都是在不同的ApplicationPath中声明。我正在使用Jersey,web.xml中的配置看起来像

Both of them are declared in different ApplicationPath. I'm using Jersey and the config in web.xml looks like

  <servlet>  
    <servlet-name>jersey</servlet-name>  
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>  
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>
            service
            webservices
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>  

我得到了


严重:冲突的URI模板。 URI模板/用于根
资源类service.Adages和URI模板/转换为
相同的正则表达式(/.*)?

SEVERE: Conflicting URI templates. The URI template / for root resource class service.Adages and the URI template / transform to the same regular expression (/.*)?

为什么我有两个不同的ApplicationPath,这个例外是在启动时出现的?

Why if I have two different ApplicationPath this exception comes at startup ?

如果我拿出param-value的包,这个有效,另外,如果我更改了其中一个@Path注释,那么我的配置有问题吗?

If I take out a package in param-value this works, also if I change one of the @Path annotations this works, so it is a problem with my configuration ?

我正在使用Jersey 1.10。谢谢大家。

I'm using Jersey 1.10. Thanks all.

推荐答案

您没有在 web.xml中定义JAX-RS应用程序。请尝试以下操作:

You did not define your JAX-RS applications in your web.xml. Try the following:

<servlet>
    <servlet-name>full.name.RestfulAdage</servlet-name>
</servlet>

<servlet>
    <servlet-name>full.name.RestfulPrediction</servlet-name>
</servlet>

<servlet-mapping>
    <servlet-name>full.name.RestfulPrediction</servlet-name>
    <url-pattern>/resourcesP/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>full.name.RestfulPrediction</servlet-name>
    <url-pattern>/resourcesA/*</url-pattern>
</servlet-mapping>

并从代码中删除 @ApplicationPAth 注释。

我用Jersey 2.7,servlet容器3.0检查了上面的代码,它确实有效。如果还有这个bug,请尝试升级到Jersey 1.17(不应该改变Jersey 1.10的任何行为,而是修改bug)并最终使用servlet容器3.0。

I checked the above code with Jersey 2.7, servlet container 3.0 and it works. If still having that bug, try upgrading to Jersey 1.17 (which should not change any behavior from Jersey 1.10, and fix bugs instead) and eventually using also a servlet container 3.0.

更新

检查可能性后,以下配置与Jersey 1.17配合使用

After checking the possibilities the configuration below work with Jersey 1.17

   <servlet>  
    <servlet-name>jersey</servlet-name>  
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>  
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>
            com.koitoer.webservices
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

在泽西岛旧版本的规范中似乎存在一些错误标记为重复的端点。使用上面的配置加载两个端点没有任何问题。

It seems there is bug in the spec in older version of Jersey that kind of circle back the references and mark as duplicate endpoints. Using the configuration above both endpoints load without any problem.


8/04/2014 09:13:40 PM
com。 sun.jersey.server.impl.container.servlet.JerseyServletContainerInitializer
addServletWithApplication INFO:在
servlet上注册名为com.koitoer.webservices.chapter2.service2.RestfulPrediction的Jersey servlet
应用程序映射, / resourcesP / * ,与相同
名称的Application类

8/04/2014 09:13:40 PM com.sun.jersey.server.impl.container.servlet.JerseyServletContainerInitializer addServletWithApplication INFO: Registering the Jersey servlet application, named com.koitoer.webservices.chapter2.service2.RestfulPrediction, at the servlet mapping, /resourcesP/*, with the Application class of the same name

8/04/2014 09:13:下午40点com.sun.jersey.server.impl.container.servlet.JerseyServletContainerInitializer
addServletWithApplication INFO:在
注册名为com.koitoer.webservices.chapter2.RestfulAdage的Jersey servlet
应用程序servlet映射, / resourcesA / * ,与
同名的Application类

8/04/2014 09:13:40 PM com.sun.jersey.server.impl.container.servlet.JerseyServletContainerInitializer addServletWithApplication INFO: Registering the Jersey servlet application, named com.koitoer.webservices.chapter2.RestfulAdage, at the servlet mapping, /resourcesA/*, with the Application class of the same name

这篇关于同一WAR中的多个JAX-RS应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆