我试图忽略从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有效负载而正确地这样做了?
发布于 2020-12-10 15:13:58
我们完全不清楚@JsonIgnoreProperties主题与错误的SpEL表达式的异常有什么关系,但我让您来决定。
因此,到目前为止,我们得到的是:
表达式部分中的
Message作为计算上下文根对象处理。Message的合同如下:公共接口消息{ /** *返回消息有效负载。*/ T getPayload();/** *返回消息的消息头(从不{@代码null},但可能为空)。*/ MessageHeaders getHeaders();}
因此,我们可以在表达式中执行headers和payload,作为对根对象的那些getter的引用。
Posts对象。它实际上不是一个列表或数组,甚至不是要在其上执行[] (索引)运算符的映射。title和author,只需遵循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。
同时,docs也给出了一些解释:https://docs.spring.io/spring-integration/docs/current/reference/html/jdbc.html#jdbc-outbound-channel-adapter
https://stackoverflow.com/questions/65235642
复制相似问题