1 / 18

Web サービス II ( 第 7 回)

Web サービス II ( 第 7 回). http://www.wakhok.ac.jp/~tatsuo/ws2/ 200 6 年 11 月 8 日 植田龍男. JAX-WS. 新しい Web サービスの枠組み じゃ、古い Web サービスって? JAX-RPC 1.0 JAX-RPC 2.0 = JAX-WS 2.0 JAXB を利用して実現 Web サービスのアノテーションの導入. クライアントプログラムの作成 (1). 自動生成ツール wsimport wsimport HelloService.wsdl

galya
Download Presentation

Web サービス II ( 第 7 回)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Webサービス II (第7回) http://www.wakhok.ac.jp/~tatsuo/ws2/ 2006年11月8日 植田龍男

  2. JAX-WS • 新しい Webサービスの枠組み • じゃ、古い Webサービスって? JAX-RPC 1.0 JAX-RPC 2.0 = JAX-WS 2.0 • JAXB を利用して実現 • Webサービスのアノテーションの導入

  3. クライアントプログラムの作成(1) • 自動生成ツール wsimport wsimport HelloService.wsdl wsimport http://localhost/Service?wsdl • HelloImpl.class, HelloServiceImpl.class, ObjectFactory.class SayHello.class, SayHelloResponse.class

  4. クライアントプログラムの作成(2) public class HelloClient { public static void main ( String[] args ) { HelloImpl port = new HelloImplService().getHelloImplPort(); String message =port.sayHello( "Tatsuo" ); System.out.printf ( message );

  5. JAX-WSの補足 • REST(Representational State Transfer) HTTP で XMLのデータなどを交換するスタイルのWebサービス • SOAP(XML-RPC) vs. REST ?? • JAX-WS は JAX-RPC の後継だが RESTにも対応可能 – JAXB がベース • サービス固有のXML Java のクラス

  6. JAX-WS のAPI • javax.jws パッケージ @WebService, @WebMethod などのアノテーション • javax.jws.soap パッケージ @SOAPBinding などアノテーション • javax.xml.ws パッケージとサブパッケージ群

  7. javax.xml.ws パッケージ • Service クラス Serviceのインナクラス enum ServiceMode ServiceMode.MESSAGE ServiceMode.PAYLOAD • Provider, Dispatch, Response などのインタフェース群

  8. JAX-WS for REST の現状 • 一部の仕様が流動的(?) • バグ有り(?) • 情報も少ない • 参照) Marc Hadley氏のブログ http://weblogs.java.net/blog/mhadley/archive/2006/03/restful_web_ser_1.html

  9. サンプル(Yahoo News検索) • Yahoo News Search の API http://developer.yahoo.com/search/news/V1/newsSearch.html • XML Schema も公開 http://api.search.yahoo.com/NewsSearchService/V1/NewsSearchResponse.xsd

  10. サンプル実行までの手順 • スキーマから xjc でJavaのソースを生成 (この時、一部手を入れる必要あり) • 生成されたソースのコンパイル • クライアントのサンプル作成とコンパイル • 実行

  11. xjc と生成されるソース • xjc NewsSearchResponse.xsd • yahoo/yp 以下に ResultSet.java, ResultType.java, ImageType.java ObjectFactory.java, package-info.java

  12. ちょっと修正が必要? Yahoo/yn/ResultSet.java @XmlRootElement(name = “ResultSet”) public class ResultSet { @XmlRootElement(name = "ResultSet", namespace = "urn:yahoo:yn")

  13. サンプルソース(1) URI nsURI = new URI("urn:yahoo:yn"); QName serviceName = new QName("yahoo",nsURI.toString()); QName portName = new QName("yahoo_port",nsURI.toString()); Service s = Service.create(serviceName);

  14. サンプルソース(2) URI address = new URI("http", null, "api.search.yahoo.com", 80, "/NewsSearchService/V1/newsSearch", "appid=jaxws_restful_sample&type=all& results=10&sort=date&query=Japan“ ,null); s.addPort(portName, HTTPBinding.HTTP_BINDING, address.toString());

  15. サンプルソース(3) Dispatch&lt;Object&gt; d = s.createDispatch(portName, jbc, Service.Mode.PAYLOAD); Map<String, Object> requestContext = d.getRequestContext(); requestContext.put( MessageContext.HTTP_REQUEST_METHOD, "GET"); StreamSource ss = (StreamSource)d.invoke(null);

  16. サンプルのソース(4) JAXBContext jbc = JAXBContext.newInstance( "yahoo.yn" ); Unmarshaller u = jbc.createUnmarshaller(); ResultSet rs = (ResultSet)u.unmarshal(ss); for (ResultType r: rs.getResult()) { System.out.println( r.getTitle() + ““ + r.getClickUrl() ); }

  17. ビルドファイル(build.xml) その1 <target name="compile"> <javac destdir="." debug="on"> <src path="." /> <classpath refid=“classpath” /> </javac> </target>

  18. ビルドファイル(build.xml) その2 <target name="run" depends="compile"> <java classname="YahooNewsTest" fork="true"> <classpath refid="classpath" /> </java> </target>

More Related