问题描述
我正在尝试使用 Java 将有效负载传递给 Play 框架中的 validate(ValidationPayload).我无法访问存储在返回 TypedMap 的 payload.getAttrs() 中的值.
I'm trying to pass a payload to a validate(ValidationPayload) in the Play Framework using Java. I can not access the values stored in payload.getAttrs() which returns a TypedMap.
我试图通过调用验证方法 payload.getAttrs().getOptional(TypedKey.create("Cookies")) 来访问 Cookie,但我总是得到一个空值.
I tried to access the Cookies by calling in the validate method payload.getAttrs().getOptional(TypedKey.create("Cookies")) but I always get a null back.
当我使用 IntelliJ 评估表达式时,我看到属性包含 Cookie、Flash 等.但我无法访问这些值.我可以在 Expression Evaluator 截图中看到值
When I evaluate expression using IntelliJ I see the attrs contain Cookies, Flash and more. But I can not access theses values. I can see the values in the Expression Evaluator screenshot
public String validate(Constraints.ValidationPayload payload) {
TypedMap attrs = payload.getAttrs();
Optional<Object> baseDomain = payload.getAttrs().getOptional(TypedKey.create("baseDomain"));
Locale value = payload.getAttrs().get(TypedKey.create("selectedLang"));
return "String";
}
如何访问存储在 TypedMap 中的这些对象?
How can I access these objects stored in a TypedMap?
推荐答案
我发现 TypedMap 映射使用 TypedKeys.键入的键对于键的每个实例都是唯一的.这意味着您需要使用与用于放入地图的键相同的实例从 typedMap 中获取.创建新密钥将导致空响应或空响应.
I figured this out a TypedMap map uses TypedKeys. A typed key is unique to each INSTANCE of the key.That means you need to fetch from the typedMap with the same instance of the key that was used to put in the map. Creating a new key will cause an empty or null response.
这应该有效:
TypedKey<String> baseDomainKey = TypedKey.create("baseDomain")
payload.getAttrs().addAttrs(baseDomainKey, "domain")
String domain = payload.getAttrs().get(baseDomainKey)
这将不起作用但是:
TypedKey<String> baseDomainKey = TypedKey.create("baseDomain")
payload.getAttrs().addAttrs(baseDomainKey, "domain")
String domain = payload.getAttrs().get(TypedKey.create("baseDomain"))
这篇关于播放框架@ValidateWithPayload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!