将 JAX-RS @PathParam 映射到带注释的 POJO 构造函数
Map JAX-RS @PathParam to POJO Constructor With Annotations
我想创建一个端点,它有一个 PathParam
自动调用要注入的对象的构造函数,它有一个字符串参数的构造函数。用代码拼写:
这是资源
@GET
@Path("/{apiVersion}" + "/item")
public Response version(@PathParam("apiVersion") APIVersion apiVersion) {
return Response.ok().build();
}
我希望在调用 APIVersion 构造函数时自动使用该字符串。在APIVersion
class
public APIVersion(String apiVersion) {
this.versionString = apiVersion;
}
是否可以只访问注释?我无法访问 ResourceConfig
.
是的,这是可能的,除了 @PathParam
之外没有任何注释,因此您提供的示例应该按原样工作。参见 https://jersey.github.io/documentation/latest/jaxrs-resources.html#d0e2271(强调我的):
In general the Java type of the method parameter may:
Be a primitive type;
Have a constructor that accepts a single String argument;
Have a static method named valueOf or fromString that accepts a single
String argument (see, for example, Integer.valueOf(String) and
java.util.UUID.fromString(String));
Have a registered implementation of
javax.ws.rs.ext.ParamConverterProvider JAX-RS extension SPI that
returns a javax.ws.rs.ext.ParamConverter instance capable of a "from
string" conversion for the type. or
Be List, Set or SortedSet, where T satisfies 2 or 3 above.
The resulting collection is read-only.
我想创建一个端点,它有一个 PathParam
自动调用要注入的对象的构造函数,它有一个字符串参数的构造函数。用代码拼写:
这是资源
@GET
@Path("/{apiVersion}" + "/item")
public Response version(@PathParam("apiVersion") APIVersion apiVersion) {
return Response.ok().build();
}
我希望在调用 APIVersion 构造函数时自动使用该字符串。在APIVersion
class
public APIVersion(String apiVersion) {
this.versionString = apiVersion;
}
是否可以只访问注释?我无法访问 ResourceConfig
.
是的,这是可能的,除了 @PathParam
之外没有任何注释,因此您提供的示例应该按原样工作。参见 https://jersey.github.io/documentation/latest/jaxrs-resources.html#d0e2271(强调我的):
In general the Java type of the method parameter may:
Be a primitive type;
Have a constructor that accepts a single String argument;
Have a static method named valueOf or fromString that accepts a single String argument (see, for example, Integer.valueOf(String) and java.util.UUID.fromString(String));
Have a registered implementation of javax.ws.rs.ext.ParamConverterProvider JAX-RS extension SPI that returns a javax.ws.rs.ext.ParamConverter instance capable of a "from string" conversion for the type. or
Be List, Set or SortedSet, where T satisfies 2 or 3 above. The resulting collection is read-only.