JAX-RS 和@Produces 匹配正则表达式的能力

JAX-RS and @Produces ability to match on regex

我正在编写一个需要版本控制的 Restful Web 服务。我想要解决这个问题的方法是使用 header 中的媒体类型来执行此操作。

请求示例:

Accept: application/vnd.test.books.v1+xml

我的终点是

@GET
@Path("/test")
@Produces("application/vnd.test.books.v1+xml")
public Response getA(){
...
}

我还希望正则表达式匹配所有版本。所以我想用 Accept header of application/vnd.test.books.*+xml 来匹配任何东西。所以在我的情况下任何版本。我尝试了类似以下的方法,但没有成功。

@GET
@Path("/test")
@Produces("application/vnd.test.books*+xml")
public Response getB(){
...
}

我想要的是下线的能力,特别是 v1 的端点,然后是 v2 以上的任何端点,我想要一个不同的端点。

您必须明确列出所有 MIME 类型的方法 @Produces:

@Produces(value = {"application/vnd.test.books.v2+xml",
                   "application/vnd.test.books.v3+xml",
                   "application/vnd.test.books.v4+xml"})