简体   繁体   中英

Camel With Cxf and Routing

I was doing some research on Camel - CXf integration and am confused about the below scenario.

So i implemented a Rest Endpoint

@Path("/authenticate")
public interface Sample {
@GET
@Path("/handshake")
@Produces(MediaType.TEXT_PLAIN)
public Response handshake();

@POST
@Path("/login")
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Produces(MediaType.APPLICATION_JSON)
public Response login(LoginRequest request) throws JsonGenerationException, JsonMappingException, IOException;
}

And the implementation as below

public class SampleImpl implements Sample{

@Context
private HttpHeaders headers;

@Autowired
CamelContext context;

public Response handshake()
{
    System.out.println("HandShake Executed Successfully");
    return Response.status(Status.OK).entity("This is a Message after Routing").build();
}

public Response login(LoginRequest request) throws JsonGenerationException, JsonMappingException, IOException {
    System.out.println("The Rquest objecr is Received "+request);
    return Response.status(Status.OK).entity(mapper.writeValueAsString(request)).build();
}

}

The Route

<camel:from uri="cxfrs:bean:SampleRestEndPoint?bindingStyle=SimpleConsumer"></camel:from>

routes it into the implementation. But since the implementation returns a response object am confused how to build the routes around this.

  1. Once the call comes into the implementation how can I execute the other routes and sent a response back?.In this case the implementation returns a custom object.
  2. How are the other routes attached to a CXF route?.
  3. Should my CXF Implemenation always return a void type?. As i see that, to get access to Exchange object camel need the return type to be void
  4. Do I completely ignore the implementation and go with the "to" steps and modify it in exchange body for the required response?.

Any pointers will be appreciated.

Dude, take a look at this - http://bushorn.com/camel-cxf-geocoder-example/

The above example is not REST though, but usage of CXF with Camel route is same.

I will do these mandatory steps:

  1. Avoid beans/custom classes - try to use the camel framework capabilities.

  2. Use XML - Spring/Blueprint DSL

Please look at the following thread.

Apache Camel and web services

I have successfully implemented web service consumption using camel and Apache CXF. If you have doubts, I can help you out.

Thanks, Gautham

@GauthamHonnavara - that is an implementation of a JS webservice with an assosiated processor however it doesnt assosiate any direct route to the endpoint.Also my question was specific to JAX-RS where you cannot generate a service class from wsdl.

Assume this use case that u need a customer to invoke the endpoint and then go through say another 5 steps, reach out to another webservice etc and then sent a response back. The above implementation sents a response back in the webservice implementation bean itself.

So to avoid this create a simple interface with the producer consumer etc, just like in my question and then make each method void if you want to inject the Exchange( if needed. ) and use below configuration

<!-- Created the CXF End Point For the  Calls to Come IN -->
<cxf:rsServer id="RestEndPoint" address="/rest"
    serviceClass="com.poc.camel.cxf.service.incoming.xxx"
    loggingFeatureEnabled="true" loggingSizeLimit="20">
    <cxf:providers>
        <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" >
            <!-- <constructor-arg ref="customObjectMapper" type="org.codehaus.jackson.map.ObjectMapper"/> -->
        </bean>
    </cxf:providers>
</cxf:rsServer>

Trick is to use the service class tag. If the interface is provided there then it doesn't need a concrete implementation from CXF.

Hope this helps. Let me know

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM