JAX – WS – 09 – Spring & Apache CXF
Merhaba Arkadaslar
Bu bolumde Spring ve Apache CXF entegrasyonunu gerceklestirecegiz.
Burada Spring’i detayli olarak anlatmayacagim. Bunun icin ilgili bolumlere bakabilirsiniz ;
http://www.injavawetrust.com/spring-spring-mvc/spring-4-x/
http://www.injavawetrust.com/spring-spring-mvc/spring-mvc/
Maven Dependency
Yeni bir Dynamic Web Project olusturalim , Maven projesine cevirelim sonrasinda pom.xml dosyasina ilgili dependency tanimlarini ekleyelim.
pom.xml
<properties> <cxf.version>3.2.0</cxf.version> <spring.version>5.0.0.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
Web Service Implementation
Onceki orneklerde yaptigimiz gibi basit olarak bir Web Service ornek implemantasyonu yapalim.
HelloWorld.java
package ws; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface HelloWorld { String sayHi(@WebParam(name = "username") String username); }
HelloWorldImpl.java
package ws; import javax.jws.WebService; @WebService(endpointInterface = "ws.HelloWorld") public class HelloWorldImpl implements HelloWorld { @Override public String sayHi(String username) { System.out.println("sayHi is called !"); return "Hello, " + username; } }
XML Configuration
Simdi de web.xml dosyamizi olusturalim , yeni bir Servlet tanimi yapacagiz.
org.apache.cxf.transport.servlet.CXFServlet
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>injavawetrust.jaxws.cxf.spring</display-name> <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> </web-app>
/services/* istegi CXFServlet tarafindan karsilanacaktir. Burada herhangi bir adress verebiliriz.
Bir baska konfigurasyon dosyasi olarak; cxf-servlet.xml dosyasini ekleyelim.
Varsayilan olarak Spring container WEB-INF altinda cxf-servlet.xml dosyasini yukleyecektir.
The CXFServlet automatically creates and starts up a Spring container, which you can then use for defining Web service endpoints. By default, this Spring container automatically loads the following XML file in the WAR: WEB-INF/cxf-servlet.xml https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.1/html/Deploying_into_a_Web_Server/DeployCxf.html#DeployCxf-Example
cxf-servlet.xml
<jax-ws:endpoint> tag’i ile WebService tanimi yapabiliriz. Bu tag’i kullanabilmek icin ilgili xmlns eklememiz gerekmektedir.
1.konfigurasyonta sadece implementor ve address attribute’lerini kullaniyoruz. Bunlar gerekli minimum konfigurasyonlardir.
2.konfigurasyonda ise daha detayli sekilde konfigurasyon gerceklestirmekteyiz.
<?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" 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"> <jaxws:endpoint implementor="ws.HelloWorldImpl" address="/HelloWorldImplPort"> </jaxws:endpoint> <jaxws:endpoint xmlns:sampleNameSpace="http://hello.example.com/" address="/HelloWorldImplPortDetailed" serviceName="sampleNameSpace:HelloWorldImplService" endpointName="sampleNameSpace:HelloWorldImplPortDetailed" implementor="ws.HelloWorldImpl"> </jaxws:endpoint> </beans>
Run Application
Ornegimizi calistiralim ;
http://localhost:8080/injavawetrust.jaxws.cxf.spring/services
http://localhost:8080/injavawetrust.jaxws.cxf.spring/services/HelloWorldImplPortDetailed?wsdl http://localhost:8080/injavawetrust.jaxws.cxf.spring/services/HelloWorldImplPort?wsdl
Daha onceki bolumlerde inceledigimiz gibi SOAP UI uzerinde test yapabiliriz.
Bir baska ornek olarak su bolumde yaptigimiz ornegi , burada da uygulayabiliriz.
http://www.injavawetrust.com/jax-ws-06-spring-jax-ws/
cxf-servlet.xml
Yeni bir jaxws:endpoint ekleyebiliriz.
<jaxws:endpoint implementor="ws.ProductWebServiceImpl" address="/ProductWebServiceImplPort"> </jaxws:endpoint>
application.xml
WEB-INF dosyasi altinda application.xml dosyasini ekleyip injection’i gerceklestirebiliriz.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="productDataService" class="service.ProductDataServiceImpl"/> <bean id="productWebService" class="ws.ProductWebServiceImpl"> <property name="productDataService" ref="productDataService"/> </bean> </beans>
web.xml
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
Ornegimizi guncelleyip tekrar calistirdigimizda ;
Github kaynak kodlar / source folder
injavawetrust-jaxws-spring
Yazimi burada sonlandiriyorum.
Herkese bol Javali gunler dilerim.
Be an oracle man , import java.*;
Leave a Reply