1

I have an interface which I am using as camel proxy Using Camel Proxy. My route is as below:

from("direct:services")
        .onException(Exception.class)
        .process(new Processor() {
            @Override
            public void process(Exchange arg0) throws Exception {
                Exception e = arg0.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
                    LOGGER.error(e, e.getMessage());
            }
        }).handled(true).end()
        .setHeader(Exchange.HTTP_URI, simple("${body.url}"))
        .setHeader(Exchange.HTTP_METHOD,simple("${body.httpMethod}"))
        .setHeader(Exchange.CONTENT_TYPE, simple("${body.contentType}"))
        .setBody(simple("${body.requestBody}"))
        .choice()
        .when(simple("${body.proxyRequired} == 'true'"))
        .inOut("{{http.urlProxy}}")
        .otherwise()
        .inOut("{{http.url}}")
        .end()
        .setBody(bean(appUtil, "extractResponse"));

The interface is :

public interface ApiServices {

    ServiceProviderResponse performOperation(ServiceProviderRequest request);

}

And the ServiceProviderResponse and ServiceProviderRequest classes :

public class ServiceProviderRequest {

    private String url;
    private String requestBody;
    private String httpMethod;
    private String contentType;
    private boolean proxyRequired;
    private Map<String, String> headers;

//getters and setters

}

public class ServiceProviderResponse {

    private int statusCode;
    private String body;
    private String errorMessage;
    private String contentType;
    private String url;

//getters and setters
}

This is how this whole thing is working :

@Produce(uri = "direct:services")
ApiServices appServices;

ServiceProviderResponse response =  appServices.performOperation(request);

Earlier my interface's method was : ServiceProviderResponse performOperation(String url); and it worked fine(ofcourse my route was different earlier). So is it something that we cannot use custom object types like this? The exception that I get :

Caused by: org.apache.camel.InvalidPayloadException: No body available of type: com.common.util.ServiceProviderResponse but has value: BeanInvocation public abstract com.common.util.ServiceProviderResponse com.common.util.ApiServices.performOperation(com.common.util.ServiceProviderRequest) with [com.common.util.ServiceProviderRequest@4ce55b]] of type: org.apache.camel.component.bean.BeanInvocation on: Message: BeanInvocation public abstract com.common.util.ServiceProviderResponse com.common.util.ApiServices.performOperation(com.common.util.ServiceProviderRequest) with [com.common.util.ServiceProviderRequest@4ce55b]]. Caused by: No type converter available to convert from type: org.apache.camel.component.bean.BeanInvocation to the required type: com.common.util.ServiceProviderResponse with value BeanInvocation public abstract com.common.util.ServiceProviderResponse com.common.util.ApiServices.performOperation(com.common.util.ServiceProviderRequest) with [com.common.util.ServiceProviderRequest@4ce55b]]. Exchange[Message: BeanInvocation public abstract com.common.util.ServiceProviderResponse com.common.util.ApiServices.performOperation(com.common.util.ServiceProviderRequest) with [com.common.util.ServiceProviderRequest@4ce55b]]]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: org.apache.camel.component.bean.BeanInvocation to the required type: com.common.util.ServiceProviderResponse with value BeanInvocation public abstract com.common.util.ServiceProviderResponse com.common.util.ApiServices.performOperation(com.common.util.ServiceProviderRequest) with [com.common.util.ServiceProviderRequest@4ce55b]]]
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:102)
    at org.apache.camel.component.bean.AbstractCamelInvocationHandler.getBody(AbstractCamelInvocationHandler.java:58)
    at org.apache.camel.component.bean.AbstractCamelInvocationHandler.afterInvoke(AbstractCamelInvocationHandler.java:148)
    at org.apache.camel.component.bean.AbstractCamelInvocationHandler$1.call(AbstractCamelInvocationHandler.java:85)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at org.apache.camel.component.bean.AbstractCamelInvocationHandler.invokeWithbody(AbstractCamelInvocationHandler.java:101)
    at org.apache.camel.component.bean.CamelInvocationHandler.invoke(CamelInvocationHandler.java:44)
    ... 43 more
Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: org.apache.camel.component.bean.BeanInvocation to the required type: com.common.util.ServiceProviderResponse with value BeanInvocation public abstract com.common.util.ServiceProviderResponse com.common.util.ApiServices.performOperation(com.common.util.ServiceProviderRequest) with [com.common.util.ServiceProviderRequest@4ce55b]]
    at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:147)
    at org.apache.camel.impl.MessageSupport.getMandatoryBody(MessageSupport.java:100)
    ... 50 more

Exception says something about TypeConverters, do I need to implement a type converter also now ? If yes how to do it? I have already gone through Camel docs for type converter but I dont understand from it much. Also I guess most of you have understood what I want to achieve, so if you guys have any different methods to accomplish this or some suggestions, please post it.

4

1 回答 1

1

您将消息正文设置为 bean(appUtil, "extractResponse") 这基本上意味着:

'将消息体设置为 bean(args) 方法返回的对象"

如果 appUtil 是一个 bean,extractResponse 是一个返回 ServiceProviderResponse 实例的方法,那么而不是:

 .setBody(bean(appUtil, "extractResponse"));

尝试:

//call to bean() as a last thing in your route def
//...
.bean(appUtil, "extractResponse"); // without the wrapping setBody call.

extractResponse 应声明为:

ServiceProviderResponse extractResponse(WhateverBodyTypeYouHadOnRoute body) {
    //return ...
}
于 2012-12-15T15:32:58.560 回答