JAX-WSã§Webãµã¼ãã¹ãä½ã£ã¦ã¿ã
ITPro -「Java SE 6完全攻略」第81回 JAX-WS その1ãããJava 6 ããæ¨æºæ·»ä»ã«ãªã£ãJAX-WSã使ãã¨ä»»æã®Javaã¯ã©ã¹ãããã£ã¨Webãµã¼ãã¹ã«ãã¦å ¬éã§ããããããã¨ãããã¨ã§è©¦ãã¦ã¿ã¾ããã
æ¦è¦
å ¬éã¾ã§ã®æé ã¯ä»¥ä¸ã®éãã
- Webãµã¼ãã¹ã¨ããã¯ã©ã¹ãæ¸ãã¦ãã¢ããã¼ã·ã§ã³ãä»ããã
- ä½æããã¯ã©ã¹ãã³ã³ãã¤ã«
- ã³ã³ãã¤ã«ããã¯ã©ã¹ãæå®ãã¦ãwsgenãå®è¡ã
- HTTPãµã¼ãã¼ãèµ·åããã©ã³ãã£ãæ¸ãã¦ããµã¼ãã¼ãèµ·åããã
1.Webãµã¼ãã¹ã¯ã©ã¹ãæ¸ãã¦ãã¢ããã¼ã·ã§ã³ãä»ãã
Webãµã¼ãã¹ã¨ãã¦å ¬éããJavaã®ã¯ã©ã¹ãæ¸ããå°ç¨ã®ã¢ããã¼ã·ã§ã³ãè¨å®ãã¾ãã
- ã¯ã©ã¹ã¯ä»»æã®POJOã§OKã
- ã¯ã©ã¹ã®ã¢ããã¼ã·ã§ã³ã¨ãã¦ããjavax.jws.WebServiceããã
- (å¿ é ã§ã¯ãªãã¨ã®ãã¨ã§ãã)å ¬éããã¡ã½ããã«ãjavax.jws.WebMethodããè¨å®ãã¾ãã
package jaxws.sample; import javax.jws.WebMethod; import javax.jws.WebService; /** * Webãµã¼ãã¹ã¨ãã¦å ¬éããã¯ã©ã¹ã * ä»»æã®POJOãã¢ããã¼ã·ã§ã³ã§ä¿®é£¾ããã°OK */ @WebService public class HelloService { @WebMethod public String hello() { return "Hello World!"; } }
2.ä½æããã¯ã©ã¹ãã³ã³ãã¤ã«
1ã§ä½æããã¯ã©ã¹ãã³ã³ãã¤ã«ãã¾ãã
$ javac jaxws/sample/HelloService
3.ã³ã³ãã¤ã«ããã¯ã©ã¹ãæå®ãã¦ãwsgenãå®è¡
ã³ã³ãã¤ã«ããã¯ã©ã¹ãå¼æ°ã§æå®ãã¦ãJDKä»å±ã®ãwsgenããå®è¡ãã¾ããã¯ã©ã¹ãã¹ã«ã2ã§ä½æããã¯ã©ã¹ãéãã¦ããå¿ è¦ãããã®ã§ãå¿ããªãã
$ wsgen -cp . jaxws.sample.HelloService
å®è¡å¾ãã./jaxws/sample/jaxwsã以ä¸ã«ããHello.classãã¨ãHelloResponse.classããä½æããã¾ãã
4.HTTPãµã¼ãã¼ãèµ·åããã©ã³ãã£ãæ¸ãã¦ããµã¼ãã¼ãèµ·åããã
ãã¨ã¯HTTPãµã¼ãã¼ãèµ·åããã©ã³ãã£ãæ¸ãã¦ããµã¼ãã¼ãèµ·åããã°OKã
package jaxws.sample; import javax.xml.ws.Endpoint; /** * HTTPãµã¼ãã¼ãèµ·åãã¦ãµã¼ãã¹ã®æä¾ãéå§ããã©ã³ãã£ã¼ */ public class Launcher { public static void main ( String[] args ) { Endpoint.publish( "http://localhost:8080/hello", new HelloService() ); } }
Endpointã¯ãjavax.xml.wsãããã±ã¼ã¸ã®ãã®ãªã®ã§ã注æããããã³ã³ãã¤ã«ãã¦å®è¡ããã°ããhttp://localhost:8080/helloãã§Webãµã¼ãã¹ãå ¬éããã¾ããwsdlããhttp://localhost:8080/hello?wsdlãã§åç §å¯ã
ã¯ã©ã¤ã¢ã³ãããæ¥ç¶ãã¦ã¿ãã
ãªãã¨ãªãrubyã§ãæ®éã«ã¢ã¯ã»ã¹ã§ãã¾ãã
require 'soap/wsdlDriver' wsdl = "http://localhost:8080/hello?wsdl" driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver puts driver.hello({}).return
å®è¡çµæã§ãã
Hello World!