2.3.2. 自定义 RESTEasy Annotations
由于字节码中添加了参数名称,您不再需要在以下注释中指定参数名称:@ PathParam、@
、@QueryParam、@Form
Param
、@CookieParam
、@HeaderParamHeaderParam
和 @MatrixParam
。为此,您必须在具有可选 value 参数的不同软件包中切换到名称相同的新注解。您可以按照以下步骤实现:
-
导入
org.jboss.resteasy.annotations.jaxrs
软件包,以替换来自 JAX-RS 规范的注释。 配置构建系统,以在字节码中记录方法参数名称。
Maven 用户可以通过将
maven.compiler.parameters
设置为true
来在字节码中启用记录方法参数名称:<properties> <maven.compiler.parameters>true</maven.compiler.parameters> </properties>
如果名称与注释的变量的名称匹配,则删除注解值。
注意您可以省略注释的方法参数的注解名称,以及注释的字段或 JavaBean 属性。
例如,请考虑以下用法:
import org.jboss.resteasy.annotations.jaxrs.*; @Path("/library") public class Library { @GET @Path("/book/{isbn}") public String getBook(@PathParam String isbn) { // search my database and get a string representation and return it } }
如果您标注的变量的名称与 path 参数不同,您可以指定名称,如下所示:
import org.jboss.resteasy.annotations.jaxrs.*; @Path("/library") public class Library { @GET @Path("/book/{isbn}") public String getBook(@PathParam("isbn") String id) { // search my database and get a string representation and return it } }