WebService归纳总结

WebService 技术在日常的开发中几乎不会用到,但也不是没有例外。这几天公司就有这个需求,需要对外使用WebService提供接口。

  1、WebService 是一种跨平台、跨语言的远程调用技术。

  2、XML、soap(简单对象访问协议(Simple Object Access Protocol))和WSDL(Web Service描述语言)就是构成WebService平台的三大技术。

    a、WebService 使用xml 封装数据、使用http协议传输数据。

    b、WebService 在使用http协议传输与接收数据是,使用soap简单对象访问协议规定的格式添加特定的xml格式消息头,以说明http消息的内容格式。

    c、WebService 通过WSDL文件来说明自己可以调用的外部服务。服务提供者可以通过注册到UDDI服务器或直接告诉给客户端调用者来暴露它的WSDL文件地址。

Java WebService 规范

  Java WebService 有三种规范

    1、JAX-WS(Java API For XML-WebService)早期的基于SOAP 的JAVA 的Web 服务规范JAX-RPC(Java API For XML-RemoteProcedure Call)目前已经被JAX-WS 规范取代。从java5开始支持JAX-WS2.0版本,Jdk1.6.0_13以后的版本支持2.1版本,jdk1.7支持2.2版本。

    2、JAXM(JAVA API For XML Message)主要定义了包含了发送和接收消息所需的API,JAXM&SAAJ 与JAX-WS 都是基于SOAP 的Web 服务,相比之下JAXM&SAAJ 暴漏了SOAP更多的底层细节,编码比较麻烦,而JAX-WS 更加抽象,隐藏了更多的细节,更加面向对象,实现起来你基本上不需要关心SOAP 的任何细节

    3、JAX-RS 是JAVA 针对REST(Representation State Transfer)风格制定的一套Web 服务规范,由于推出的较晚,该规范(JSR 311,目前JAX-RS 的版本为1.0)并未随JDK1.6 一起发行。

Java Webservice 框架

  1、jws 是java语言对WebService服务的一种实现,用来开发和发布服务。而从服务本身的角度来看JWS服务是没有语言界限的。但是Java语言为Java开发者提供便捷发布和调用WebService服务的一种途径。

  2、Axis2是Apache下的一个重量级WebService框架

  3、XFire是一个高性能的WebService框架,在Java6之前,它的知名度甚至超过了Apache的Axis2,XFire的优点是开发方便,与现有的Web整合很好,可以融为一体,并且开发也很方便。但是对Java之外的语言,没有提供相关的代码工具。

  4、CXF是Apache旗下一个重磅的SOA简易框架,它实现了ESB(企业服务总线)。CXF来自于XFire项目,经过改造后形成的。CXF不但是一个优秀的Web Services / SOAP / WSDL 引擎,也是一个不错的ESB总线。

Java CXF 框架服务端整合springMVC 

  1、pom文件中引入

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>apache-cxf</artifactId>
<version>3.2.6</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>cxf-services-wsn-api</artifactId>
<groupId>org.apache.cxf.services.wsn</groupId>
</exclusion>
<exclusion>
<artifactId>cxf-services-wsn-core</artifactId>
<groupId>org.apache.cxf.services.wsn</groupId>
</exclusion>
<exclusion>
<artifactId>cxf-services-ws-discovery-api</artifactId>
<groupId>org.apache.cxf.services.ws-discovery</groupId>
</exclusion>
<exclusion>
<artifactId>cxf-services-ws-discovery-service</artifactId>
<groupId>org.apache.cxf.services.ws-discovery</groupId>
</exclusion>
</exclusions>
</dependency>

  2、在web.xml 中配置
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

  3、在web.xml同级目录下添加 cxf-servlet.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">

<!-- <import resource="classpath:META-INF/cxf/cxf.xml" />-->
<!-- <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />-->
<!-- //implementor:实现类的地址 address:调用时的地址-->
<jaxws:endpoint id="personQueryService"
implementor="service.impl.WebServiceImpl" address="/msgservice" />

</beans>
  
  4、编写接口 添加 @javax.jws.WebService 注解,并设置targetNamespace 属性、设置参数注解@WebParam(如果不设置会出现方法参数不显示在wsdl 文件中)
@javax.jws.WebService(targetNamespace="http://ucp.xxx.com")
public interface WebService {
String getValue(@WebParam(name = "name")String name);

int getAge(@WebParam(name = "age")int age);
}

  5、编写实现类 同上添加@javax.jws.WebService 注解,并设置targetNamespace 属性(一定要相同)。@WebParam 可写可不写。
@Override
public String getValue(@WebParam(name = "name") String name) {

return "你好"+name+"欢迎使用WebService";
}

@Override
public int getAge(int age) {
return age;
}

6、请求地址使用 http://127.0.0.1:8080/services/msgservice?wsdl 也就是web.xml 中定义的前缀 services 再加上在 cxf-servlet.xml 中定义的address 。


客户端大体代码:如果需要扩展请自行扩展,但万变不离其中,就是工厂,客户端,请求。
public static void main(String[] args) {
//创建连接工厂
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
//创建连接客户端
Client client = clientFactory.createClient("http://localhost:8082/services/msgservice?wsdl");
try{
Object[] result = null;
Object[] args1 = new Object[7];
args1[0] = 12;
//请求
result= client.invoke("getAge", args1);
if(result!=null && result.length > 0){
if(result[0]!= null){
String str = result[0].toString();
System.out.println(str);

}else{
}
}else{
}
}catch (Exception e) {
e.printStackTrace();
}

}

cxf客户端报错:faultCode=INVALID_WSDL: Expected element '{http://schemas.xmlsoap.org/wsdl/}

原因是请求的地址后没有加 ?wsdl .


  
原文地址:https://www.cnblogs.com/hxz-nl/p/13802225.html