2017-04-05 59 views
0

我想在Camel上打一個REST端點並將該數據轉換成一個類(並且爲了簡單起見並測試將該類轉換爲JSON字符串)並將POST發送到本地服務器。我可以做到這一切,但做出最後的帖子,似乎只是掛起。流程工作在變換沒有

應用:

@SpringBootApplication 
public class App { 

    /** 
    * A main method to start this application. 
    */ 
    public static void main(String[] args) { 
     SpringApplication.run(App.class, args); 
    } 

    @Component 
    public class RestTest extends RouteBuilder { 
     @Override 
     public void configure() throws Exception { 
      restConfiguration().component("restlet").host("localhost").port(8000).bindingMode(RestBindingMode.json); 

      rest("/test").enableCORS(true) 
        .post("/post").type(User.class).to("direct:transform"); 

      from("direct:transform") 
        .transform().method("Test", "alter") 
        .to("http4:/localhost:8088/ws/v1/camel"); 
     } 
    } 

} 

豆:

@Component("Test") 
public class Test { 
    public void alter (Exchange exchange) { 
     ObjectMapper mapper = new ObjectMapper(); 

     User body = exchange.getIn().getBody(User.class); 

     try { 
      String jsonInString = mapper.writeValueAsString(body); 

      exchange.getOut().setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.POST)); 
      exchange.getOut().setHeader(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON); 
      exchange.getOut().setBody(jsonInString); 

     } catch (JsonGenerationException e) { 
      e.printStackTrace(); 
     } catch (JsonMappingException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

用戶:

public class User { 
    @JsonProperty 
    private String firstName; 

    @JsonProperty 
    private String lastName; 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 
} 

UPDATE

能得到它的WO RK與過程,而不是變換,但是當一個響應從POST發送回駱駝錯誤:

from("direct:transform") 
        .process(new Processor() { 
         public void process(Exchange exchange) throws Exception { 
          ObjectMapper mapper = new ObjectMapper(); 

          User body = exchange.getIn().getBody(User.class); 

          try { 
           String jsonInString = mapper.writeValueAsString(body); 

           exchange.getOut().setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.POST)); 
           exchange.getOut().setHeader(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON); 
           exchange.getOut().setBody(jsonInString); 

          } catch (JsonGenerationException e) { 
           e.printStackTrace(); 
          } catch (JsonMappingException e) { 
           e.printStackTrace(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
        }) 
        .to("http4://0.0.0.0:8088/ws/v1/camel"); 

錯誤

com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) 
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:284) 
    at com.fasterxml.jackson.databind.SerializerProvider.mappingException(SerializerProvider.java:1110) 
    at com.fasterxml.jackson.databind.SerializerProvider.reportMappingProblem(SerializerProvider.java:1135) 
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:69) 
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:32) 
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:292) 
    at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1429) 
    at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1158) 
    at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:988) 
    at org.apache.camel.component.jackson.JacksonDataFormat.marshal(JacksonDataFormat.java:155) 
    at org.apache.camel.processor.MarshalProcessor.process(MarshalProcessor.java:69) 
    at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109) 
    at org.apache.camel.processor.MarshalProcessor.process(MarshalProcessor.java:50) 
    at org.apache.camel.component.rest.RestConsumerBindingProcessor$RestConsumerBindingMarshalOnCompletion.onAfterRoute(RestConsumerBindingProcessor.java:363) 
    at org.apache.camel.util.UnitOfWorkHelper.afterRouteSynchronizations(UnitOfWorkHelper.java:154) 
    at org.apache.camel.impl.DefaultUnitOfWork.afterRoute(DefaultUnitOfWork.java:278) 
    at org.apache.camel.processor.CamelInternalProcessor$RouteLifecycleAdvice.after(CamelInternalProcessor.java:317) 
    at org.apache.camel.processor.CamelInternalProcessor$InternalCallback.done(CamelInternalProcessor.java:246) 
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:109) 
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) 
    at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:97) 
    at org.apache.camel.component.restlet.RestletConsumer$1.handle(RestletConsumer.java:68) 
    at org.apache.camel.component.restlet.MethodBasedRouter.handle(MethodBasedRouter.java:54) 
    at org.restlet.routing.Filter.doHandle(Filter.java:150) 
    at org.restlet.routing.Filter.handle(Filter.java:197) 
    at org.restlet.routing.Router.doHandle(Router.java:422) 
    at org.restlet.routing.Router.handle(Router.java:639) 
    at org.restlet.routing.Filter.doHandle(Filter.java:150) 
    at org.restlet.routing.Filter.handle(Filter.java:197) 
    at org.restlet.routing.Router.doHandle(Router.java:422) 
    at org.restlet.routing.Router.handle(Router.java:639) 
    at org.restlet.routing.Filter.doHandle(Filter.java:150) 
    at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:140) 
    at org.restlet.routing.Filter.handle(Filter.java:197) 
    at org.restlet.routing.Filter.doHandle(Filter.java:150) 
    at org.restlet.routing.Filter.handle(Filter.java:197) 
    at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:202) 
    at org.restlet.Component.handle(Component.java:408) 
    at org.restlet.Server.handle(Server.java:507) 
    at org.restlet.engine.connector.ServerHelper.handle(ServerHelper.java:63) 
    at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:143) 
    at org.restlet.engine.connector.HttpServerHelper$1.handle(HttpServerHelper.java:64) 
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:79) 
    at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83) 
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:82) 
    at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:675) 
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:79) 
    at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:647) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 

通往的是什麼進程,改造的根本區別的問題?

+0

你爲什麼要設置exchange.getOut配置?你可以使用exchange.getIn。 –

+0

@SoucianceEqdamRashti'getIn'使處理器運行多次而不發出發佈請求。 –

+0

不應該發生這種情況,您可以在http4端點上設置bridgeEndpoint = true,否則生產者和使用者的http頭可能會混雜在一起。它應該與exchange.getIn一起工作。你可以在這裏看到類似的東西https://github.com/SoucianceEqdamRashti/Integration/blob/master/CamelDemo/src/main/java/org/souciance/integration/http/RedirectHTTP.java –

回答

0

camel中的「Processor」是最低級別的消息處理原語。在封面下,轉換定義被執行爲org.apache.camel.processor.TransformProcessor,它本身就是一個處理器。事實上,大多數情況下,所有的東西都是處於引擎蓋下的處理器,因此嚴格地說,您可以使用純處理器完成的轉換任務:

您最後的錯誤是因爲您需要將HTTP調用的輸出解組可以使用Jackson將其整理回JSON。類似的東西:

from("direct:transform") 
    .transform().method("Test", "alter") 
    .to("http4:/localhost:8088/ws/v1/camel") 
    .unmarshal().json(JsonLibrary.Jackson, User.class); 
+0

使用轉換給我的結果是一樣的因爲使用'getIn'而不是'getOut'。它運行'alter'方法11次(是的,每次11次)只是在到達'to'時掛起,並且永遠不會解析。 '無法運行以下服務器端任務:sun.net.httpserver.ServerImpl $ Exchange' –