0

我正在尝试执行以下操作(很多细节,对不起......):

有一个 jax-rs 服务,@EJB以避免 jndi 查找。就像是

@Path("rest/my-path)
public class Service {

  @EJB
  private MyEJB me;

  @Path("foo")
  @GET
  public String foo() {
    return me.foo();
  }
}

使用CXFNonSpringJaxrsServlet和限制url-pattern. web.xml 应该是这样的:

<servlet>
    <servlet-name>my-servlet</servlet-name>
    <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
    <init-param>
        <param-name>jaxrs.serviceClasses</param-name>
        <param-value>
            com.example.Service
        </param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>my-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

所以理论上http://localhost:8080/my-app/rest/my-path/foo会调用,你猜对了,foo()

TomEE 的日志支持这一点:

设置服务器的发布地址为 /REST Application:

http://localhost:8080/my-app/

URI:http://localhost:8080/my-app/rest/my-path

获取http://localhost:8080/my-app/rest/my-path/foo

但实际上,servlet-mapping 和 jaxrs 解析器的组合使得

http://localhost:8080/my-app/rest/my-path/foo返回404

和(注意双/rest/rest/)

http://localhost:8080/my-app/rest/rest/my-path/foo actually invoke the method but MyEJB is null

Didn't find any combination of servlet-mapping and service path and jaxrs.address that makes the root url return the index.html and the correct service url is calling foo and MyEJB is not null

Any ideas om how to solve this?

4

1 回答 1

2

Remove this from your web.xml:

<servlet>
    <servlet-name>my-servlet</servlet-name>
    <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
    <init-param>
        <param-name>jaxrs.serviceClasses</param-name>
        <param-value>
            com.example.Service
        </param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>my-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

TomEE scans your application automatically and adds any REST controllers. During startup, the logs will contain the path to your REST url. You may need to add an Application.class like this:

@ApplicationPath("/rest-prefix")
public class ApplicationConfig extends Application {
    public Set<Class<?>> getClasses() {
        return new HashSet<Class<?>>(Arrays.asList(Service.class));
    }
}
于 2016-08-16T18:17:16.227 回答