如何通过标头在Apache Camel JPA中传递namedQuery参数? [英] how to pass namedQuery parameters in Apache Camel JPA by header?
问题描述
我有这条骆驼路线:
from("direct:getUser")
.pollEnrich("jpa://User?namedQuery=User.findById&consumeDelete=false");
这是我的用户实体:
@Entity
@NamedQueries({
@NamedQuery(name="User.findAll", query="SELECT u FROM User u"),
@NamedQuery(name="User.findById", query="SELECT u FROM User u WHERE u.id = :id")
})
public class User{
@Id
private String id;
}
我通过设置标头尝试了此路线:
I have tried this route by setting the header:
from("direct:getUser")
.setHeader("id", simple("myid"))
.pollEnrich("jpa://User?namedQuery=User.findById&consumeDelete=false");
但是它不起作用
是否有任何方法可以通过标题设置jpa属性?骆驼文档在parameters
选项中引用了此内容,但我没有找到示例
Is there any method to set jpa properties by the headers? The camel documentation quote this in parameters
option but i don't found the examples
选项:
parameters
此选项基于注册表,需要使用#表示法.这 键/值映射用于构建查询参数.它是 预期为通用类型java.util.Map,其中 键是给定JPA查询的命名参数及其值 是您要选择的相应有效值.骆驼 2.19:它也可以用于生产者.当用于生产者时,简单表达式可以用作参数值.它 允许您从邮件正文标头中检索参数值 等
This option is Registry based which requires the # notation. This key/value mapping is used for building the query parameters. It is expected to be of the generic type java.util.Map where the keys are the named parameters of a given JPA query and the values are their corresponding effective values you want to select for. Camel 2.19: it can be used for producer as well. When it's used for producer, Simple expression can be used as a parameter value. It allows you to retrieve parameter values from the message body header and etc.
推荐答案
我希望回答还不晚.无论如何,我的项目中都有类似的问题,客户端使用参数id
进行HTTP GET,JPA查询使用该参数,最终将结果编组回HTTP客户端.我在Spring应用程序中运行骆驼.
I hope it's not too late to answer. In any case I had a similar issue in my project, the client does a HTTP GET with a parameter id
, which is used by the JPA query and the result is finally marshalled back to the HTTP client. I'm running camel in a Spring application.
我终于想出了如何以一种合理的干净的方式来实现它.
I finally figured out how to achieve it in a reasonably clean way.
这是定义路径的RouteBuilder
:
@Override
public void configure() throws Exception {
Class dataClass = SomeClass.class;
JacksonDataFormat format = new JacksonDataFormat();
format.setUnmarshalType(dataClass);
String jpaString = String
.format("jpa://%1$s?resultClass=%1$s&namedQuery=q1" +
"¶meters={\"id\":${headers.id}}", dataClass.getName());
from("jetty://http://localhost:8080/test").toD(jpaString) // note the .toD
.marshal(format)
}
这是StringToMapTypeConverter
类,否则骆驼无法将{"id":X}转换为地图
And this is the StringToMapTypeConverter
class, otherwise camel cannot convert {"id": X} to a map
public class StringToMapTypeConverter implements TypeConverters {
private static final ObjectMapper mapper = new ObjectMapper();
private static JavaType mapType;
static {
mapType = mapper.getTypeFactory().constructMapType(Map.class,
String.class, Object.class);
}
@Converter
public Map<String, Object> toMap(String map) throws IOException {
return mapper.readValue(map, mapType);
}
}
请记住将其添加到上下文中.在春季,就像这样:
Remember to add it to the context. In Spring is something like:
<bean id="myStringToMapTypeConverter" class="....StringToMapTypeConverter" />
参考:
- http://camel.apache.org/jpa.html
- http://camel.apache.org/message-endpoint.html# MessageEndpoint-DynamicTo
- http://camel.apache.org/type-converter.html# TypeConverter-Addtypeconverterclassesatruntime
- http://camel.apache.org/jpa.html
- http://camel.apache.org/message-endpoint.html#MessageEndpoint-DynamicTo
- http://camel.apache.org/type-converter.html#TypeConverter-Addtypeconverterclassesatruntime
这篇关于如何通过标头在Apache Camel JPA中传递namedQuery参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!