Spring with Apache Camel and Web services getting started - web-services

I am starting to try to figure out how to work with Apache Camel within a Spring framework. The first thing that I want to try to get a grasp on would be running simple web service calls over this but I have no idea where to start.
Right now all I have is a basic HelloWorld Spring project set up and am trying to figure out what needs to be configured Apache Camel wise and where to get started on creating a simple web service.
I have taken a look at the examples on the Apache site but I was hoping maybe someone here knew of a tutorial that was a basic start to finish of something like I am trying to do.
Thanks for any tips or help that you all have!

I really found this one useful once: http://camel.apache.org/spring-ws-example.html (and complete source code in the camel distribution).
There are multiple ways you can deal with web services with Camel. Either use the spring web services as in the example I mention, or use Apache CXF.
Spring Web services are really easy to get started with, compared to CXF. Although CXF is a more complete web service stack.
There are multiple Camel and CXF examples here:
http://camel.apache.org/examples.html
If you go with CXF, you may well benefit from actually learning a few "CXF only" examples before mixing with Camel.
There are essentially two ways to do web services, contract first start with a WSDL, then auto generate classes/interfaces. The other approach is code first - where the you start with java classes and then get an auto generated WSDL.
There are some rather good articles over here: http://cxf.apache.org/resources-and-articles.html
But to answer your question, I don't know of any good step-by-step tutorial on this matter. The examples in the links are really good though.

I had the same question, and I have found this article
Step by step introduction to Web services creation using CXF and Spring:
http://www.ibm.com/developerworks/webservices/library/ws-pojo-springcxf/index.html?ca=drs-
In short, there is four steps to create a web service
1 - Create a service endpoint interface (SEI) and define a method to
be exposed as a Web service.
package demo.order;
import javax.jws.WebService;
#WebService
public interface OrderProcess {
String processOrder(Order order);
}
2 - Create the implementation class and annotate it as a Web service.
package demo.order;
import javax.jws.WebService;
#WebService(endpointInterface = "demo.order.OrderProcess")
public class OrderProcessImpl implements OrderProcess {
public String processOrder(Order order) {
return order.validate();
}
}
3 - Create beans.xml and define the service class as a Spring bean
using a JAX-WS front end.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint
id="orderProcess"
implementor="demo.order.OrderProcessImpl"
address="/OrderProcess" />
</beans>
4 - Create web.xml to integrate Spring and CXF.
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

Related

RESTful web service with apache cxf

I've already developed a SOAP web service in Java with apache cxf and now I've to develop the same services in RESTful.
I have some troubles to deploy the web service on my tomcat. In fact, I don't know how should I configure my web.xml or any other XML file.
There are many examples online but everything I've tried so far doesn't work.
To build the project, I use an Ant build tool.
Anyone knows some good tips?
Here my service class used to test tutorials
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
#Path("/book")
public class BookService {
#GET
#Path("{id}")
#Produces({"application/xml","application/json"})
public Book getBookForId(#PathParam("id") int id) {
Book book = null
book = DB.getBookForId();
if(book == null){
return Response.status(Response.Status.BAD_REQUEST).build();
}else{
return Response.ok(book).build();
}
}
}
And my web.xml looks like this most of the time :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>REST</display-name>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
Apache CXF's example and other examples are "easy" to understand but the fast is that what i've done until now won't work.
My web service is deployed on my tomcat but everytime there is error like this :
Servlet.service() for servlet [Jersey Web Application] in context with path [/WebServices] threw exception [L''exécution de la servlet a lancé une exception] with root cause
java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;
at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:669)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
I know that this kind of error are stupid btu i don't get it Zzz.
I have to use jdk 1.6.0.45 so i can't try example which use jdk 1.7 and higher version. The fact is most of example tell to use Jersey 2.18...

How to identify cxf web service end point

I am trying to creating web service using wsdl first approach and CXF. I am able to generate java file from wsdl and deploy the war file to tomcat server. However, I don't see any soapaction in the generated file. How do I identify the end point url for this web service?
thanks,
Usually in CXF you use Spring configuration to configure endpoint, as described in JAX-WS Configuration. Usually address is relative, e.g.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:endpoint id="classImpl"
implementor="org.apache.cxf.jaxws.service.Hello"
address="/helloService"/>
</beans>
Address is local to you web app context root.
Assuming that name of you web application is SomeWebApp and the server is available at localhost:8080 then web service should be published at http://localhost:8080/SomeWebApp/helloService. You can test it retrieving WSDL at: http://localhost:8080/SomeWebApp/helloService?wsdl. This URL can be used to create SOAP UI project (the tool that I really recommend for exploring and testing SOAP services).
If you don't use Spring to configure endpoint or you still can't access web service please provide more details about your configuration.

Spring web-scoped beans and axis2

