英文:
Json to Spring Expression mapping exception
问题
以下是您要的翻译内容:
当我尝试发起一个包含以下请求主体的POST请求(来自Postman),我遇到了映射异常:
> 类型定义错误:[简单类型,类org.springframework.expression.spel.support.ReflectiveMethodResolver];嵌套异常为com.fasterxml.jackson.databind.exc.InvalidDefinitionException:找不到类org.springframework.expression.spel.support.ReflectiveMethodResolver的序列化器,也没有发现创建BeanSerializer的属性(为避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链:com.freddiemac.security.policy.Policy["accessPolicies"]->java.util.ArrayList[0]->com.freddiemac.security.policy.AccessPolicy["condition"]->org.springframework.expression.spel.standard.SpelExpression["evaluationContext"]->org.springframework.expression.spel.support.StandardEvaluationContext["methodResolvers"]->java.util.ArrayList[0])
POST调用 - 请求主体:
{
"accessPolicies": [{
"policyId": 1,
"resource": [
"/my-resource"
],
"action": [
"POST"
],
"condition": "#claims['userRoles'].contains('developer')",// 这在Java中被映射为Spring表达式
"effect": "Allow"
}]
}
POJO类:
private String policyId;
private List<String> resource;
private List<String> action;
private List<String> role;
private Expression condition; // 从org.springframework.expression.Expression导入;
请问您是否需要其他帮助?提前感谢您的回复。
英文:
When I try to make a POST request (from postman) which has request body as below, I am facing mapping exception :
> Type definition error: [simple type, class org.springframework.expression.spel.support.ReflectiveMethodResolver]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.expression.spel.support.ReflectiveMethodResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.freddiemac.security.policy.Policy["accessPolicies"]->java.util.ArrayList[0]->com.freddiemac.security.policy.AccessPolicy["condition"]->org.springframework.expression.spel.standard.SpelExpression["evaluationContext"]->org.springframework.expression.spel.support.StandardEvaluationContext["methodResolvers"]->java.util.ArrayList[0])
POST call - Request Body :
{
"accessPolicies": [{
"policyId": 1,
"resource": [
"/my-resource"
],
"action": [
"POST"
],
"condition": "#claims['userRoles'].contains('developer')", // this is mapped as Spring expression in java
"effect": "Allow"
}]
}
POJO class :
private String policyId;
private List<String> resource;
private List<String> action;
private List<String> role;
private Expression condition; // import from : org.springframework.expression.Expression;
Could you please help me on this. Thanks in advance.
答案1
得分: 0
Expression
接口没有序列化程序,因此接收String
,而String
具有有效的序列化程序。
然后,您可以使用SpelExpressionParser
将String
解析为有效的Expression
。
class PostRequestDto {
private String serviceId;
private String version;
private List<AccessPolicy> accessPolicies;
// 省略了为了简洁性而省略的getter和setter方法
}
class AccessPolicy {
private String policyId;
private List<String> resource;
private List<String> action;
private List<String> role;
private String condition; // 在接收 SpEL 表达式时使用 String 序列化程序
private String effect;
// 省略了为了简洁性而省略的getter和setter方法
// 将接收到的 String 解析为 SpEL 表达式
public Expression getConditionExpression() {
// 为了简洁性,省略了适当的错误处理
ExpressionParser expressionParser = new SpelExpressionParser();
return expressionParser.parseExpression( this.condition );
}
}
英文:
Expression
interface doesn't have serializer, so receive String
instead, which does have valid serializer.
Then you can use SpelExpressionParser
to parse String
into a valid Expression
.
class PostRequestDto {
private String serviceId;
private String version;
private List<AccessPolicy> accessPolicies;
// Getters and setters omitted for brevity
}
class AccessPolicy {
private String policyId;
private List<String> resource;
private List<String> action;
private List<String> role;
private String condition; // utilize String serializer when accepting SpEL Expression
private String effect;
// Getters and setters omitted for brevity
// parse received String to derive SpEL Expression
public Expression getConditionExpression() {
// proper error handling omitted for brevity
ExpressionParser expressionParser = new SpelExpressionParser();
return expressionParser.parseExpression( this.condition );
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论