JAX-WS自学笔记JAX-WS使用教程 1、JAX-WS概述 JAX-WS 2.0 的全称为 Java API for XML-Based Web services (JAX-WS) 2.0。JAX-WS 2.0 是对 JAX-RPC 1.0 规范的扩展,是 JAX-RPC 1.1 的后续版本, JAX-RPC 2.0标准发布不久后便被重新命名为 JAX-WS 2.0。 JAX-WS 2.0 是面向 Java 5 的开发 Web services 的最新编程标准,它提供了新的编程模型和对以往的 JAX-RPC 方式的 Web ser...
features
parameter will have their default values.
* @return
* returns Hello
*/
@WebEndpoint(name = "HelloPort")
public Hello getHelloPort(WebServiceFeature... features) {
return (Hello)super.getPort(new QName("http://ws/", "HelloPort"), Hello.class, features);
}
}
ObjectFactory.java
package test.ws;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the test.ws package.
* An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
private final static QName _Say_QNAME = new QName("http://ws/", "say");
private final static QName _SayResponse_QNAME = new QName("http://ws/", "sayResponse");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: test.ws
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link Say }
*
*/
public Say createSay() {
return new Say();
}
/**
* Create an instance of {@link SayResponse }
*
*/
public SayResponse createSayResponse() {
return new SayResponse();
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Say }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://ws/", name = "say")
public JAXBElement Java class for say complex type.
*
* The following schema fragment specifies the expected content contained within this class.
*
* Java class for sayResponse complex type.
*
* The following schema fragment specifies the expected content contained within this class.
*
*
* <complexType name="say">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="arg0" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="arg1" type="{http://www.w3.org/2001/XMLSchema}int"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
*
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "say", propOrder = {
"arg0",
"arg1"
})
public class Say {
protected String arg0;
protected int arg1;
/**
* Gets the value of the arg0 property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getArg0() {
return arg0;
}
/**
* Sets the value of the arg0 property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setArg0(String value) {
this.arg0 = value;
}
/**
* Gets the value of the arg1 property.
*
*/
public int getArg1() {
return arg1;
}
/**
* Sets the value of the arg1 property.
*
*/
public void setArg1(int value) {
this.arg1 = value;
}
}
SayResponse.java
package test.ws;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
/**
*
* <complexType name="sayResponse">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="return" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
*
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayResponse", propOrder = {
"_return"
})
public class SayResponse {
@XmlElement(name = "return")
protected String _return;
/**
* Gets the value of the return property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getReturn() {
return _return;
}
/**
* Sets the value of the return property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setReturn(String value) {
this._return = value;
}
}
创建SEI类
实现Hello接口,并添加@WebService注释,并至少为@WebService添加endpointInterface属性。
package test.ws;
import javax.jws.WebService;
@WebService(serviceName = "HelloService", portName = "HelloPort", endpointInterface = "test.ws.Hello", targetNamespace = "http://ws/")
public class HelloSEI implements Hello {
@Override
public String say(String arg0, int arg1) {
return "Hello , I am "+arg0+", I am "+arg1+" years old";
}
}
生成服务端要领:定义一个发布接口类,该接口类实现portType里定义的接口,重写该方法即可。在WebService里定义的serviceName = "HelloService"是服务,protName是服务里定义的端口,endpointInterface是指发布的接口,该接口即是实现的接口,即在portType里定义的操作类接口,targetNamespace是定义的域名,跟wsdl相关,建议修改wsdl里的域名,自定义域名即可.
2.3发布Web Service
2.3.1在应用程序中发布
JDK6提供了发布Web Service的简便方法: Endpoint.publish("http://localhost:8080/HelloService", new Hello());
如果是从WSDL生成的Web Service,则为, Endpoint.publish("http://localhost:8080/HelloService", new HelloSEI());
代码如下:
package test;
import javax.xml.ws.Endpoint;
import test.ws.HelloSEI;
public class TestHello {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/HelloService", new HelloSEI());
}
}
2.3.2在Web应用程序中发布
利用SUN公司提供的辅助包,可以将Web Service发布为Web应用程序。
2.3.2.1 依赖包
activation.jar
FastInfoset.jar
http.jar
jaxb-impl.jar
jaxws-rt.jar
mimepull.jar
resolver.jar
stax-ex.jar
streambuffer.jar
2.3.2.1发布步骤
1) 修改web.xml,添加
features
parameter will have their default values.
* @return
* returns Hello
*/
@WebEndpoint(name = "HelloPort")
public Hello getHelloPort(WebServiceFeature... features) {
return (Hello)super.getPort(new QName("http://ws/", "HelloPort"), Hello.class, features);
}
}
修改为,
package test.ws;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.1.1 in JDK 6
* Generated source version: 2.1
*
*/
@WebServiceClient(name = "HelloService", targetNamespace = "http://ws/")
public class HelloService
extends Service
{
private final static URL HELLOSERVICE_WSDL_LOCATION;
public HelloService(URL wsdlLocation){ //wsdl地址由构造函数提供,非指定
super(wsdlLocation, new QName("http://ws/", "HelloService"));
}
static {
URL url = null;
try {
url = new URL("file:/D:/MyEclipseNGCRMWorkspace/jax-ws/wsdl/HelloService.wsdl");
} catch (MalformedURLException e) {
e.printStackTrace();
}
HELLOSERVICE_WSDL_LOCATION = url;
}
public HelloService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public HelloService() {
super(HELLOSERVICE_WSDL_L