【Java】【JAX-WS】 Webサービス / Metro [3] ~基本編 / オブジェクトのやり取り ~

はじめに

http://blogs.yahoo.co.jp/dk521123/36139336.html
http://blogs.yahoo.co.jp/dk521123/36140561.html
の続き。

上記の2つの関連記事を拡張し、今度は独自のオブジェクトのやり取りできるようにする

サンプル

サーバサイドの実装

SampleWebService.java
package com.sample.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlElement;

import com.sample.webservice.entities.SampleInputEntity;
import com.sample.webservice.entities.SampleOutputEntity;

@WebService(name = "SampleWebService")
public class SampleWebService {

  @WebMethod
  public SampleOutputEntity sayYourAge(
      @XmlElement(required = true, nillable = false)
      @WebParam(name = "inputEntity")
      SampleInputEntity inputEntity) {


    String output;
    if (inputEntity.getAge() == null) {
      output = "SAY AGE!!";
    } else {
      output = inputEntity.getName() + "'s age is " + inputEntity.getAge() + ".";
    }
    return new SampleOutputEntity(output);
  }
}
SampleInputEntity.java
package com.sample.webservice.entities;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SampleInputEntity")
public class SampleInputEntity implements Serializable {
  private static final long serialVersionUID = 1L;

  @XmlElement(name = "name", required = true, nillable = false)
  private String name;
  @XmlElement(name = "age", required = false, nillable = true)
  private Long age;
  
  // 【2】JAX-WSを使用する場合、デフォルト・コンストラクタが必要
  public SampleInputEntity() {
    this.name = "";
    this.age = null;
  }

  public SampleInputEntity(String name, Long age) {
    this.name = name;
  }

  public String getName() {
    return this.name;
  }

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

  public Long getAge() {
    return this.age;
  }

  public void setAge(Long age) {
    this.age = age;
  }
}
SampleOutputEntity.java
package com.sample.webservice.entities;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SampleOutputEntity")
public class SampleOutputEntity implements Serializable {
  private static final long serialVersionUID = 1L;

  @XmlElement(name = "output")
  private String output;

  // 【2】JAX-WSを使用する場合、デフォルト・コンストラクタが必要
  public SampleOutputEntity() {
    this.output = "";
  }

  public SampleOutputEntity(String output) {
    this.output = output;
  }

  public String getOutput() {
    return this.output;
  }

  public void setOutput(String output) {
    this.output = output;
  }
}

クライアントサイドの実装

 * サーバサイドのインターフェースが変わったので、サーバを再起動したのちに、
   以下の関連記事で行った wsimportコマンドを実行し、クライアント側のコードを再作成自する必要がある
http://blogs.yahoo.co.jp/dk521123/36140561.html
Main.java
package com.sample.webservice.client;

import javax.xml.bind.JAXBElement;

import com.sample.webservice.client.stub.ObjectFactory;
import com.sample.webservice.client.stub.SampleInputEntity;
import com.sample.webservice.client.stub.SampleOutputEntity;
import com.sample.webservice.client.stub.SampleWebService;
import com.sample.webservice.client.stub.SampleWebServiceService;

public class Main {

  public static void main(String[] args) {
    SampleWebServiceService service = new SampleWebServiceService();
    SampleWebService proxy = service.getSampleWebServicePort();

    ObjectFactory factory = new ObjectFactory();

    SampleInputEntity inputEntity1 = factory.createSampleInputEntity();
    inputEntity1.setName("Ken");
    JAXBElement<Long> inputValue1 = factory.createSampleInputEntityAge(11L);
    inputEntity1.setAge(inputValue1);
    
    SampleOutputEntity result1 = proxy.sayYourAge(inputEntity1);
    System.out.println("Result1 : " + result1.getOutput());

    SampleInputEntity inputEntity2 = factory.createSampleInputEntity();
    inputEntity1.setName("Mike");
    inputEntity1.setAge(null);
    
    SampleOutputEntity result2 = proxy.sayYourAge(inputEntity2);
    System.out.println("Result2 : " + result2.getOutput());
  }
}

関連記事

Webサービス / Metro [1] ~入門編 / サーバサイドの構築 ~

http://blogs.yahoo.co.jp/dk521123/36139336.html

Webサービス / Metro [2] ~入門編 / クライアントサイドの構築 ~

http://blogs.yahoo.co.jp/dk521123/36140561.html