spring 集成 jsonignore 属性

spring integration jsonignore properties

我试图忽略从 JMS 收到的一些 json 信息:

{"publishedDate":"2018","title":"How to","author":"rcade"}

我正在使用@JsonIgnoreProperties

@JsonIgnoreProperties({"title", "author", "publishedDate"})
public class Posts {
    @com.fasterxml.jackson.annotation.JsonIgnoreProperties({"publishedDate"})
    private String title;
    private String author;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
}

public class JsonToPojoTransformerBean {

    public Posts transform(@org.jetbrains.annotations.NotNull Message message) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        Posts result = mapper.readValue(message.getPayload().toString(), Posts.class);
        return result;
    }
}

然后我尝试插入它们。

 <int:transformer id="jsonToProdObjectTransformer" ref="JsonToPojoTransformerBean" input-channel="JmsInbound"
                     method="transform" output-channel="feed"/>

<int-jdbc:outbound-channel-adapter id="jdbcOutbound"
                                       channel="feed"
                                       data-source="dataSource"
                                       query="INSERT INTO posts(title, author)
                                       values(:payload[title], :payload[author])"/>

但一如既往地出现错误:

Invalid property 'payload[title]' of bean class [org.springframework.messaging.support.GenericMessage]: Illegal attempt to get property 'payload' threw exception; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'payload[title]' of bean class [org.springframework.messaging.support.GenericMessage]: Property referenced in indexed property path 'payload[title]' is neither an array nor a List nor a Set nor a Map; returned value was [com.example.Posts@76038c44]

这是我第一次尝试这个,为什么它不能作为 payload[title] 使用?我需要如何为 query 提供这些值?我是否正确地忽略了一些 json 有效负载?

完全不清楚 @JsonIgnoreProperties 主题与错误 SpEL 表达式异常的关系,但我留给您决定。

所以,到目前为止我们得到的是:

  1. Spring 表达式部分中的集成将 Message 作为求值上下文根对象处理。 Message 的合同如下:

    public interface Message<T> {
    
     /**
      * Return the message payload.
      */
     T getPayload();
    
     /**
      * Return message headers for the message (never {@code null} but may be empty).
      */
     MessageHeaders getHeaders();
    
    }
    

因此我们可以在表达式中做 headerspayload 作为对根对象的那些 getter 的引用。

  1. 你的负载是 Posts 对象。这实际上不是一个列表或数组,甚至不是要在其上执行 [](索引)运算符的映射。

  2. 要访问那些 titleauthor,您只需遵循 SpEL 中的 getter 规则。因此,具有 属性 名称的相同纯点运算符:values(:payload.title, :payload.author).

如果这不起作用,您需要考虑将 ExpressionEvaluatingSqlParameterSourceFactory 注入到 <int-jdbc:outbound-channel-adapter> 中,并使用目标表达式的参数名称别名。

在示例项目中查看一些想法:https://github.com/spring-projects/spring-integration-samples/tree/master/basic/jdbc

文档也给出了一些解释:https://docs.spring.io/spring-integration/docs/current/reference/html/jdbc.html#jdbc-outbound-channel-adapter