JAX-WS
The JAX-WS set of packages and classes is the successor to the Java API for XML-based RPC (JAX-RPC), a technology for making remote procedure calls from one Java object to another over the Internet.
The following codes are on JDK6 7 JDK8.
-------------------------------------
//a Service Endpoint Interface
package com.jaxws;
import javax.jws.*;
import javax.jws.soap.*;
import javax.jws.soap.SOAPBinding.*;
@WebService
@SOAPBinding(style = Style.RPC)
public interface TestServer {
@WebMethod double getTestValue(double input);
@WebMethod String getTime();
}
-------------------------------------
//a Service Implemmentation Bean
package com.jaxws;
import java.util.*;
import javax.jws.*;
@WebService(endpointInterface = "com.jaxws.TestServer")
public class TestServerImpl implements TestServer {
@Override
public double getTestValue(double input) {
return Math.ceil(input);
}
@Override
public String getTime() {
Date now = new Date();
return now.toString();
}
}
-------------------------------------
// a Service Publisher
package com.jaxws;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.xml.ws.*;
public class TestServerPublisher {
public static void main(String[] arguments) {
try {
TestServerImpl tws = new TestServerImpl();
Endpoint.publish("http://127.0.0.1:5665/service", tws);
//Open a browser and load the address http://127.0.0.1:5665/service?wsdl
Desktop d;
d = Desktop.getDesktop();
URI uri = new URI("http://127.0.0.1:5665/service?wsdl");
d.browse(uri);
} catch (URISyntaxException ex) {
...
} catch (IOException ex) {
...
}
}
}
-------------------------------------
//a Web Service Client
package com.test;
import com.jaxws.TestServer;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
class TestClient{
public static void main(String[] arguments) throws Exception {
URL url = new URL("http://127.0.0.1:5665/service?wsdl");
QName qname = new QName("http://jaxws.com/","TestServerImplService");
Service service = Service.create(url, qname);
TestServer tws = service.getPort(TestServer.class);
System.out.println(tws.getTime());
System.out.println(tws.getTestValue(135D));
}
}
------------------------------------
Tools:
wsimport Ref: https://jax-ws.java.net/jax-ws-ea3/docs/wsimport.html
wsimport -verbose -p com.test.ws -keep http://127.0.0.1:5665/service?wsdl
wsgen -verbose -keep -cp "." -wsdl com.jaxws.TestServerImpl
Endpoint.publish is on com.sun.net.httpserver. https://blogs.oracle.com/michaelmcm/entry/http_server_api_in_java
The following codes are on JDK6 7 JDK8.
-------------------------------------
//a Service Endpoint Interface
package com.jaxws;
import javax.jws.*;
import javax.jws.soap.*;
import javax.jws.soap.SOAPBinding.*;
@WebService
@SOAPBinding(style = Style.RPC)
public interface TestServer {
@WebMethod double getTestValue(double input);
@WebMethod String getTime();
}
-------------------------------------
//a Service Implemmentation Bean
package com.jaxws;
import java.util.*;
import javax.jws.*;
@WebService(endpointInterface = "com.jaxws.TestServer")
public class TestServerImpl implements TestServer {
@Override
public double getTestValue(double input) {
return Math.ceil(input);
}
@Override
public String getTime() {
Date now = new Date();
return now.toString();
}
}
-------------------------------------
// a Service Publisher
package com.jaxws;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.xml.ws.*;
public class TestServerPublisher {
public static void main(String[] arguments) {
try {
TestServerImpl tws = new TestServerImpl();
Endpoint.publish("http://127.0.0.1:5665/service", tws);
//Open a browser and load the address http://127.0.0.1:5665/service?wsdl
Desktop d;
d = Desktop.getDesktop();
URI uri = new URI("http://127.0.0.1:5665/service?wsdl");
d.browse(uri);
} catch (URISyntaxException ex) {
...
} catch (IOException ex) {
...
}
}
}
-------------------------------------
//a Web Service Client
package com.test;
import com.jaxws.TestServer;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
class TestClient{
public static void main(String[] arguments) throws Exception {
URL url = new URL("http://127.0.0.1:5665/service?wsdl");
QName qname = new QName("http://jaxws.com/","TestServerImplService");
Service service = Service.create(url, qname);
TestServer tws = service.getPort(TestServer.class);
System.out.println(tws.getTime());
System.out.println(tws.getTestValue(135D));
}
}
------------------------------------
Tools:
wsimport Ref: https://jax-ws.java.net/jax-ws-ea3/docs/wsimport.html
wsimport -verbose -p com.test.ws -keep http://127.0.0.1:5665/service?wsdl
wsgen -verbose -keep -cp "." -wsdl com.jaxws.TestServerImpl
Endpoint.publish is on com.sun.net.httpserver. https://blogs.oracle.com/michaelmcm/entry/http_server_api_in_java
评论
发表评论