如何在Apache骆驼休息区发出过帐请求

时间:2018-10-29 17:35:56

标签: spring rest spring-boot spring-camel

我是带弹簧靴的新apache rest dsl,做了以下更改

主类

package com.javaoutofbounds.pojo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"com.ccs.batchfile"})
public class BatchFileApplication {

    public static void main(String[] args) {
        SpringApplication.run(BatchFileApplication.class, args);
    }
}

服务等级

package com.ccs.batchfile.service;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.springframework.stereotype.Component;

@Component
public class BatchFileService extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        restConfiguration().component("servlet").bindingMode(RestBindingMode.json);
        rest("/batchFile").consumes("application/json").produces("application/json").get("/routeStart").to("direct:startRoute");
    }

}

路线类别

package com.ccs.batchfile.routes;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ccs.batchfile.processor.StartRouteProcessor;

@Component
public class StartRoute  extends RouteBuilder{

     @Autowired
     private StartRouteProcessor startRouteProcessor;

    @Override
    public void configure() throws Exception {
        from("direct:startRoute").log("Inside StartRoute")
        .process(startRouteProcessor);
    }

}

处理器类

package com.ccs.batchfile.processor;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.springframework.stereotype.Component;

@Component("startRouteProcessor")
public class StartRouteProcessor implements Processor{

    public void process(Exchange exchange) throws Exception {
        String message = exchange.getIn().getBody(String.class);
        System.out.println(message);
    }

}

当我在邮递员中进行以下发布请求时,我无法控制StartRouteProcessor

http://localhost:8080/batchFile/routeStart/

我已经使用下面的测试负载来检查是否有效。

{
 "title" : "test title",
 "singer" : "some singer"
}

当我发布以上请求时,我收到404错误。请对此提供帮助

1 个答案:

答案 0 :(得分:0)

我尝试了您的示例,您需要添加两个更改。

在“主”类中,“组件扫描”注解是正确的,但是您必须添加名称为“ CamelServlet”的“ ServletRegistrationBean”:

package org.funcode.app.main;

import org.apache.camel.component.servlet.CamelHttpTransportServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication(scanBasePackages = {"org.funcode.app"})
public class BatchFileApplication {

    private static final String CAMEL_URL_MAPPING = "/api/*";
    private static final String CAMEL_SERVLET_NAME = "CamelServlet";

    public static void main(String[] args) {
        SpringApplication.run(BatchFileApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean registration =
                new ServletRegistrationBean(new CamelHttpTransportServlet(), CAMEL_URL_MAPPING);
        registration.setName(CAMEL_SERVLET_NAME);
        return registration;
    }
}

如果您想在日志上查看在请求中发布的内容,则需要将请求的方法更改为“发布”:

package org.funcode.app.main;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.springframework.stereotype.Component;

@Component
public class BatchFileService extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        restConfiguration().component("servlet").bindingMode(RestBindingMode.json);

        rest("/batchFile")
            .consumes("application/json")
            .produces("application/json")
            .post("/routeStart")
            .to("direct:startRoute");
    }
}

希望对您有帮助。