@Post 在@Path 中捕获括号的方法在 Jersey 中不匹配
@Post method with capturing parenthesis in @Path not matched in Jersey
我无法让 JAX-RS POST 方法与 Jersey 匹配。逐字路径工作正常(“/prefix/ABC/DEF”),但带括号的捕获(“/prefix/{alpha}/{beta}”)无法触发。以下是使用 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);
...
}
并在此处执行 class:
@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 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).
细化:仅当 overriding/implementing 方法上已有 Jax-RS 2.0 注释时,以上内容才成立 - 在本例中为 @PathParam
。在这种情况下,规范要求忽略任何父 Jax-RS 2.0 注释。感谢 peeskillet 指出这一点。
This may be because @Path
is not an @Inherited
annotation (possibly because inherited annotations can be problematic because of multiple inheritance).
改进:Jax-RS 2.0 规范似乎强制要求任何 implementation/overridden 方法都将从父级继承 Jax-RS 注释,并且没有指定这必须基于标准注释继承,所以也许泽西岛有其他事情在起作用。
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
package annotated with @Inherited
are @Consumes
and @Produces
的唯一注释。
我无法让 JAX-RS POST 方法与 Jersey 匹配。逐字路径工作正常(“/prefix/ABC/DEF”),但带括号的捕获(“/prefix/{alpha}/{beta}”)无法触发。以下是使用 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);
...
}
并在此处执行 class:
@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 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).
细化:仅当 overriding/implementing 方法上已有 Jax-RS 2.0 注释时,以上内容才成立 - 在本例中为 @PathParam
。在这种情况下,规范要求忽略任何父 Jax-RS 2.0 注释。感谢 peeskillet 指出这一点。
This may be because
@Path
is not an@Inherited
annotation (possibly because inherited annotations can be problematic because of multiple inheritance).
改进:Jax-RS 2.0 规范似乎强制要求任何 implementation/overridden 方法都将从父级继承 Jax-RS 注释,并且没有指定这必须基于标准注释继承,所以也许泽西岛有其他事情在起作用。
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 onReadOnlyAtomFeed.getFeed
is not inherited byActivityLog.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
package annotated with @Inherited
are @Consumes
and @Produces
的唯一注释。