java - @Post 方法在 @Path 中捕获括号在 Jersey 中不匹配

标签 java rest jersey jax-rs grizzly

我在获取 JAX-RS POST 方法与 Jersey 匹配时遇到问题。逐字路径工作正常(“/prefix/ABC/DEF”),但带括号的捕获(“/prefix/{alpha}/{beta}”)无法触发。以下是使用 Jersey 在服务器接口(interface)中定义的相关方法。

public interface CollectorEndpoint
{
    ...
    @POST
    @Path("/prefix/{alpha}/{beta}") //doesn't match
    @Consumes(MediaType.APPLICATION_JSON)
    Response method1(@PathParam("alpha") String alpha,
                     @PathParam("beta") String beta,
                     String jsonContent);
    @POST
    @Path("/prefix/ABC/DEF")        //works for that one specific case
    @Consumes(MediaType.APPLICATION_JSON)
    Response method2(String jsonContent);
    ...
}

在实现类中:

@Path("/collect")
public class RestCollectorEndpoint implements CollectorEndpoint {
    ...
    @Override
    public Response method1(@PathParam("alpha") String alpha,
                            @PathParam("beta") String beta,
                            String jsonContent) {...}

    @Override
    public Response method2(String jsonContent);
    ...
}

我收到以下日志:

Matching path [/prefix/notabc/notdef]
X-Jersey-Tracing-010: MATCH       [ ---- /  0.77 ms |  ---- %] Pattern [/getpattern1(/)?] is NOT matched
X-Jersey-Tracing-011: MATCH       [ ---- /  0.77 ms |  ---- %] Pattern [/getpattern2(/)?] is NOT matched
X-Jersey-Tracing-012: MATCH       [ ---- /  0.78 ms |  ---- %] Pattern [/getpattern3(/)?] is NOT matched
X-Jersey-Tracing-013: MATCH       [ 0.09 /  0.79 ms |  7.47 %] RequestMatching summary
X-Jersey-Tracing-014: RESP-FILTER [ 0.23 /  1.18 ms | 18.96 %] Filter by [org.glassfish.jersey.filter.LoggingFilter @76ccd017 #-2147483648]
X-Jersey-Tracing-015: RESP-FILTER [ 0.26 /  1.19 ms | 21.52 %] Response summary: 1 filters
X-Jersey-Tracing-016: FINISHED    [ ---- /  1.21 ms |  ---- %] Response status: 404/CLIENT_ERROR|Not Found
Date: Sun, 17 Apr 2016 18:19:08 GMT
Content-Length: 0

我是否缺少一些简单的东西,或者我是否需要以某种方式在某处启用更高级的模式匹配?

最佳答案

关于JAX-RS 2.0注释继承的部分规范非常清楚。请参阅下面的引用:

3.6 Annotation Inheritance

JAX-RS annotations may be used on the methods and method parameters of a super-class or an implemented interface. Such annotations are inherited by a corresponding sub-class or implementation class method provided that the method and its parameters do not have any JAX-RS annotations of their own. Annotations on a super-class take precedence over those on an implemented interface. The precedence over conflicting annotations defined in multiple implemented interfaces is implementation specific. Note that inheritance of class or interface annotations is not supported.

If a subclass or implementation method has any JAX-RS annotations then all of the annotations on the superclass or interface method are ignored. E.g.:

public interface ReadOnlyAtomFeed {
    @GET 
    @Produces("application/atom+xml")
    Feed getFeed();
}

@Path("feed")
public class ActivityLog implements ReadOnlyAtomFeed {
    public Feed getFeed() {...}
}

In the above, ActivityLog.getFeed inherits the @GET and @Produces annotations from the interface. Conversely:

@Path("feed")
public class ActivityLog implements ReadOnlyAtomFeed {
    @Produces("application/atom+xml")
    public Feed getFeed() {...}
}

In the above, the @GET annotation on ReadOnlyAtomFeed.getFeed is not inherited by ActivityLog.getFeed and it would require its own request method designator since it redefines the @Produces annotation.

For consistency with other Java EE specifications, it is recommended to always repeat annotations instead of relying on annotation inheritance.


由于某种原因,javax.ws.rs的唯一注释包注释为 @Inherited@Consumes@Produces .

关于java - @Post 方法在 @Path 中捕获括号在 Jersey 中不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36682011/

相关文章:

java - 在 apache wicket 中获取对象模型的原因是什么?

java - 如何在另一个 HashSet<String> 中正确传递/添加 HashSet<String> 中的字符串?

java - 如何在不使用servlet但使用JAX-RS的情况下将json对象从ajax发布到java类?

java - REST 文件上传 - 分段或仅在输入流上发送内容

java - 我对这种骡子 Jersey 流程做错了什么?

java - 如何在 Grizzly http 服务器中为 Jersey 设置模板基本路径?

java - 在java中使用字节

java - 如何从 pdfBox 导入 ImageIOUtil 和 PDFText2HTML 类

java - Java 中逐步更新对象的替代选项

ruby - 构建路由 API