首页 文章

Resteasy无法使用@ApplicationPath

提问于
浏览
1

我无法使用简单的@ApplicationPath注释使JAX-RS与Resteasy 2.3.5一起使用 . 这是我正在使用的代码:

@ApplicationPath("/rest")
public class MyApplication extends Application {
  @Override
  public Set<Class<?>> getClasses() {
    final Set<Class<?>> s = new HashSet<Class<?>>();
    s.add(ViewController.class);
    return s;
  }
}

@Path("/")
public class ViewController {
  @GET
  @Path("/test")
  public String test() {
    return "Yes!";
  }
}

在“/ uri-context / rest / test /”上请求抛出404.使用Jersey一切都无缝地工作 . 因为这是JAX-RS的一个非常重要的部分,出了什么问题呢?

目前我只使用了4个我想要的Resteasy库:

  • async-http-servlet-3.0-2.3.5.Final.jar

  • jaxrs-api-2.3.5.Final.jar

  • resteasy-jaxrs-2.3.5.Final.jar

  • scannotation-1.0.3.jar

尽管如此,放置所有库(resteasy-cdi-2.3.5.Final.jar除外)也无法解决问题 .

1 回答

  • 3

    注意 Jax-RS 1.0, pathslash

    @ApplicationPath("api") //NO slash
    public class MyApplication extends Application {
    }
    
    @Path("/users")  // YES slash
    public class ViewController {
    
      @GET
      @Path("all") //NO slash
      public String all() {
        return "Yes!";
      }
    }
    

    通常使用斜线可以使它更好 .


    JAX-RS 2的更新:

    在JAX-RS 2中,规范DOES表示将忽略 @ApplicationPath@Path 中的前导和尾随斜杠 .

    (3)If the resource class URI template does not end with a ‘/’ character
    then one is added during the concatenation.
    

    对于我在Jersey 2和Resteasy上测试的内容,现在已经得到了尊重 .

相关问题