JAX-WS:Java API for XML Web Services是Java程序设计语言一个用来创建Web服务的API。JAX-WS是sun的Java企业平台一部分。和其它Java EE的API一样,JAX-WS使用了Java SE 5引入的Java标注机制来简化Web服务客户端和服务端的开发和部署。

1.创建Web Services Endpoint Interface
HelloWolrd.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.devnp.jaxws;

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

@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld {

@WebMethod
String getHelloWorld(String name);
}

2.创建 Web Service Endpoint Implementation
HelloWolrdImpl.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.devnp.jaxws;

import javax.jws.WebService;

@WebService(endpointInterface = "com.devnp.jaxws.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

@Override
public String getHelloWorld(String name) {
// TODO Auto-generated method stub
return "Hello, This is JAX-WS " + name;
}

}

3.创建 Endpoint Publisher
HelloWolrdPublisher.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.devnp.endpoint;

import javax.xml.ws.Endpoint;

import com.devnp.jaxws.HelloWorldImpl;

public class HelloWolrdPublisher {

public static void main(String[] args) {
// TODO Auto-generated method stub

Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
}

}

4.测试
Web Services的测试工具,可以使用SoapUI。
通过浏览器来访测试地址:


SoapUI 测试:

4.1 通过代码来测试
HelloWolrdPublisherTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.devnp.endpoint;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.devnp.jaxws.HelloWorld;

public class HelloWolrdPublisherTest {
public static void main(String[] args) throws Exception {

URL url = new URL("http://localhost:9999/ws/hello?wsdl");

QName qname = new QName("http://jaxws.devnp.com/", "HelloWolrdImplService");

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

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

System.out.println(hello.getHelloWorld("Du Liu"));

}
}

输出结果:
Hello, This is JAX-WS Du Liu

4.2 使用wsimport 来创建客户端

wsimport 常用命令:https://docs.oracle.com/javase/6/docs/technotes/tools/share/wsimport.html

使用命令如下:
wsimport -keep http://localhost:9999/ws/hello?wsdl

需要注意的是生成的java类的路径和原路径是一样。
生成的代码如下:
HelloWolrd.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

package com.devnp.client;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Action;


/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.9-b130926.1035
* Generated source version: 2.2
*
*/
@WebService(name = "HelloWorld", targetNamespace = "http://jaxws.devnp.com/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HelloWorld {


/**
*
* @param arg0
* @return
* returns java.lang.String
*/
@WebMethod
@WebResult(partName = "return")
@Action(input = "http://jaxws.devnp.com/HelloWorld/getHelloWorldRequest", output = "http://jaxws.devnp.com/HelloWorld/getHelloWorldResponse")
public String getHelloWorld(
@WebParam(name = "arg0", partName = "arg0")
String arg0);

}

HelloWolrdImplService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

package com.devnp.client;

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.WebServiceException;
import javax.xml.ws.WebServiceFeature;


/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.9-b130926.1035
* Generated source version: 2.2
*
*/
@WebServiceClient(name = "HelloWorldImplService", targetNamespace = "http://jaxws.devnp.com/", wsdlLocation = "http://localhost:9999/ws/hello?wsdl")
public class HelloWorldImplService
extends Service
{

private final static URL HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
private final static WebServiceException HELLOWORLDIMPLSERVICE_EXCEPTION;
private final static QName HELLOWORLDIMPLSERVICE_QNAME = new QName("http://jaxws.devnp.com/", "HelloWorldImplService");

static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("http://localhost:9999/ws/hello?wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
HELLOWORLDIMPLSERVICE_WSDL_LOCATION = url;
HELLOWORLDIMPLSERVICE_EXCEPTION = e;
}

public HelloWorldImplService() {
super(__getWsdlLocation(), HELLOWORLDIMPLSERVICE_QNAME);
}

public HelloWorldImplService(WebServiceFeature... features) {
super(__getWsdlLocation(), HELLOWORLDIMPLSERVICE_QNAME, features);
}

public HelloWorldImplService(URL wsdlLocation) {
super(wsdlLocation, HELLOWORLDIMPLSERVICE_QNAME);
}

public HelloWorldImplService(URL wsdlLocation, WebServiceFeature... features) {
super(wsdlLocation, HELLOWORLDIMPLSERVICE_QNAME, features);
}

public HelloWorldImplService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}

public HelloWorldImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}

/**
*
* @return
* returns HelloWorld
*/
@WebEndpoint(name = "HelloWorldImplPort")
public HelloWorld getHelloWorldImplPort() {
return super.getPort(new QName("http://jaxws.devnp.com/", "HelloWorldImplPort"), HelloWorld.class);
}

/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* @return
* returns HelloWorld
*/
@WebEndpoint(name = "HelloWorldImplPort")
public HelloWorld getHelloWorldImplPort(WebServiceFeature... features) {
return super.getPort(new QName("http://jaxws.devnp.com/", "HelloWorldImplPort"), HelloWorld.class, features);
}

private static URL __getWsdlLocation() {
if (HELLOWORLDIMPLSERVICE_EXCEPTION!= null) {
throw HELLOWORLDIMPLSERVICE_EXCEPTION;
}
return HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
}

}

测试:
HelloWorldClientTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.devnp.client;

public class HelloWorldClientTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
HelloWorldImplService helloService = new HelloWorldImplService();
HelloWorld hello = helloService.getHelloWorldImplPort();

System.out.println(hello.getHelloWorld("DU LIU"));
}

}

结果:
Hello, This is JAX-WS DU LIU
5. 代码下载
JAX-WS-HelloWolrd.zip