使用 Jersey 1.x 将 HeaderParam 反序列化为 POJO
Deserializing HeaderParam to POJO using Jersey 1.x
对我的应用程序的请求发送一个 AuthToken
header 参数。
我是否可以使用 Jersey 将其自动反序列化并解码为如下所示的 POJO?
@POST
public Response postSomething(@HeaderParam("AuthToken") AuthToken token) {
log.info("User " + token.getUser() + " posted something");
}
注意:我目前正在使用 Jersey 1.x。
泽西岛有几种可能的解决方案 1.x:
正在创建构造函数
在 AuthToken
class:
中创建接受单个 String
参数的构造函数
public class AuthToken {
public AuthToken(String content) {
// Parse the token here
}
...
}
正在创建 valueOf
或 fromString
方法
创建一个名为 valueOf
或 fromString
的静态方法,它接受单个 String
参数:
public class AuthToken {
public static AuthToken valueOf(String content) {
// Parse the token here and return an AuthToken instance
}
...
}
有关更多详细信息,请查看 Jersey @HeaderParam
文档 1.x:
Binds the value(s) of a HTTP header to a resource method parameter, resource class field, or resource class bean property. A default value can be specified using the DefaultValue
annotation. The type T
of the annotated parameter, field or property must either:
- 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)
)
- Be
List<T>
, Set<T>
or SortedSet<T>
, where T
satisfies 2 or 3 above. The resulting collection is read-only.
If the type is not one of those listed in 4 above then the first value (lexically) of the header is used.
[...]
对我的应用程序的请求发送一个 AuthToken
header 参数。
我是否可以使用 Jersey 将其自动反序列化并解码为如下所示的 POJO?
@POST
public Response postSomething(@HeaderParam("AuthToken") AuthToken token) {
log.info("User " + token.getUser() + " posted something");
}
注意:我目前正在使用 Jersey 1.x。
泽西岛有几种可能的解决方案 1.x:
正在创建构造函数
在 AuthToken
class:
String
参数的构造函数
public class AuthToken {
public AuthToken(String content) {
// Parse the token here
}
...
}
正在创建 valueOf
或 fromString
方法
创建一个名为 valueOf
或 fromString
的静态方法,它接受单个 String
参数:
public class AuthToken {
public static AuthToken valueOf(String content) {
// Parse the token here and return an AuthToken instance
}
...
}
有关更多详细信息,请查看 Jersey @HeaderParam
文档 1.x:
Binds the value(s) of a HTTP header to a resource method parameter, resource class field, or resource class bean property. A default value can be specified using the
DefaultValue
annotation. The typeT
of the annotated parameter, field or property must either:
- Be a primitive type
- Have a constructor that accepts a single
String
argument- Have a static method named
valueOf
orfromString
that accepts a singleString
argument (see, for example,Integer.valueOf(String)
)- Be
List<T>
,Set<T>
orSortedSet<T>
, whereT
satisfies 2 or 3 above. The resulting collection is read-only.If the type is not one of those listed in 4 above then the first value (lexically) of the header is used.
[...]