基本上,我希望能够在JSON中发布对象并使用Java打印此对象的详细信息。
为了我想要(我必须)使用SPRING-BOOT和Camel
这是代表我的对象的类:
public class Response {
private long id;
private String content;
public Response(){
}
public Response(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content){
this.content = content;
}
}
然后我有一个休息控制器:
@RestController
public class BasicController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
//Handle a get request
@RequestMapping("/test")
public Response getResponse(@RequestParam(value="name", defaultValue="World") String name) {
System.out.println("Handle by spring");
return new Response(counter.incrementAndGet(),
String.format(template, name));
}
//Handle a post request
@RequestMapping(value = "/post", method = RequestMethod.POST)
public ResponseEntity<Response> update(@RequestBody Response rep) {
if (rep != null) {
rep.setContent("HANDLE BY SPRING");
}
return new ResponseEntity<Response>(rep, HttpStatus.OK);
}
}
使用此代码,我能够处理发布请求和打印细节,但我必须使用Camel。所以我尝试了以下内容:
1)我添加了一个bean conf
@SpringBootApplication
public class App
{
private static final String CAMEL_URL_MAPPING = "/camel/*";
private static final String CAMEL_SERVLET_NAME = "CamelServlet";
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
public ServletRegistrationBean servletRegistrationBean() {
ServletRegistrationBean registration = new ServletRegistrationBean(new CamelHttpTransportServlet(), CAMEL_URL_MAPPING);
registration.setName(CAMEL_SERVLET_NAME);
return registration;
}
}
2)然后我创建了2条路线。
<?xml version="1.0" encoding="UTF-8"?>
<routes xmlns="http://camel.apache.org/schema/spring">
<!-- <route id="test"> -->
<!-- <from uri="timer://trigger"/> -->
<!-- <to uri="log:out"/> -->
<!-- </route> -->
<route id="test2">
<from uri="servlet:///test"/>
<to uri="log:Handle by camel"/>
</route>
</routes>
有了这个,我就可以向骆驼提出请求了。但我不知道如何建立Spring和骆驼之间的联系。有没有办法用我的弹簧控制器处理请求然后调用骆驼路线?在同一个网址上..
答案 0 :(得分:4)
您可以使用自动装配的producerTemplate
来调用Camel路由。如果添加依赖项,将创建它:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot</artifactId>
<version>${camel.version}</version> <!-- use the same version as your Camel core version -->
</dependency>
有关详情,请参阅Camel文档here。
在你的情况下你会打电话给:
producerTemplate.sendBody...