作者:没有结果的爱请你收好 | 来源:互联网 | 2023-02-04 15:57
1> 小智..:
我希望现在回答还为时不晚.无论如何我在我的项目中遇到了类似的问题,客户端使用参数执行HTTP GET,id
JPA查询使用该参数,结果最终被编组回HTTP客户端.我在Spring应用程序中运行camel.
我终于想出了如何以一种相当干净的方式实现它.
这是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
类,否则camel无法将{"id":X}转换为地图
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 toMap(String map) throws IOException {
return mapper.readValue(map, mapType);
}
}
请记住将其添加到上下文中.在Spring中是这样的:
参考文献:
http://camel.apache.org/jpa.html
http://camel.apache.org/message-endpoint.html#MessageEndpoint-DynamicTo
http://camel.apache.org/type-converter.html#TypeConverter-Addtypeconverterclassesatruntime