Json to Spring Expression映射异常

huangapple go评论89阅读模式
英文:

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 :

{
&quot;accessPolicies&quot;: [{
	&quot;policyId&quot;: 1,
	&quot;resource&quot;: [
		&quot;/my-resource&quot;
	],
	&quot;action&quot;: [
		&quot;POST&quot;
	],
	&quot;condition&quot;: &quot;#claims[&#39;userRoles&#39;].contains(&#39;developer&#39;)&quot;, // this is mapped as Spring expression in java
	&quot;effect&quot;: &quot;Allow&quot;
}]

}

POJO class :

private String policyId;
private List&lt;String&gt; resource;
private List&lt;String&gt; action; 
private List&lt;String&gt; 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具有有效的序列化程序。

然后,您可以使用SpelExpressionParserString解析为有效的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&lt;AccessPolicy&gt; accessPolicies;
    // Getters and setters omitted for brevity
}

class AccessPolicy {
    private String policyId;
    private List&lt;String&gt; resource;
    private List&lt;String&gt; action;
    private List&lt;String&gt; 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 );
    }
}

huangapple
  • 本文由 发表于 2020年7月24日 03:21:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63061685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定