JAX – WS – 06 – Spring & JAX – WS
Merhaba Arkadaslar
Bu bolumde Spring ve JAX-WS 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
<dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.0.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.xml.ws/jaxws-rt --> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>2.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jvnet.jax-ws-commons.spring/jaxws-spring --> <dependency> <groupId>org.jvnet.jax-ws-commons.spring</groupId> <artifactId>jaxws-spring</artifactId> <version>1.9</version> </dependency> </dependencies>
Model
Product sinifimizi tanimlayalim…
Product.java
package model; public class Product { private int id; private String name; private double price; public Product() { super(); } public Product(int id, String name, double price) { super(); this.id = id; this.name = name; this.price = price; } // getters and setters // toString }
Sample Data
Test amacli bir kac tane Product objesi olusturalim ve List’e ekleyelim.
ProductData.java
package data; import java.util.ArrayList; import java.util.List; import model.Product; public class ProductData { public static List<Product> products = new ArrayList<Product>(); static { // Product product1 = new Product(1, "SAMSUNG G610", 1.2999); Product product2 = new Product(2, "ASUS X550VX", 3.4999); Product product3 = new Product(3, "HP15-BS017NT", 1.598); // products.add(product1); products.add(product2); products.add(product3); } }
Find Product Service / Service Layer
Simdi de id degerine gore List’ten geriye ilgili Product objesini bulan test metodumuzu olusturalim.
ProductDataService.java
package service; import model.Product; public interface ProductService { public Product getProductById(int id); }
ProductDataServiceImpl.java
package service; import data.ProductData; import model.Product; public class ProductDataServiceImpl implements ProductDataService { @Override public Product getProductById(int id) { Product product = ProductData.products.stream() .filter(p -> p.getId() == id).findFirst() .orElse(new Product(0, "Not Found!", 0.0)); return product; } }
Web Service Implementation
Simdi de Web Service interface ve class’imizi uygulayalim.
ProductWebService.java
getProductById metodu id degeri almakta ve geriye Product donmekte.
package ws; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; import model.Product; @WebService @SOAPBinding(style = Style.RPC) public interface ProductWebService { @WebMethod public Product getProductById(@WebParam(name = "id") int id); }
ProductWebServiceImpl.java
package ws; import javax.jws.WebService; import model.Product; import service.ProductDataService; @WebService(endpointInterface = "ws.ProductWebService") public class ProductWebServiceImpl implements ProductWebService { private ProductDataService productDataService; public void setProductDataService(ProductDataService productDataService) { this.productDataService = productDataService; } @Override public Product getProductById(int id) { Product product = productDataService.getProductById(id); return product; } }
Deployment Descriptor Configuration
Simdi de web.xml dosyamizi olusturalim. Servlet tanimi olarak com.sun.xml.ws.transport.http.servlet.WSSpringServlet sinifini kullanacagiz.
org.springframework.web.context.ContextLoaderListener sinifini ile configurasyonumuzu yapabiliriz.
Boylece applicationContext.xml dosyamiz okunacaktir.
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.spring</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>WSSpringServlet</servlet-name> <servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>WSSpringServlet</servlet-name> <url-pattern>/productSOAPService</url-pattern> </servlet-mapping> </web-app>
applicationContext.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://jax-ws.dev.java.net/spring/core http://jax-ws.java.net/spring/core.xsd http://jax-ws.dev.java.net/spring/servlet http://jax-ws.java.net/spring/servlet.xsd "> <bean id="productDataService" class="service.ProductDataServiceImpl"/> <bean id="productWebService" class="ws.ProductWebServiceImpl"> <property name="productDataService" ref="productDataService"/> </bean> <wss:binding url="/productSOAPService"> <wss:service> <ws:service bean="#productWebService" /> </wss:service> </wss:binding> </beans>
service.ProductDataServiceImpl ve ws.ProductWebServiceImpl icin bean tanimi yapalim.
Setter injection’i gerceklestirelim.
<wss:binding> icin URL tanimini ekleyelim. Burada dikkat edecek olursak web.xml dosyasinda tanimladigimiz URL ile ayni.
<wss:service> , <ws:service> bean tanimi icin #productWebService bilgisi veriyoruz.
Run Application
Ornegimizi SOAP UI ile test edebiliriz.
http://localhost:8080/injavawetrust.jaxws.spring/productSOAPService?wsdl
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