RESTful web service with apache cxf - web-services

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...

Related

SOAP AXIS2 - Webservice on WebSphere 8.5

I am trying to create a Webservice implementation deployed on a WebSphere 8.5 application server.
I already created the skeleton and all the stubs but I don't know how to package my application.
I noticed that using axis2-wsdl2code-maven-plugin a services.xml file is created and this should replace the old WSDD file from axis1. However I don't know where should I place the services.xml and how to set web.xml (if it is needed).
Everything should be packaged in a simple war file.
I cannot find any simple documentation for this.
UPDATE:
I was able to deploy my application but I cannot reach neither the service nor its wsdl (the WSDL is not present inside the archive).
When I'm trying to reach my webservice i get:
org.apache.axis2.AxisFault: The service cannot be found for the
endpoint reference
I have the following services.xml file, located under WEB-INF/services fodler of my WAR archive.
<?xml version="1.0" encoding="UTF-8"?><!-- This file was auto-generated from WSDL --><!-- by the Apache Axis2 version: 1.6.2 Built on : Apr 17, 2012 (05:33:49 IST) --><serviceGroup>
<service name="PagamentoBollettinoPostaleInf">
<messageReceivers>
<messageReceiver mep="http://www.w3.org/ns/wsdl/in-out" class="myservice.PagamentoBollettinoPostaleInfMessageReceiverInOut"/>
</messageReceivers>
<parameter name="ServiceClass">myservice.PagamentoBollettinoPostaleInfSkeleton</parameter>
<parameter name="useOriginalwsdl">true</parameter>
<parameter name="modifyUserWSDLPortAddress">true</parameter>
<operation name="getPagamentoBollettinoPostaleInf" mep="http://www.w3.org/ns/wsdl/in-out" namespace="******">
<actionMapping>urn:getPagamentoBollettinoPostaleInf</actionMapping>
<outputActionMapping>urn:getPagamentoBollettinoPostaleInfResponse</outputActionMapping>
</operation>
</service>
</serviceGroup>
This is my WEB.XML file:
<?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" 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>inviaAnomalia</display-name>
<servlet>
<display-name>Apache-Axis Servlet</display-name>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
This is the endpoint i am trying to connect to:
http://localhost:9080/war_context_root/services/PagamentoBollettinoPostaleInf
This happens when I try to get the WSDL file of my webservice by connecting to: http://localhost:9080/war_context_root/services/PagamentoBollettinoPostaleInf?wsdl
SOLVED
The solution was to follow the steps shown here: http://maksim.sorokin.dk/it/2011/01/13/axis2-maven-servlets-tomcat/
Then, in order to deploy correctly, WSDL and services.xml descriptor file must be placed inside:
WEB-INF/services/<ServiceName>/META-INF
Furthermore, disabling IBM JAX-WS Engine as suggested by Bruce T. and setting the classloader as shown in the following image solved the issue.

Deploy Web Service using Jboss 6

I create a web service using net beans 7.1 and Jboss.
i am following the steps of this tutorial:
http://campuscurico.utalca.cl/~pabrojas/?page_id=192
But when i deploy the project with glassfish i go to:
http://localhost:8080/ProjectName/webServiceName?WSDL
And it show me the wsdl.
But when i deploy the project with Jboss 6.1 Final, and i go to the url:
http://localhost:8080/ProjectName/webServiceName?WSDL
The page show me Estado HTTP 404 - /
but when i go to the:
http://localhost:8080/ProjectName/
Show me the index jsp page
What is my problem, why i can't deploy the project with jboss?
Who can help me?.
Thanks
I do not if i need configure my web.xml
I have this web.xml:
<?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">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Have a look in the generated WSDL in your JBOSS. You can find it in your JBOSS Home directory:
JBOSS6/standalone/data/wsdl...
In the WSDL the url to your wsdl is specified in the soap:address tag.
If no wsdl is stored in your jboss you have to share some more information about your web service files.

Using Jersey 2.5 on Websphere 8.X

I am using the following configurations for my Rest-WS application and it deploys/works well on WAS 7.x.
But when i deploy the same on WAS8.0, i get the following exception:
(java.lang.NoSuchMethodError:
javax/ws/rs/core/Application.getProperties()Ljava/util/Map)
I understand that WAS 8.X has a JAX-RS-1.0 runtime and since the 1.0 api interface does not have the Application::getProperties(), i get the exception above.
So how do i get my project to work on WAS 8.X. Any pointers would be much appreciated..
My current application configuration as below -
JAX-RS-2.0/JSR339
JERSEY 2.5
JACKSON 2.3
WEB.XML
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.packagename.resources.MyAppResourceConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
MyAppResourceConfig.java
public class MyAppResourceConfig extends ResourceConfig {
public MonitoringResourceConfig() {
// Registers JAX-RS Components
register(RequestContextFilter.class);
register(MyRestResource.class);
packages( "com.packagename");
}
}
MyRestResource.java contains #path definitions.
P.S - I tried setting the JVM property DisableIBMJAXRSEngine in WAS, but it doesn't help.
I'm having the same trouble with Jersey 2.6 and WAS 8.0.0.3. I tried changing the class loading policy to parent last (on both, web module and application) and putting the Jersey jars into the application. It solved that problem, but is too restricted for my context.

Spring with Apache Camel and Web services getting started

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>

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