【问题标题】:How to make post request in apache camel rest如何在 apache camel rest 中发出 post 请求
【发布时间】:2018-11-02 23:42:39
【问题描述】:

我是新的带有 spring boot 的 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 错误。请帮忙解决这个问题

【问题讨论】:

    标签: spring rest spring-boot spring-camel


    【解决方案1】:

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

    在您的“主”类中,“组件扫描”注释是正确的,但您必须添加名称为“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");
        }
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2023-04-07
      相关资源
      最近更新 更多