I want to use spring web-scoped beans in web-service written on AXIS2 framework.
How to configure it?
Axis2 and Spring documentation disagree with each other.
Axis2 documentation says:
For the purpose of this example, we'll configure Spring via a WAR file's web.xml. Let's add a context-param and a listener:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
Spring documentation says:
When using a Servlet 2.4+ web container, with requests processed outside of Spring's DispatcherServlet (e.g. when using JSF or Struts), you need to add the following javax.servlet.ServletRequestListener to the declarations in your web application's 'web.xml' file.
<web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
When I use Axis2-recommended ContextLoaderListener with Spring web-scoped beans I got on deploy
java.lang.IllegalStateException: No thread-bound request found: Are
you referring to request attributes outside of an actual web request,
or processing a request outside of the originally receiving thread? If
you are actually operating within a web request and still receive this
message, your code is probably running outside of
DispatcherServlet/DispatcherPortlet: In this case, use
RequestContextListener or RequestContextFilter to expose the current
request.
When I use Spring-recommeded RequestContextListener I got running Web-service with fault on requests:
<faultstring>The SERVICE_OBJECT_SUPPLIER parameter is not specified.</faultstring>
In other words: how to configure AXIS2 with Spring and RequestContextListener?
No such functionality in AXIS2. See request https://issues.apache.org/jira/browse/AXIS2-5467 "Extend Spring support to accept web-scoped beans" for detail.

Spring 3 MVC + Web Services (JAX-WS)

We have a Spring 3 MVC webapplication, and we are trying to expand it with Web Services.
I have now tried with JAX-WS Web-services, annotating WebService and WebMethod on the appropriate places.
I do have a dispatcher mapped in my web.xml. This is the standard spring DispatcherServlet. And its config: dispatcher-servlet.xml is working perfectly fine for the MVC stuff.
The problem comes when I try to expose the WebServices. I do this by adding the following bean to the dispatcher-servlet.xml:
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
<property name="baseAddress" value="http://localhost:8080/service/" />
</bean>
If this bean is added. Then the WebServices works perfectly, but all the MVC stuff stops working.
So my second try was to create 2 dispatchers. One named mvc-dispatcher and one webservice-dispatcher. Each of them mapped to respectivly /mvc and /ws. And then put only the SimpleJaxWsServiceExporter in the webservice-config and only the standard MVC stuff in the other one.
But still the same problems.
I can only get the MVC to work if I disable/comment-out the web-service dispatcher.
I cant belive this is supposed to be so complicated... What am I not getting?
Any help would be greatly appriciated.
I cant find any decent tutorials doing JAX-WS and spring 3 MVC...
Thanks in advance!
I'm assuming by dispatcher you mean a spring dispatcher, I'd recommend against that. Just have the JAX-WS be a different servlet on it's own, i.e.
https://cxf.apache.org/docs/a-simple-jax-ws-service.html
Then if you need to allow Spring beans to be injected extend SpringBeanAutowiringSupport as in this example.
How to make an #WebService spring aware
Hope this helps!
You can use Apache CXF that implements the JAXWS Specification has a very good integration with Spring, actually CXF use behind the scenes Spring.
In practice you could proceed in this way:
in your web.xml you have configure the cxf servlet as below
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
....
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
Apache CXF configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath*:META-INF/cxf/cxf-extension-*.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:endpoint
id="yourService"
implementor="#yourService"
address="/yourAddres">
</jaxrs:server>
</beans>
your bean
#Service
#WebService(serviceName = "soapSvnClientService")
public class SoapSvnClientService {
#WebMethod(operationName = "service")
public void service(#WebParam String param1,
#WebParam String param2){
....
}
}
I hope that this can help you

Regarding Spring and Apache CXF Integration

Inside the applicationcontext.xml file we have like this
<bean id="vincent" class="com.bayer.vincent.service.vincent"/>
<jaxws:endpoint
id="vincentSOAP"
implementor="#vincent"
implementorClass="com.bayer.vincent.service.vincent"
address="/vincent/soap"
bindingUri="http://schemas.xmlsoap.org/wsdl/soap/" />
what does this mean by this defination ??
My question is how the vincent class is being getting called ??
CXF has provided a custom spring namespace to help easily configure a webservice endpoint here.
If the implementor starts with a #, CXF makes the assumption that the endpoint is a Spring Bean, the way it is in your case.
The endpoint will have to be a normal JAX-WS endpoint, i.e annotated with #Webservice annotation, eg:
#WebService(serviceName="MemberService", endpointInterface="org.bk.memberservice.endpoint.MemberEndpoint", targetNamespace="http://bk.org/memberservice/")
Now any call to your uri-/vincent/soap, will be redirected by the CXF front controller(which you can register in the web.xml file):
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
which maintains an internal registry of payload uri's to handlers(in this case the Spring bean) and dispatches the request appropriately.
As far I understand there created proxy class which forwards all calls to your real class.
See also Configuring an Endpoint where described all jaxws:endpoint attributes.