JAX-WS从Web服务返回复杂对象

时间:2018-12-02 18:57:35

标签: jax-ws custom-object

我刚开始使用JAX-WS。基于此this代码,我刚刚添加了一个自定义类(Book),这是我想从Web服务返回的内容。 它有效,但是我有两个问题:

  1. 为什么自动调用函数setBookNamesetBookPublisher?是因为命名约定吗?我的意思是,如果我有一个变量bookName,JAX-WS会搜索一个函数setBookName作为设置器(并搜索getBookName作为获取器)?
  2. 这两个函数为什么被两次调用? 运行应用程序,我有以下输出:

    Hello World JAX-WS mkyong

    将书名设置为“ JAX-WS-Tutorial”

    将出版商设置为“我自己”

    将书名设置为“ JAX-WS-Tutorial”

    将出版商设置为“我自己”

    获取图书名称:JAX-WS-Tutorial,出版商:Myself

Book.java:

package com.mkyong.ws;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement( name = "Book" )
public class Book {

  private String bookName;
  private String bookPublisher;

  public Book() {
    this.bookName = "JAX-WS-Tutorial";
    this.bookPublisher = "Myself";
  }


  public void setBookName(String bookName) {
    this.bookName = bookName;
    System.out.format("\nSetting bookname to '%s'\n",  bookName);
  }


  public String getBookName() {
    return this.bookName;
  }


  public void setBookPublisher(String bookPublisher) {
    this.bookPublisher = bookPublisher;
    System.out.format("\nSetting bookpublisher to '%s'\n",  bookPublisher);
  }

  public String getBookPublisher() {
    return this.bookPublisher;
  }
}

HelloWorld.java:

package com.mkyong.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld{
  @WebMethod String getHelloWorldAsString(String name);
  @WebMethod Book getBook();
}

HelloWorldClient.java:

package com.mkyong.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.mkyong.ws.HelloWorld;

public class HelloWorldClient{

  public static void main(String[] args) throws Exception {
    URL url = new URL("http://localhost:9999/ws/hello?wsdl");

    //1st argument service URI, refer to wsdl document above
    //2nd argument is service name, refer to wsdl document above
    QName qname = new QName("http://ws.mkyong.com/", "HelloWorldCodeService");

    Service service = Service.create(url, qname);

    HelloWorld hello = service.getPort(HelloWorld.class);

    System.out.println(hello.getHelloWorldAsString("mkyong"));

    System.out.format("\nGet book name: %s,  publisher: %s\n", hello.getBook().getBookName(), hello.getBook().getBookPublisher());
  }

}

HelloWorldCode.java:

package com.mkyong.ws;

import javax.jws.WebService;

//Service Implementation
@WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldCode implements HelloWorld {

  private Book book;


  public HelloWorldCode() {
      this.book = new Book();
  }

  @Override
  public String getHelloWorldAsString(String name) {
      return "Hello World JAX-WS " + name;
  }

  @Override
  public Book getBook() {
      return this.book;
  }
}

HelloWorldPublisher.java:

package com.mkyong.endpoint;

import javax.xml.ws.Endpoint;
import com.mkyong.ws.HelloWorldCode;

//Endpoint publisher
public class HelloWorldPublisher{

  public static void main(String[] args) {
     Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldCode());
  }

}

0 个答案:

没有答案