Java: un semplice esempio di webservice utilizzando il framework Apache CXF

Apache CXF - esempio di webservice utilizzando Apache CXF

Piccolo progetto di esempio di un webservice realizzatto in java, utilizzando il framework Apache CXF. Il progetto è realizzato con Eclipse 4.2 e gestito da Maven.

1) Prima di tutto le dipendenze necessarie:

<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-frontend-jaxws</artifactId>
	<version>2.7.3</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http</artifactId>
	<version>2.7.3</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
	<version>3.1.1.RELEASE</version>
</dependency>

2) Integrare CXF nel progetto WEB

Per integrare il framework CXF è sufficiente aggiungere questa servlet al WEB.xml:

<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>/services/*</url-pattern>
</servlet-mapping>

3) Definire il Webservice

Nel file cxf-servlet.xml cominciamo a definire un pò di informazioni sul webservice, tanto per cominciare:

  • L’interfaccia che definira’ realmente quali sono i metodi esposti dal webservice
  • La classe che implementa l’interfaccia e aggiunge la logica di business

Questo è la definizione:

<jaxws:server id="jaxwsService"
	serviceClass="it.nicola.test.PingPongService" address="/pingpongservice">
	<jaxws:serviceBean>
		<bean class="it.nicola.test.PingPongServiceImpl" />
	</jaxws:serviceBean>
</jaxws:server>

4) Implementare il webservice

In questo piccolo esempio definiamo un webservice che espone un solo metodo, chiamato “ping” che restituisce la stringa “pong”. Per rendere le cose più emozionanti diamo in ingresso al metodo ping l’oggetto Mittente, una istanza della classe Personaggio.
Questo è l’interfaccia che definisce il webservice:

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
 
@WebService
public interface PingPongService {
 
	@WebMethod(operationName="ping")
    String ping (@WebParam(name = "Mittente" ) Personaggio mittente);
 
}

Questa la classe che implementa l’interfaccia:

import javax.jws.WebService;
 
@WebService(endpointInterface = "it.nicola.test.PingPongService", serviceName = "PingPongService")
public class PingPongServiceImpl implements PingPongService {
 
	public String ping(Personaggio mittente) {
		return "pong";
	}
}

5) Dare una occhiata al WSDL per vedere l’effetto che fà…

Il WSDL generato da CXF, una volta fatto il deploy di questo fantastico webservice è questo:

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://test.nicola.it/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="PingPongServiceService" targetNamespace="http://test.nicola.it/">
	<wsdl:types>
		<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://test.nicola.it/" elementFormDefault="unqualified" targetNamespace="http://test.nicola.it/" version="1.0">
			<xs:element name="ping" type="tns:ping"/>
			<xs:element name="pingResponse" type="tns:pingResponse"/>
			<xs:complexType name="ping">
				<xs:sequence>
					<xs:element minOccurs="0" name="Mittente" type="tns:personaggio"/>
				</xs:sequence>
			</xs:complexType>
			<xs:complexType name="personaggio">
			<xs:sequence>
				<xs:element minOccurs="0" name="cognome" type="xs:string"/>
				<xs:element minOccurs="0" name="nickname" type="xs:string"/>
				<xs:element minOccurs="0" name="nome" type="xs:string"/>
			</xs:sequence>
			</xs:complexType>
			<xs:complexType name="pingResponse">
				<xs:sequence>
					<xs:element minOccurs="0" name="return" type="xs:string"/>
				</xs:sequence>
			</xs:complexType>
		</xs:schema>
	</wsdl:types>
	<wsdl:message name="ping">
		<wsdl:part element="tns:ping" name="parameters"></wsdl:part>
	</wsdl:message>
	<wsdl:message name="pingResponse">
		<wsdl:part element="tns:pingResponse" name="parameters"></wsdl:part>
	</wsdl:message>
	<wsdl:portType name="PingPongService">
		<wsdl:operation name="ping">
			<wsdl:input message="tns:ping" name="ping"></wsdl:input>
			<wsdl:output message="tns:pingResponse" name="pingResponse"></wsdl:output>
		</wsdl:operation>
	</wsdl:portType>
	<wsdl:binding name="PingPongServiceServiceSoapBinding" type="tns:PingPongService">
	<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
		<wsdl:operation name="ping">
			<soap:operation soapAction="" style="document"/>
			<wsdl:input name="ping">
				<soap:body use="literal"/>
			</wsdl:input>
			<wsdl:output name="pingResponse">
				<soap:body use="literal"/>
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="PingPongServiceService">
		<wsdl:port binding="tns:PingPongServiceServiceSoapBinding" name="PingPongServicePort">
			<soap:address location="http://localhost:8180/pingpongservice/services/pingpongservice"/>
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>

Non sto ad annoiarvi con la classe Personaggio, un semplice bean con le proprietà Nome, Cognome e Nickname (la trovate comunque nell’esempio).

Questo è quanto.

Apache CXF - esempio di webservice utilizzando Apache CXF

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>