SpringBoot实现WebService
1.AXIS2实现方式
axis2调用客户端
@Component
public class Axis2WsClientTest {
//默认超时时间20s
public static final Long timeOutInMilliSeconds = 2 * 20000L;
/**
* axis2调用客户端
*
* @param url 请求服务地址
* @param nameSpace 命名空间
* @param method 方法名
* @param reqXml 请求报文
* @param timeOutInMilliSeconds 超时时间
* @return String 类型的响应报恩
* @throws AxisFault 遇到异常则抛出不处理
*/
public static String sendWsMessage(String url, String nameSpace, String method, String reqXml, long timeOutInMilliSeconds) throws AxisFault {
String resXml = null;
try {
EndpointReference targetEPR = new EndpointReferen
RPCServiceClient sender = new RPCServiceClient();
Options options = sender.getOptions();
options.setTimeOutInMilliSeconds(timeOutInMilliSeconds); //超时时间20s
options.setTo(targetEPR);
QName qName = new QName(nameSpace, method);
Object[] param = new Object[]{reqXml};
//这是针对返值类型的
Class<?>[] types = new Class[]{String.class};
Object[] response = sender.invokeBlocking(qName, param, types);
resXml = response[0].toString();
// System.out.println("响应报文:" + resXml);
} catch (AxisFault e) {
e.printStackTrace();
throw e;
}
return resXml;
}
/**
* axis2调用客户端
*
* @param url 请求服务地址
* @param nameSpace 命名空间
* @param method 方法名
* @param reqXml 请求报文
* @param timeOutInMilliSeconds 超时时间
* @return String 类型的响应报恩
* @throws AxisFault 遇到异常则抛出不处理
*/
public static String sendWsMessage2(String url, String nameSpace, String method, String action, String reqXml, long timeOutInMilliSeconds) throws AxisFault {
String resXml = null;
try {
EndpointReference targetEPR = new EndpointReference(url);
RPCServiceClient sender = new RPCServiceClient();
Options options = sender.getOptions();
options.setTimeOutInMilliSeconds(timeOutInMilliSeconds); //超时时间20s
options.setTo(targetEPR);
QName qName = new QName(nameSpace, method);
Object[] param = new Object[]{action, reqXml};
//这是针对返值类型的
Class<?>[] types = new Class[]{String.class};
Object[] response = sender.invokeBlocking(qName, param, types);
resXml = response[0].toString();
// System.out.println("响应报文:" + resXml);
} catch (AxisFault e) {
e.printStackTrace();
throw e;
}
return resXml;
}
public static void main(String[] args) {
String msg = "<?xml version='1.0' encoding='UTF-8'?>\n" +
"<loginParam>\n" +
"<cmsUrl>127.0.0.1</cmsUrl>\n" +
"<userName>sam6</userName>\n" +
"<passwd>12345</passwd>\n" +
"</loginParam>\n";
String url = "http://127.0.0.1:8092/services/ThirdBayonetService";
String nameSpace = "http://service.thirdBayonet.webservice.bms.hikvision.com";
String method = "initSystem";
String reqXml = "请求报文";
String resXml = sendWsMessage(url,nameSpace,method,returnMsg,timeOutInMilliSeconds);
System.out.println("响应报文:" + resXml);
} catch (Exception e) {
e.printStackTrace();
}
}
}
axis2服务端
axis2依赖
<!--axis2 begin -->
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-corba</artifactId>
<version>1.7.9</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-spring</artifactId>
<version>1.7.9</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-kernel</artifactId>
<version>${axis2.version}</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-ant-plugin</artifactId>
<version>1.7.9</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>${axis2.version}</version>
<exclusions>
<exclusion>
<groupId>javax-servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-jaxws</artifactId>
<version>${axis2.version}</version>
</dependency>
<!--axis2 end -->
services.xml
<?xml version="1.0" encoding="UTF-8" ?>
<serviceGroup>
<!--name:暴露的服务名 根据需求配置或者自定义
scope:默认即可
targetNamespace:命名空间 根据需求配置或者自定义-->
<!-- scope: 会话范围共有四种
1.application : 应用级别。生命周期和Axis2引擎生命周期相同。
2.soapsession : 使用addressing headers中的自定义引用属性来管理会话。
3.transportsession : 使用transport cookies来管理会话,生命周期和底层的transport相同。
4.request :生命周期很短,和请求处理周期相同。
-->
<service name="ThirdBayonetService" scope="application" targetNamespace="http://service.thirdBayonet.webservice.bms.hikvision.com" >
<!-- description元素来描述该服务 -->
<description>解析数据</description>
<!--命名空间 根据需求配置或者自定义-->
<!--<schema schemaNamespace="http://service.axis2.unvhis.com"/>-->
<schema schemaNamespace="http://service.thirdBayonet.webservice.bms.hikvision.com"/>
<!--实现类全路径-->
<parameter name="ServiceClass">com.uniview.Webservice.ThirdBayonetServiceImpl</parameter>
<operation name="insertVehicleInfo">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
</service>
</serviceGroup>
读取配置文件
@Slf4j
@Configuration
public class Axis2WebServiceConfiguration {
//服务访问前缀
public static final String URL_PATH = "/services/*";
//services.xml文件的位置
public static final String SERVICES_FILE_PATH = "WEB-INF/services/axis2/META-INF/services.xml";
//AXIS2参数key
public static final String AXIS2_REP_PATH = "axis2.repository.path";
@Bean
public ServletRegistrationBean axis2Servlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
servletRegistrationBean.setServlet(new AxisServlet());
servletRegistrationBean.addUrlMappings(URL_PATH);
String path;
if(SystemUtil.getOsInfo().isWindows()){
path = System.getProperty("user.dir") + "/config/WEB-INF";
} else {
path = "/usr/dafg/config/WEB-INF";
}
log.debug("path:{}", path);
servletRegistrationBean.addInitParameter(AXIS2_REP_PATH, path);
servletRegistrationBean.setLoadOnStartup(1);
return servletRegistrationBean;
}
}
服务端的实现
public interface ThirdBayonetService {
public String searchCrossingInfo(String cityName);
public String deleteCrossingInfo(String cityName);
public String searchVehicleInfo(String xml);
public String initSystem(String cityName);
public String insertPlateAlarmInfo(String cityName);
public String modifyPlateAlarmInfo(String cityName);
public String UndoPlateAlarmInfo(String cityName);
public String insertCrossingInfo(String cityName);
public String getRecentMessage(String cityName);
public String deletePlateAlarmInfo(String cityName);
public String insertVehicleInfo(String xml);
public String modifyCrossingInfo(String cityName);
}
@Slf4j
@Service
public class ThirdBayonetServiceImpl implements ThirdBayonetService {
@Override
public String searchCrossingInfo(String cityName) {
return null;
}
@Override
public String deleteCrossingInfo(String cityName) {
return null;
}
@Override
public String searchVehicleInfo(String xml) {
return null;
}
@Override
public String initSystem(String cityName) {
return null;
}
@Override
public String insertPlateAlarmInfo(String cityName) {
return null;
}
@Override
public String modifyPlateAlarmInfo(String cityName) {
return null;
}
@Override
public String UndoPlateAlarmInfo(String cityName) {
return null;
}
@Override
public String insertCrossingInfo(String cityName) {
return null;
}
@Override
public String getRecentMessage(String cityName) {
return null;
}
@Override
public String deletePlateAlarmInfo(String cityName) {
return null;
}
@Override
public String insertVehicleInfo(String xml) {
String response = "<?xml version='1.0' encoding='UTF-8'?>\n" +
"<root>\n" +
"<code>0</code>\n" +
"<message>插入信息成功</message>\n" +
"</root>\n";
return response;
}
@Override
public String modifyCrossingInfo(String cityName) {
return null;
}
}
查看生成的WSDL文件
http://ip:port/services/ThirdBayonetService?wsdl
WSDL 服务进行交互的基本元素:
Types(消息类型):数据类型定义的容器,它使用某种类型系统(如 XSD)。
Message(消息):通信数据的抽象类型化定义,它由一个或者多个 part 组成。
Part:消息参数
PortType(端口类型):特定端口类型的具体协议和数据格式规范。,它由一个或者多个 Operation组成。
Operation(操作):对服务所支持的操作进行抽象描述,WSDL定义了四种操作:
1.单向(one-way):端点接受信息;
3.要求-响应(solicit-response):端点发送消息,然后接受相关消息;
4.通知(notification[2] ):端点发送消息。
Binding:特定端口类型的具体协议和数据格式规范。
Port:定义为绑定和网络地址组合的单个端点。
Service:相关端口的集合,包括其关联的接口、操作、消息等。
<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions
targetNamespace="http://com.liuxiang.xfireDemo/HelloService"
xmlns:tns="http://com.liuxiang.xfireDemo/HelloService"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding"
xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="qualified" elementFormDefault="qualified"
targetNamespace="http://com.liuxiang.xfireDemo/HelloService">
<xsd:element name="sayHello">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1"
name="name" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sayHelloResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="0"
name="return" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sayHelloResponse">
<wsdl:part name="parameters" element="tns:sayHelloResponse" />
</wsdl:message>
<wsdl:message name="sayHelloRequest">
<wsdl:part name="parameters" element="tns:sayHello" />
</wsdl:message>
<wsdl:portType name="HelloServicePortType">
<wsdl:operation name="sayHello">
<wsdl:input name="sayHelloRequest"
message="tns:sayHelloRequest" />
<wsdl:output name="sayHelloResponse"
message="tns:sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloServiceHttpBinding"
type="tns:HelloServicePortType">
<wsdlsoap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="sayHelloRequest">
<wsdlsoap:body use="literal" />
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<wsdlsoap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloService">
<wsdl:port name="HelloServiceHttpPort"
binding="tns:HelloServiceHttpBinding">
<wsdlsoap:address
location="http://localhost:8080/xfire/services/HelloService" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
(一) definitions元素
所有的WSDL文档的根元素均是definitions元素。该元素封装了整个文档,同时通过其name提供了一个WSDL文档。除了提供一个命名空间(targetNamespace)外,该元素没有其他作用,故不作详细描述。
(二)types元素
WSDL采用了W3C XML模式内置类型作为其基本类型系统。types元素用作一个容器,用于定义XML模式内置类型中没有描述的各种数据类型(不太明白:XML模式内置类型中没有描述的各种数据类型)。当声明消息部分的有效时,消息定义使用了在types元素中定义的数据类型和元素。在本文的WSDL文档中的types定义:
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="qualified" elementFormDefault="qualified"
targetNamespace="http://com.liuxiang.xfireDemo/HelloService">
<xsd:element name="sayHello">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1"
name="name" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="sayHelloResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="0"
name="return" nillable="true" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
上面是数据定义部分,该部分定义了两个元素,一个是sayHello,一个是sayHelloResponse:
sayHello:定义了一个复杂类型,仅仅包含一个简单的字符串,将来用来描述操作的参入传入部分;
sayHelloResponse:定义了一个复杂类型,仅仅包含一个简单的字符串,将来用来描述操作的返回值;
这里sayHelloResponse是和sayHello相关的,sayHello相对于一个方法,里面的: type=”xsd:string”,name=”name”,是确定传入name的参数是String类型的,而sayHelloResponse中的 name=”return” type=”xsd:string” 是确定方法sayHello(String name)返回的类型是String类型的。
(三)import元素
import元素使得可以在当前的WSDL文档中使用其他WSDL文档中指定的命名空间中的定义元素。本例子中没有使用import元素。通常在用户希望模块化WSDL文档的时候,该功能是非常有效果的。
import的格式如下:
<wsdl:import namespace="http://xxx.xxx.xxx/xxx/xxx" location="http://xxx.xxx.xxx/xxx/xxx.wsdl"/>
必须有namespace属性和location属性:
1.namespace属性:值必须与正导入的WSDL文档中声明的targetNamespace相匹配;
2.location属性:必须指向一个实际的WSDL文档,并且该文档不能为空。
(四)message元素
message元素描述了Web服务使用消息的有效负载。message元素可以描述输出或者接受消息的有效负载;还可以描述SOAP文件头和错误detail元素的内容。定义message元素的方式取决于使用RPC样式还是文档样式的消息传递。在本文中的message元素的定义,本文档使用了采用文档样式的消息传递:
<wsdl:message name="sayHelloResponse">
<wsdl:part name="parameters" element="tns:sayHelloResponse" />
</wsdl:message>
<wsdl:message name="sayHelloRequest">
<wsdl:part name="parameters" element="tns:sayHello" />
</wsdl:message>
该部分是消息格式的抽象定义:定义了两个消息sayHelloResponse和sayHelloRequest:
1.sayHelloRequest:
sayHello操作的请求消息格式,由一个消息片断组成,名字为parameters,元素是我们前面定义的types中的元素;
2.sayHelloResponse:
sayHello操作的响应消息格式,由一个消息片断组成,名字为parameters,元素是我们前面定义的types中的元素;
如果采用RPC样式的消息传递,只需要将文档中的element元素修改为type即可
(五)portType元素
portType元素定义了Web服务的抽象接口。该接口有点类似Java的接口,都是定义了一个抽象类型和方法,没有定义实现。在WSDL中,portType元素是由binding和service元素来实现的,这两个元素用来说明Web服务实现使用的Internet协议、编码方案以及Internet地址。
一个portType中可以定义多个operation,一个operation可以看作是一个方法,本文中WSDL文档的定义:
<wsdl:portType name="HelloServicePortType">
<wsdl:operation name="sayHello">
<wsdl:input name="sayHelloRequest"
message="tns:sayHelloRequest" />
<wsdl:output name="sayHelloResponse"
message="tns:sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
portType定义了服务的调用模式的类型,这里包含一个操作sayHello方法,同时包含input和output表明该操作是一个请求/响应模式,请求消息是前面定义的sayHelloRequest,响应消息是前面定义的sayHelloResponse。input表示传递到Web服务的有效负载,output消息表示传递给客户的有效负载。
这里相当于抽象类中定义了一个抽象方法sayHello,而方法参数的定义和返回值的定义都是在types中设置的,方法名又是在message中定义有的。
(六)binding
binding元素将一个抽象portType映射到一组具体协议(SOAO和HTTP)、消息传递样式、编码样式。通常binding元素与协议专有的元素和在一起使用,本文中的例子:
<wsdl:binding name="HelloServiceHttpBinding"
type="tns:HelloServicePortType">
<wsdlsoap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="sayHelloRequest">
<wsdlsoap:body use="literal" />
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<wsdlsoap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
这部分将服务访问点的抽象定义与SOAP、HTTP绑定,描述如何通过SOAP/HTTP来访问按照前面描述的访问入口点类型部署的访问入口。
其中规定了在具体SOAP调用时,应当使用的soapAction是”xxx”,这个Action在WebService代码调用中是很重要的。具体的使用需要参考特定协议定义的元素。
(七)service元素和port元素
service元素包含一个或者多个port元素,其中每个port元素表示一个不同的Web服务。port元素将URL赋给一个特定的binding,甚至可以使两个或者多个port元素将不同的URL赋值给相同的binding。文档中的例子:
<wsdl:service name="HelloService">
<wsdl:port name="HelloServiceHttpPort"
binding="tns:HelloServiceHttpBinding">
<wsdlsoap:address
location="http://localhost:8080/xfire/services/HelloService" />
</wsdl:port>
</wsdl:service>
对于这个WSDL文档的学习,第一次看是感觉非常陌生的,而且里面元素又多,学习的话先是要了解外层结构代表的意义和作用,然后理解里面的元素的意义和作用,有些元素作用不大,有些元素又是很关联的,有些元素是比较重要的。
WSDL图解:
2. cxf实现方式
2.1 CXF调取客户端
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class clientClass {
public static void client() {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://127.0.0.1:8080/webService/startService?wsdl");
Object[] objects = new Object[0];
try {
// invoke("方法名",参数1,参数2,参数3....);
objects = client.invoke("getStr", "ldw", "成功");
System.out.println("返回数据:" + objects[0]);
client.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
client();
}
}
2.2 CXF服务端
2.2.1 cxf依赖
<!--引入CXF:用于开发WebService-->
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<!--排除javax.validation,否则启动报错-->
<exclusions>
<exclusion>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</exclusion>
</exclusions>
<version>3.3.6</version>
</dependency>
2.2.2 读取配置文件
import WebserviceTest.cxf.Webservice.WebServiceStart;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import javax.xml.ws.Endpoint;
@Configuration
public class CxfConfig {
@Resource
private Bus bus; //private SpringBus bus;
@Autowired
private WebServiceStart webServiceStart;
// 配置CXF服务发布,默认服务是在host:port/services/发布地址
// 如果想改变它的url,在application.properties中修改:cxf.path = /自定义
@Bean
public Endpoint another_endpoint() {
EndpointImpl endpoint = new EndpointImpl(bus, webServiceStart);
endpoint.publish("/startService"); //发布地址
return endpoint;
}
/**
* 如果要发布多个服务,可以在这里面添加,只需要改变发布地址即可。
*/
// @Bean
// public Endpoint another_endpoint() {
// EndpointImpl endpoint = new EndpointImpl(bus, webServiceStart);
// endpoint.publish("/startService"); //发布地址
// return endpoint;
// }
}
2.2.3 cxf实现服务端
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(name = "webServiceStart", targetNamespace="http://server.webservice.ldw.com/")
public interface WebServiceStart {
@WebMethod(action = "http://service.demo.example.com/getStr",//定义一个soapAction,用于找到这个方法以执行
operationName="getStr"//用于定义暴漏在外得方法名称,等价于PortType 中得 operation name=“”
)
@WebResult(name = "returnStr")//定义返回值得名称
public String getStr(@WebParam(name="input1",targetNamespace="http://server.webservice.ldw.com/" )String str,
@WebParam(name="arg1",targetNamespace="http://server.webservice.ldw.com/")String str2);
}
/*
* @WebService(name = “webServiceStart”, targetNamespace=“http://server.webservice.ldw.com/”)
* @WebService:定义webService接口
* 属性:name = “webServiceStart”:用于指定暴露在外的接口名称,对应WSDL文件中的<wsdl:protType name="webServiceStart">
* 属性:targetNamespace=“http://server.webservice.ldw.com/” :定义目标的命名空间,通常设置为包名的倒叙
*
* @WebMethod: 用于指定该方法为webService 方法
* 属性:
* action : 用于定义soapAction,用于找到该方法
* operationName : 用于定义暴漏在外得方法名称,等价于PortType 中得 operation name=""
*
* @WebResult:用于定义方法返回信息,
* 属性:
* name : 用于定义返回值的名称
*
* @WebParam:用来声明参数
* 属性:
* name:暴漏在外的参数名称,即为调用者用到的参数名
* targetNamespace:指定命名空间
* mode:用于定义参数的类型,输入参数、
import org.springframework.stereotype.Component;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
/**
* serviceName: 对外发布的服务名称,对应service name=""
* targetNamespace://指定你想要的名称空间,通常使用使用包名反转
* endpointInterface:指定接口包全名
*/
@Component
@WebService(serviceName="WebServiceStartWS",
targetNamespace="http://server.webservice.ldw.com/",
endpointInterface = "com.ldw.webservice.server.WebServiceStart")
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)//用于指定soap协议版本,必须放在实现类上
public class WebServiceStartImpl implements WebServiceStart {
@Override
public String getStr(String str, String str2) {
return str + str2;
}
}
/*
* @BindingType
* 用于指定soap的协议,特别注意:该注解必须放在实现类上,才可生效
*
* */
@Webservice实现服务端
public interface WeatherInterface {
public String queryWeather(String cityName);
public String searchCrossingInfo(String cityName);
public String deleteCrossingInfo(String cityName);
public String searchVehicleInfo(String xml);
public String initSystem(String cityName);
public String insertPlateAlarmInfo(String cityName);
public String modifyPlateAlarmInfo(String cityName);
public String UndoPlateAlarmInfo(String cityName);
public String insertCrossingInfo(String cityName);
public String getRecentMessage(String cityName);
public String deletePlateAlarmInfo(String cityName);
public String insertVehicleInfo(String xml);
public String modifyCrossingInfo(String cityName);
}
import javax.jws.WebService;
@WebService //@WebService表示该类是一个服务类,需要发布其中的public的方法
public class WeatherInterfaceImpl implements WeatherInterface {
@Override
public String queryWeather(String cityName) {
System.out.println("获取城市名"+cityName);
String weather="暴雨";
return weather;
}
@Override
public String searchCrossingInfo(String cityName) {
return null;
}
@Override
public String deleteCrossingInfo(String cityName) {
return null;
}
@Override
public String searchVehicleInfo(String xml) {
return null;
}
@Override
public String initSystem(String cityName) {
return null;
}
@Override
public String insertPlateAlarmInfo(String cityName) {
return null;
}
@Override
public String modifyPlateAlarmInfo(String cityName) {
return null;
}
@Override
public String UndoPlateAlarmInfo(String cityName) {
return null;
}
@Override
public String insertCrossingInfo(String cityName) {
return null;
}
@Override
public String getRecentMessage(String cityName) {
return null;
}
@Override
public String deletePlateAlarmInfo(String cityName) {
return null;
}
@Override
public String insertVehicleInfo(String xml) {
String response = "<?xml version='1.0' encoding='UTF-8'?>\n" +
"<root>\n" +
"<code>0</code>\n" +
"<message>信息成功</message>\n" +
"</root>\n";
return response;
}
@Override
public String modifyCrossingInfo(String cityName) {
return null;
}
}
import javax.xml.ws.Endpoint;
public class WeatherServer {
public static void main(String[] args) {
Endpoint.publish("http://127.0.0.1:12345/weather", new WeatherInterfaceImpl());
}
}