在Jersey中不匹配@Path的@Post方法在@Path中捕获括号 [英] @Post method with capturing parenthesis in @Path not matched in Jersey

查看:72
本文介绍了在Jersey中不匹配@Path的@Post方法在@Path中捕获括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使JAX-RS POST方法与Jersey匹配.逐字记录路径可以正常工作("/prefix/ABC/DEF"),但是带括号的捕获("/prefix/{alpha}/{beta}")无法触发.以下是使用Jersey的服务器界面中定义的方法.

I am having trouble getting JAX-RS POST methods to match with Jersey. Verbatim paths work fine ("/prefix/ABC/DEF"), but parenthesised captures ("/prefix/{alpha}/{beta}") fail to trigger. Here are the methods in question, as defined in the server interface, using Jersey.

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

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

Is there something simple that I am missing, or do I need to somehow enable fancier pattern matching somewhere?

推荐答案

来自我的原始答案:

用于参数化Jersey方法的Jax-RS 2.0批注需要放置在实现的旁边-如果仅将它们放置在父接口中,则它们不会被拾取(至少在Jersey 22.2.2中).

Jax-RS 2.0 annotations for parameterised Jersey methods need to be placed next to the implementation - if they are just placed in a parent interface they are not picked up (at least in Jersey 22.2.2).

Refinement:仅当覆盖/实现方法上已有Jax-RS 2.0注释时,以上情况才适用-在这种情况下为@PathParam.在这种情况下,规范要求所有父Jax-RS 2.0批注都将被忽略.感谢peeskillet指出了这一点.

Refinement: the above is only true if there is already a Jax-RS 2.0 annotation on the overriding/implementing method - in this case, @PathParam. In this case, the spec mandates that any parent Jax-RS 2.0 annotations are disregarded. Thanks to peeskillet for pointing this out.

这可能是因为@Path不是@Inherited批注(可能是因为继承的批注可能由于多重继承而出现问题).

This may be because @Path is not an @Inherited annotation (possibly because inherited annotations can be problematic because of multiple inheritance).

Refinement:Jax-RS 2.0规范似乎要求所有实现/重写的方法都将从父级继承Jax-RS注释,并且未指定此方法必须基于标准注释继承,因此可能还有其他用途在泽西岛上班.

Refinement: The Jax-RS 2.0 spec seems to mandate that any implementation/overridden methods will inherit Jax-RS annotations from the parent, and doesn't specify that this will necessarily be based on standard annotation inheritance, so perhaps something else is at work in Jersey.

这篇关于在Jersey中不匹配@Path的@Post方法在@Path中捕获括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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