标签:order tom 电子商务 put cmd命令 机器 协议 万维网 获取
Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序。Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。Web Service是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。Web Service减少了应用接口的花费。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。
(详情可参考webservice的百度百科)
Web服务为Internet上应用程序之间的交互提供了方便
Web服务也减轻了企业级应用中出现的异构系统的整合危机
Web服务的优势包括:
1.使用JAX-WS发布和调用web服务
(JAX-WS--->web服务标准,jdk中的一个组件,集成了JAXB,本质上其实是Scoket编程)
01.发布自己的ws服务
源码介绍:
HelloService.java
package cn.myservice; //Service端(服务器端) import javax.jws.WebService; import javax.xml.ws.Endpoint; //局域网任何人都可以访问 @WebService public class HelloService { public void say(String name){ System.out.println("Hello"+name); } public static void main(String[] args) { /** * 端口号:50000 * 一个标识(区分的作用):hello * 发布者:new HelloService() */ Endpoint.publish("http://localhost:50000/hello", new HelloService()); System.out.println("server is listening ...."); } }
运行效果:
大家也可以用cmd命令 netstat -na来看看有没有我们发布的端口号的存在(如下图,它是处于监听状态的)
现在我们的局域网上都可以访问我发布的(http://localhost:50000/hello)这个服务了
效果:
02.调用自己的ws服务
001.MyEclipse自带工具调用
步骤一:
步骤二:
步骤三:
步骤四:
步骤五:
步骤六:
步骤七:
这时就完成了调用,控制台就会打印相应的信息
002.书写代码调用
其中我们myservice包中的类我们是不需要自己去写的,我们可以使用jdk中的wsimport.exe利用我们cmd命令给我们生成(当然是在保证我们的jdk安装,环境变量配置成功的情况下)
操作如下:
这时,我们来看看我们的c盘根目录:
源码介绍:
1.HelloService.java
package cn.myservice; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.ws.Action; import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; /** * This class was generated by the JAX-WS RI. * JAX-WS RI 2.2.9-b130926.1035 * Generated source version: 2.2 * */ @WebService(name = "HelloService", targetNamespace = "http://myservice.cn/") @XmlSeeAlso({ ObjectFactory.class }) public interface HelloService { /** * * @param arg0 */ @WebMethod @RequestWrapper(localName = "say", targetNamespace = "http://myservice.cn/", className = "cn.myservice.Say") @ResponseWrapper(localName = "sayResponse", targetNamespace = "http://myservice.cn/", className = "cn.myservice.SayResponse") @Action(input = "http://myservice.cn/HelloService/sayRequest", output = "http://myservice.cn/HelloService/sayResponse") public void say( @WebParam(name = "arg0", targetNamespace = "") String arg0); }
2.HelloServiceService.java
package cn.myservice; 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 = "HelloServiceService", targetNamespace = "http://myservice.cn/", wsdlLocation = "http://localhost:50000/hello?wsdl") public class HelloServiceService extends Service { private final static URL HELLOSERVICESERVICE_WSDL_LOCATION; private final static WebServiceException HELLOSERVICESERVICE_EXCEPTION; private final static QName HELLOSERVICESERVICE_QNAME = new QName("http://myservice.cn/", "HelloServiceService"); static { URL url = null; WebServiceException e = null; try { url = new URL("http://localhost:50000/hello?wsdl"); } catch (MalformedURLException ex) { e = new WebServiceException(ex); } HELLOSERVICESERVICE_WSDL_LOCATION = url; HELLOSERVICESERVICE_EXCEPTION = e; } public HelloServiceService() { super(__getWsdlLocation(), HELLOSERVICESERVICE_QNAME); } public HelloServiceService(WebServiceFeature... features) { super(__getWsdlLocation(), HELLOSERVICESERVICE_QNAME, features); } public HelloServiceService(URL wsdlLocation) { super(wsdlLocation, HELLOSERVICESERVICE_QNAME); } public HelloServiceService(URL wsdlLocation, WebServiceFeature... features) { super(wsdlLocation, HELLOSERVICESERVICE_QNAME, features); } public HelloServiceService(URL wsdlLocation, QName serviceName) { super(wsdlLocation, serviceName); } public HelloServiceService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) { super(wsdlLocation, serviceName, features); } /** * * @return * returns HelloService */ @WebEndpoint(name = "HelloServicePort") public HelloService getHelloServicePort() { return super.getPort(new QName("http://myservice.cn/", "HelloServicePort"), HelloService.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 HelloService */ @WebEndpoint(name = "HelloServicePort") public HelloService getHelloServicePort(WebServiceFeature... features) { return super.getPort(new QName("http://myservice.cn/", "HelloServicePort"), HelloService.class, features); } private static URL __getWsdlLocation() { if (HELLOSERVICESERVICE_EXCEPTION!= null) { throw HELLOSERVICESERVICE_EXCEPTION; } return HELLOSERVICESERVICE_WSDL_LOCATION; } }
3.ObjectFactory.java
package cn.myservice; 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 cn.myservice package. * <p>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 _SayResponse_QNAME = new QName("http://myservice.cn/", "sayResponse"); private final static QName _Say_QNAME = new QName("http://myservice.cn/", "say"); /** * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: cn.myservice * */ public ObjectFactory() { } /** * Create an instance of {@link SayResponse } * */ public SayResponse createSayResponse() { return new SayResponse(); } /** * Create an instance of {@link Say } * */ public Say createSay() { return new Say(); } /** * Create an instance of {@link JAXBElement }{@code <}{@link SayResponse }{@code >}} * */ @XmlElementDecl(namespace = "http://myservice.cn/", name = "sayResponse") public JAXBElement<SayResponse> createSayResponse(SayResponse value) { return new JAXBElement<SayResponse>(_SayResponse_QNAME, SayResponse.class, null, value); } /** * Create an instance of {@link JAXBElement }{@code <}{@link Say }{@code >}} * */ @XmlElementDecl(namespace = "http://myservice.cn/", name = "say") public JAXBElement<Say> createSay(Say value) { return new JAXBElement<Say>(_Say_QNAME, Say.class, null, value); } }
4.package-info.java
@javax.xml.bind.annotation.XmlSchema(namespace = "http://myservice.cn/") package cn.myservice;
5.Say.java
package cn.myservice; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlType; /** * <p>say complex type的 Java 类。 * * <p>以下模式片段指定包含在此类中的预期内容。 * * <pre> * <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"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "say", propOrder = { "arg0" }) public class Say { protected String arg0; /** * 获取arg0属性的值。 * * @return * possible object is * {@link String } * */ public String getArg0() { return arg0; } /** * 设置arg0属性的值。 * * @param value * allowed object is * {@link String } * */ public void setArg0(String value) { this.arg0 = value; } }
6.SayResponse.java
package cn.myservice; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlType; /** * <p>sayResponse complex type的 Java 类。 * * <p>以下模式片段指定包含在此类中的预期内容。 * * <pre> * <complexType name="sayResponse"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "sayResponse") public class SayResponse { }
7.MyTest.java(测试类)
package cn.test; import cn.myservice.HelloService; import cn.myservice.HelloServiceService; public class MyTest { public static void main(String[] args) { HelloServiceService service = new HelloServiceService(); HelloService port = service.getHelloServicePort(); port.say("坤坤"); } }
8.运行效果
这就完成了调用。
2.使用CXF发布和调用web服务
未完待续。。。
标签:order tom 电子商务 put cmd命令 机器 协议 万维网 获取
原文地址:http://www.cnblogs.com/zhangzongle/p/6034394.html