圌ãã¯ç§ã«WebãµãŒãã¹ã®éçºãéå§ããã¿ã¹ã¯ãèšå®ãã説æã®ãªãåçŽãªãããžã§ã¯ãã®ãããªãã®ããããŸããã ãã¡ããããããžã§ã¯ãã¯éå§ãããŸããã§ããã Springãšã¯äœãããããã©ã®ããã«æ©èœããã®ããç§ã«ãããããªãã£ãã ãã·ã¢èªãŸãã¯è±èªã®SpringããŒã«ã䜿çšããWebãµãŒãã¹ã®éçºã«é¢ããé©åãªèšäºãèŠã€ãããŸããã§ããã ç§ã¯èªåã§ãã¹ãŠãç解ããªããã°ãªããªãã£ããããã¯ãã¹ãŠãããã»ã©æããªãããšãå€æããã
ãããŠæè¿ãSpringã«è¿œå ãããæ°ããæ©èœã調ã¹ãå€ããµãŒãã¹ãæŽæ°ããããšã«ããŸããããã®çµæããã®èšäºãæžãããã«ãªããŸããã
ãã®èšäºã¯ãSpring-WSã䜿çšããŠSOAPãããã³ã«ã䜿çšããåçŽãªWebãµãŒãã¹ãéçºããããã®ã¬ã€ãã§ãã
ãããŠããŠãŒã¶ãŒåãåãå ¥ãããµãŒããŒäžã§æšæ¶ãšçŸåšæå»ãéä¿¡ããç°¡åãªãµãŒãã¹ãäœæããŸãã
äœãå¿ èŠã§ããïŒ
- IDE Eclipseã䜿çšããŠããŸã ã
- ã¢ãª
- soapUI
- Jdk
- Jre
- Spring Framework 3.1.2
- Spring-WS 2.1.0
- XMLBeans 2.5.0
- wsdl4j
- commons-logging-1.1.1
ä»äºã®æºå
æ°ããWebã¢ããªã±ãŒã·ã§ã³ãããžã§ã¯ããäœæããŸãã Eclipseã§ã¯ãããã¯ããã¡ã€ã«=>æ°èŠ=>åçWebãããžã§ã¯ããã§ãã
ãããžã§ã¯ããHelloServiceãšåŒã³ãŸããã
次ã«ãã©ã€ãã©ãªãSpringãXMLBeanãwsdl4jãcommons-loggingãããããžã§ã¯ããã£ã¬ã¯ããªWEB-INF / libã«ã³ããŒããŸãã
å¿ èŠã«å¿ããŠããã¹ãŠã®ã¢ããªã±ãŒã·ã§ã³ã§ãã©ãã°ããªãããã«ããµãŒããŒã©ã€ãã©ãªã«è¿œå ã§ããŸãã
WSDLã¹ããŒãã®äœæ
æ¬è³ªçã«ãWSDLã¹ããŒã ã¯ãµãŒãã¹ãèšè¿°ããããšãç®çãšããŠããŸãã
ãã¡ãããæåã§è¡ãããšã¯ãããŸããã ãã®ã¹ããŒã ã¯Springã«ãã£ãŠèªåçã«çæãããŸãããããã«ã€ããŠã¯åŸã§è©³ãã説æããŸãã
å ¥åããŒã¿ãšåºåããŒã¿ã決å®ããŸã
å ¥åããŒã¿ïŒ
- ã¹ããªã³ã°åã
åºåããŒã¿ïŒ
- ã¹ããªã³ã°ã°ãªãŒãã£ã³ã°;
- çŸåšæå»ã
å ¥åããŒã¿ãšåºåããŒã¿ã®èª¬æãäœæãã
WEB-INFãã£ã¬ã¯ããªã§ãHelloService.xsdãã¡ã€ã«ãäœæããŸãã ãã®ãã¡ã€ã«ã¯ãWSDLã¹ããŒã ãçæãã察å¿ããJavaã¯ã©ã¹ãäœæããããã«å¿ èŠã«ãªããŸãã
ãã¡ã€ã«ããã¹ãïŒ
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/HelloService" elementFormDefault="qualified"> <element name="ServiceRequest"> <complexType> <sequence> <element name="name" type="string" maxOccurs="1" minOccurs="1"/> </sequence> </complexType> </element> <element name="ServiceResponse"> <complexType> <sequence> <element name="hello" type="string" maxOccurs="1" minOccurs="1"/> <element name="currentTime" type="time" maxOccurs="1" minOccurs="1"/> </sequence> </complexType> </element> </schema>
targetNamespaceå±æ§ã¯ã䜿çšãããåå空éã§ãã ã€ãŸã äœæããããã¹ãŠã®ãªããžã§ã¯ãã¯ãããã±ãŒãžorg.example.helloServiceã«é 眮ãããŸãã
ServiceRequestèŠçŽ ãšServiceResponseèŠçŽ ã¯ãããããå ¥åããŒã¿ãšåºåããŒã¿ïŒèŠæ±/å¿çïŒãèšè¿°ããŸãã
minOccursããã³maxOccurså±æ§ã¯ã1ã€ã®èŠçŽ å ã§ã®ãã®ã³ã³ããŒãã³ãã®ç¹°ãè¿ãæ°ã決å®ããŸãã ãããã®ãã©ã¡ãŒã¿ãŒãæå®ãããŠããªãå Žåãããã©ã«ãã§ã¯1ãšçãããšèŠãªãããŸãããªãã·ã§ã³ã®ã³ã³ããŒãã³ãã®å ŽåãminOccurs = 0ãæå®ããå¿ èŠããããŸãã ã³ã³ããŒãã³ãã®æ°ã«å¶éã¯ãããŸããïŒmaxOccurs = unboundedã
XMLã¹ããŒãã®è©³çŽ°ã«ã€ããŠã¯ã ãã¡ããã芧ãã ãã ã
JavaBeansãäœæãã
äœæãããã¹ããŒã ã«åºã¥ããŠãJavaã¯ã©ã¹ãäœæããŸãã ãããè¡ãã«ã¯ãbuild.xmlãã¡ã€ã«ãäœæããŸãã
<?xml version="1.0" encoding="UTF-8"?> <project name="imsjob" default="build" basedir="."> <property name="WS_HOME" value="C:\AST\lib\standart"/> <property name="encoding" value="UTF-8"/> <path id="xbean.classpath"> <fileset dir="${WS_HOME}"> <include name="*.jar"/> </fileset> </path> <taskdef name="xmlbean" classname="org.apache.xmlbeans.impl.tool.XMLBean" classpathref="xbean.classpath" /> <target name="init"> <echo message="Start init"/> </target> <target name="build" depends="init"> <xmlbean schema="HelloService.xsd" destfile="lib\helloservice.jar" classpathref="xbean.classpath"/> </target> </project>
WS_HOMEãã©ã¡ãŒã¿ãŒã¯ãXMLBeansãé 眮ãããŠãããã£ã¬ã¯ããªãæããŠããå¿ èŠããããŸãã
HelloService.xsd-äœæãããã¹ããŒã ãžã®ãã¹ã
lib \ helloservice.jar-äœæãããjava-libraryã
次ã«ãAnt-buildãå®è¡ããŸãïŒæ¢ã«ã€ã³ã¹ããŒã«ãããŠããããšãæã¿ãŸãïŒã
Eclipseã§ã¯ã次ã®ããã«å®è¡ã§ããŸãããã¡ã€ã«build.xml => Run As => Ant Buildã䜿çšããŠRMBã
ã³ãã³ãã©ã€ã³çµç±ã®å ŽåïŒ
ant -buildfile build.xml
ããŠãç§ãã¡ã¯å»ºèšã®å®äºãåŸ ã£ãŠããŸãã ãã®åŸããããžã§ã¯ããã£ã¬ã¯ããªWEB-INF \ libã§é©åãªã©ã€ãã©ãªïŒhelloservice.jarïŒã®ååšã確èªã§ããŸãã
ãµãŒãã¹å®è£
ã€ã³ã¿ãŒãã§ã€ã¹ãšãµãŒãã¹ã¯ã©ã¹ãäœæããŸã
ãµãŒãã¹ã€ã³ã¿ãŒãã§ã€ã¹ïŒHelloService.javaïŒ
package org.example; import java.util.Calendar; public interface HelloService { public String getHello(String name) throws Exception; public Calendar getCurrentTime(); }
ãµãŒãã¹ã®å®è£ ïŒHelloServiceImpl.javaïŒ
package org.example; import java.util.Calendar; import org.springframework.stereotype.Service; @Service public class HelloServiceImpl implements HelloService { public String getHello(String name) throws Exception { return "Hello, " + name + "!"; } public Calendar getCurrentTime() { return Calendar.getInstance(); } }
ãã®ã³ãŒãã«ã¯ã³ã¡ã³ãã¯å¿ èŠãªããšæããŸãã ãããŸã§Springã«åºäŒã£ãããšããªã人ã質åãæããããå¯èœæ§ãããã®ã¯ã@ Serviceã¢ãããŒã·ã§ã³ã ãã§ãã ããããç§ã¯åãããšã«ã€ããŠå°ãåŸã§èª¬æããŸãã
ãšã³ããã€ã³ã
ãšã³ããã€ã³ã-çä¿¡èŠæ±ïŒäžçš®ã®ãšã³ããªãã€ã³ãïŒã®åŠçãæ åœããã¯ã©ã¹ã
ãã¡ã€ã«HelloServiceEndpoint.javaãäœæããŸãã
package org.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ws.server.endpoint.annotation.Endpoint; import org.springframework.ws.server.endpoint.annotation.PayloadRoot; import org.example.helloService.ServiceRequestDocument; import org.example.helloService.ServiceRequestDocument.ServiceRequest; import org.example.helloService.ServiceResponseDocument; import org.example.helloService.ServiceResponseDocument.ServiceResponse; @Endpoint public class HelloServiceEndpoint{ private static final String namespaceUri = "http://www.example.org/HelloService"; private HelloService helloService; @Autowired public void HelloService (HelloService helloService) { this.helloService = helloService; } @PayloadRoot(localPart = "ServiceRequest", namespace = namespaceUri) public ServiceResponseDocument getService(ServiceRequestDocument request) throws Exception { ServiceRequestDocument reqDoc = request; ServiceRequest req = reqDoc.getServiceRequest(); ServiceResponseDocument respDoc = ServiceResponseDocument.Factory.newInstance(); ServiceResponse resp = respDoc.addNewServiceResponse(); String userName = req.getName(); String helloMessage = testNewService.getHello(userName); Calendar currentTime = testNewService.getCurrentTime(); resp.setHello(helloMessage); resp.setCurrentTime(currentTime); return respDoc; } }
ããã§äœãè¡ãããŸãããïŒ
@Endpointã¢ãããŒã·ã§ã³ã¯ããã®ã¯ã©ã¹ãçä¿¡èŠæ±ãåŠçããããšã決å®ããã ãã§ãã
namespaceUri -xml-schemeã®äœææã«æå®ãããã®ãšåãããŒã ã¹ããŒã¹ã
ã§ã¯ãå°ãæ»ã£ãŠ@ Serviceã¢ãããŒã·ã§ã³ãæãåºããŸãããã äžå¿ èŠãªæ å ±ã§èªè ã«è² æ ããããããªãããã«è©³çŽ°ã«å ¥ããªãå Žåããã®æ³šéã¯é©åãªãªããžã§ã¯ããäœæããããSpringã«æ瀺ããŸãã ãŸãã @ Autowiredã¢ãããŒã·ã§ã³ã¯ã察å¿ãããªããžã§ã¯ããæ¿å ¥ïŒèªåçã«çœ®æïŒããã®ã«åœ¹ç«ã¡ãŸãã ãã¡ããããããã®æ³šéã䜿çšããŠåçŽãªã¢ããªã±ãŒã·ã§ã³ãæ§ç¯ããå Žåãæå³ã¯ãããŸãããããã®äŸã§ã¯ããããé€å€ããªãããšã«ããŸããã
ããã§ã¯ãå ã«é²ã¿ãŸãããã
ã¡ãœããã®åã®@PayloadRootã¢ãããŒã·ã§ã³ã¯ããã®ã¡ãœãããåŒã³åºããããšãããªã¯ãšã¹ãããã€åä¿¡ããããã決å®ããŸãã ãã®äŸã§ã¯ããServiceRequestãã§ãã
ããã§ãªããã°ãåã³ããã¹ãŠãæ確ã«ãªããŸãã ServiceRequestãServiceResponseãªã©ã«æ³šæããŠãã ããã -ãããã¯ãxml-schemeã«åºã¥ããŠäœæãããã¯ã©ã¹ã§ãã
SpringãµãŒãã¹ã®æ§æ
ãã®ãããå®æã«è¿ã¥ããŠããŸãã
service-ws-servlet.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:sws="http://www.springframework.org/schema/web-services" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="org.example" /> <sws:annotation-driven /> <bean class="org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter"> <property name="marshaller" ref="marshaller" /> <property name="unmarshaller" ref="marshaller" /> </bean> <bean id="marshaller" class="org.springframework.oxm.xmlbeans.XmlBeansMarshaller"/> <sws:dynamic-wsdl id="HelloService" portTypeName="service" locationUri="/HelloService" > <sws:xsd location="/WEB-INF/HelloService.xsd" /> </sws:dynamic-wsdl> </beans>
swsïŒæ³šéé§åå -ãã®ãããžã§ã¯ãã§ã¯æ³šéã䜿çšãããŠããããšã瀺ããŠããŸãã
ã³ã³ããã¹ãïŒcomponent-scanã¯ãã¢ãããŒã·ã§ã³ã®æ€çŽ¢ãå®è¡ãããããã±ãŒãžã瀺ããŸãããæ€çŽ¢ã¯ãµãããã±ãŒãžã§ãå®è¡ãããŸãã
次ã®2ã€ã®Beanã¯åžžã«å€æŽãããŸããã ãããã®æ¬è³ªã¯ãXmlããJavaãªããžã§ã¯ããžã®ãªã¯ãšã¹ãã®åä¿¡ãšå€æãããã«éå€æã§ãã
swsïŒdynamic-wsdlã¯ãçæãããXmlã¹ããŒã ã«åºã¥ããŠWSDLããã¥ã¡ã³ããèªåçã«çæããŸãã
locationã¯ãã¹ããŒããžã®ãã¹ã瀺ããŸãã
locationUri -WSDLã¹ããŒã ã䜿çšã§ããã¢ãã¬ã¹ïŒã³ã³ãããåºæºãšããïŒã
ç§ã®å ŽåãWSDLã¯æ¬¡ã®ã¢ãã¬ã¹ã§å ¥æã§ããŸãã
localhost / HelloService / HelloService.wsdl
ãããã€ã¡ã³ãèšè¿°å
ãããŠæåŸã«ãæåŸã
WEB-INFãã£ã¬ã¯ããªã§ãweb.xmlãã¡ã€ã«ãå€æŽãŸãã¯äœæããŸãã
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>HelloService</display-name> <description>HelloService</description> <servlet> <servlet-name>service-ws</servlet-name> <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class> <init-param> <param-name>transformWsdlLocations</param-name> <param-value>true</param-value> </init-param> <servlet-mapping> <servlet-name>service-ws</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
ãã®ãã¡ã€ã«ã«ã€ããŠã¯ãã説æããŸãããã»ãšãã©ã®äººã¯ãã§ã«ç¥ã£ãŠããã¯ãã§ãã åçŽãªãããžã§ã¯ãã®å Žåãæ¬è³ªçã«å€æŽããªãã§ãã ããã ãµãŒãã¬ããã®ååïŒservlet-nameïŒãservice-ws -servlet.xmlã®Springæ§æãã¡ã€ã«ã®ååãšäžèŽããªããã°ãªããªãããšã«æ³šæããŠãã ããã
ããã§ã¯ãã¢ããªã±ãŒã·ã§ã³ããµãŒããŒã«ãããã€ããŸãã
ããã§ããµãŒãã¹ã®äœæãå®äºããŸããã äœãèŠéããªãå ŽåããµãŒãã¹ã¯æ©èœããã¯ãã§ãã
ãã«ã¹ãã§ãã¯
æ£ããæäœã®æåã®å åã¯ãäœæãããWSDLã¹ããŒã ã§ãã
確èªããã«ã¯ããã®ã¹ããŒã ã®ã¢ãã¬ã¹ïŒhttpïŒ//localhost/HelloService/HelloService.wsdlïŒã«ç§»åããŠã次ãåç §ããŠãã ããïŒxmlãã¡ã€ã«ã衚瀺ãããã¯ãã§ãã äœã衚瀺ãããªãå Žåããšã©ãŒã衚瀺ãããå Žåã¯ãèšäºå šäœã泚ææ·±ãèªã¿çŽããééã£ãããšãæ¢ããŸãã
ããã«æ€èšŒããã«ã¯ãsoapUIãå¿ èŠã§ãïŒããŒãžã§ã³3.0.1ããããŸãïŒã
ã€ã³ã¹ããŒã«ããŠå®è¡ããŸãã
æ°ãããããžã§ã¯ããäœæããŸããFile=> New soapUI Projectã [åæWSDL / WADL]ãã£ãŒã«ãã«ãWSDLã¹ããŒã ãžã®ãªã³ã¯ãæ¿å ¥ããŸãïŒhttpïŒ//localhost/HelloService/HelloService.wsdlïŒã
äœæããããããžã§ã¯ãã§ãå¿ èŠãªãªã¯ãšã¹ããéããŸãã
[åå]ãã£ãŒã«ãã§ååãå ¥åãã[èŠæ±ãéä¿¡]ãã¿ã³ãã¯ãªãã¯ããŸã
çµæãšããŠãæšæ¶ãšçŸåšã®æå»ãå«ããµãŒããŒããå¿çãåãåããŸãã
äœãåé¡ãçºçããå Žåã¯ããã®èšäºãããäžåºŠãèªã¿ãã ããã
次ã¯ïŒ
ããã§ã¯ããã®WebãµãŒãã¹ã®ã¯ã©ã€ã¢ã³ããäœæããå¿ èŠããããŸãã ããããããã¯ãã§ã«å¥ã®èšäºã®è³æã§ããã誰ããèå³ãæã£ãŠããã®ã§ããã°ãåŸã§æžããããããŸããã