33.3.1 Webサービス実装クラスを作成する

Webサービスの処理を記述したWebサービス実装クラスを作成します。ここでは,受け取った要求メッセージの内容を計算して,応答メッセージを返すWebサービス実装クラスを作成します。

Webサービス実装クラスの作成例を次に示します。

package com.sample;


import javax.activation.DataHandler;
import javax.jws.WebService;
import javax.xml.ws.soap.MTOM;
import javax.xml.bind.annotation.XmlMimeType;
import com.sun.xml.ws.developer.StreamingAttachment;
import com.sun.xml.ws.developer.StreamingDataHandler;

@MTOM
@StreamingAttachment(dir="C:/TMP", parseEagerly=true, memoryThreshold=50000L)
@WebService(serviceName="UserInfoService",targetNamespace="http://sample.com")
public class UserInfoImpl {

   public UserData getUserData(String in0, @XmlMimeType("application/octet-stream")DataHandler in1)
       throws UserInfoException {

       if (in1 != null) {
           if (in1 instanceof StreamingDataHandler) {
               StreamingDataHandler sdh = null;
               try {
                   //社員情報への顔写真の登録処理
                   sdh = (StreamingDataHandler)in1;
                   ・・・・
               } catch(Exception e) {
                   throw new UserInfoException("Exception occurred.", e.getMessage());
               } finally {
                   try {
                       if (sdh != null) {
                           //データのクローズ
                           sdh.close();
                       }
                   } catch(Exception ex) {
                       ・・・・
                   }
               }
           }
       }

       UserData userdata = new UserData();
       //登録した社員の名前と所属を設定
       if (in0.equals("1")) {
           userdata.setName("Sato Taro");
           userdata.setSection("The personnel section");
       } if ( ・・・・) {
           ・・・・・
       } ・・・・

       //登録確認メッセージを設定
       if (in1 == null) {
           userdata.setMessage("Failure(no image).");
       } else {
           userdata.setMessage("Success.");
       }
       return userdata;
   }
}

作成したUserInfoImpl.javaは,UTF-8形式でc:¥temp¥jaxws¥works¥streaming¥server¥src¥com¥sample¥ディレクトリに保存します。

また,com.sample.UserInfoImplで使用しているユーザ定義型クラスcom.sample.UserDataを作成します。通常,ユーザ定義型クラスの作成は任意ですが,ここではユーザ定義型クラスを作成します。

ユーザ定義型クラスの作成例を次に示します。

package com.sample;

import java.lang.String;

public class UserData {

   private String message;
   private String name;
   private String section;

   public UserData() {
   }

   public String getMessage() {
       return this.message;
   }

   public void setMessage(String message) {
       this.message = message;
   }
   public String getName() {
       return this.name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getSection() {
       return this.section;
   }

   public void setSection(String section) {
       this.section = section;
   }
}

作成したUserData.javaは,UTF-8形式で,c:¥temp¥jaxws¥works¥streaming¥server¥src¥com¥sample¥ディレクトリに保存します。

またcom.sample.UserInfoImplでスローしている例外クラスcom.sample.UserInfoExceptionを作成します。通常,例外クラスの作成は任意ですが,ここでは例外クラスを作成します。

例外クラスの作成例を次に示します。

package com.sample;

import java.lang.Exception;
import java.lang.String;

public class UserInfoException extends Exception {

   String detail;

   public UserInfoException(String message, String detail) {
       super(message);
       this.detail = detail;
   }

   public String getDetail() {
       return detail;
   }
}

作成したUserInfoException.javaは,UTF-8形式でc:¥temp¥jaxws¥works¥streaming¥server¥src¥com¥sample¥ディレクトリに保存します。