为了账号安全,请及时绑定邮箱和手机立即绑定

Java :WebService&CXF使用

标签:
Java

WebService

• WebService通过HTTP POST方式接受客户的请求

• WebService与客户端之间一般使用SOAP协议传输XML数据

• 它本身就是为了跨平台或跨语言而设计的

网络上提供的服务:

http://webxml.com.cn/



XML 版 HTTP

SOAP 简单对象访问协议

SOAP作为一个基于XML语言的协议用于在网上传输数据。

 SOAP = 在HTTP的基础上+XML数据。

 SOAP是基于HTTP的。

 SOAP的组成如下:

 Envelope – 必须的部分。以XML的根元素出现。

 Headers – 可选的。

 Body – 必须的。在body部分,包含要执行的服务器的方法。和发送到服务器的数据。


WSDL web服务描述语言

就是一个文档 用于描述当前服务的一些信息

服务名称 service 标签的name

服务的发布地址  address 标签的location

服务提供懂得方法 operatioin 标签的name

方法的参数类型 

方法的返回值类型


发布一个WebService服务

第一步:创建一个Java项目

第二步:创建一个类,加入Webservice注解

第三步:提供一个方法sayHello

第四步:在main方法中调用jdk提供的发布服务的方法

第五步:访问服务的wsdl文档(服务的发布地址+?wsdl)http://172.29.20.5:8080/hello?wsdl

package webservice;

import javax.jws.WebService;

import javax.xml.ws.Endpoint;

@WebService

public class HolleWebService {

public String sayHello(String name,int i){

System.out.println("服务端的sayHello方法被调用了。。。。");

return "helle" + name;

}

public static void main(String[] args) {

String address = "http://172.29.20.5:8080/hello";

Object implementor = new HolleWebService();

Endpoint.publish(address, implementor);

}

}


jdk中wsimport命令使用

作用:解析wsdl文件,生成客户端本地代码

 

客户端调用

使用wsimport命令解析wsdl文件生成本地代码

通过本地代码创建一个代理对象

通过代理对象实现远程调用

package webservice;


public class App {

public static void main(String[] args) {

HolleWebServiceService holleWebServiceService = new HolleWebServiceService();

HolleWebService proxy = holleWebServiceService.getHolleWebServicePort();

String sayHello = proxy.sayHello("stevezong", 1);

System.out.println(sayHello);

}

}




CXF


 支持多种协议:

• SOAP1.1,1.2

• XML/HTTP

• CORBA(Common Object Request Broker Architecture公共对象请求代理体系结构,早期语言使用的WS。C,c++,C#) 

• 并可以与Spring进行快速无缝的整合

• 灵活的部署:可以运行在Tomcat,Jboss,Jetty(内置),IBMWS,BeaWL上面。

CXF 服务端 开发 spring + cxf

第一步:创建动态web项目

第二步:导入CXF相关jar包

第三步:在web.xml中配置CXF框架提供的一个Servlet

第四步:在类路径下提供cxf.xml

第五步:开发一个接口和实现类

第六步:在cxf.xml中注册服务

2

maven

<cxf.version>3.0.1</cxf.version>

<!-- cxf -->

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-rt-frontend-jaxws</artifactId>

<version>${cxf.version}</version>

</dependency>

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-rt-transports-http</artifactId>

<version>${cxf.version}</version>

</dependency>

<!-- Jetty is needed if you're are not using the CXFServlet -->

<dependency>

<groupId>org.apache.cxf</groupId>

<artifactId>cxf-rt-transports-http-jetty</artifactId>

<version>${cxf.version}</version>

</dependency>




3

wel

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5">

<display-name>cxf</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>


<!-- <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 

<init-param> <param-name>config-location</param-name> <param-value>classpath:spring-cxf.xml</param-value> 

</init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> 

<servlet-name>cxf</servlet-name> <url-pattern>/service/*.do</url-pattern> 

</servlet-mapping> -->

<!-- 配置CXF框架提供的Servlet -->

<servlet>

<servlet-name>cxf</servlet-name>

<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

<!-- 通过初始化参数指定CXF框架的配置文件位置 -->

<init-param>

<param-name>config-location</param-name>

<param-value>classpath:cxf.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>cxf</servlet-name>

<url-pattern>/service/*</url-pattern>

</servlet-mapping>



</web-app>



3

cxf

<?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"

xmlns:soap="http://cxf.apache.org/bindings/soap"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans.xsd

http://cxf.apache.org/bindings/soap 

http://cxf.apache.org/schemas/configuration/soap.xsd

http://cxf.apache.org/jaxws 

http://cxf.apache.org/schemas/jaxws.xsd">

<!-- 引入CXF Bean定义如下,早期的版本中使用 -->

<import resource="classpath:META-INF/cxf/cxf.xml" />

<!-- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

--><import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<bean id="helloService" class="com.stevezong.cxf.HelloServiceImpl"></bean>

<jaxws:server id="myService" address="/cfxService">

<jaxws:serviceBean>

<ref bean="helloService"/>

</jaxws:serviceBean>

</jaxws:server>

</beans>



4

java

package com.stevezong.cxf;


import javax.jws.WebService;


@WebService

public interface HelloService {

public String sayHello(String name);

}


package com.stevezong.cxf;


public class HelloServiceImpl implements HelloService{


public String sayHello(String name) {

System.out.println("cxf的sayHello");

return name + " name ";


}






CXF 客户端 


方式一:使用jdk提供的wsimport命令生成本地代码完成调用


方式二:使用CXF提供的方式(重点)

第一步:创建Java项目并导入CXF相关jar包

第二步:使用wsimport或者CXF提供wsdl2java生成本地代码,只需要生成接口文件

第三步:将接口文件复制到项目中

第四步:提供spring配置文件,注册客户端代理对象

第五步:读取spring配置文件,创建spring工厂,从工厂中获取代理对象,实现远程调用

1

maven

导包

2

C:\Program Files\Java\jdk1.8.0_111\bin>wsimport.exe -s \test http://127.0.0.1:80

80/cxf/service/cfxService?wsdl

正在解析 WSDL...

正在生成代码...

正在编译代码...


3

package com.stevezong.cxf;


import javax.jws.WebService;


@WebService

public interface HelloService {

public String sayHello(String name);

}

4

<?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"

xmlns:soap="http://cxf.apache.org/bindings/soap"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans.xsd

http://cxf.apache.org/bindings/soap 

http://cxf.apache.org/schemas/configuration/soap.xsd

http://cxf.apache.org/jaxws 

http://cxf.apache.org/schemas/jaxws.xsd">

<!-- 引入CXF Bean定义如下,早期的版本中使用 -->

<import resource="classpath:META-INF/cxf/cxf.xml" />

<!-- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

--><import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<jaxws:client id="myClient" address="http://127.0.0.1:8080/cxf/service/cfxService" serviceClass="cxf.HelloService"></jaxws:client>

</beans>


5

package cxf;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Test2 {

public static void main(String[] args) {

ApplicationContext ctx  = new ClassPathXmlApplicationContext("cxf2.xml");

HelloService proxy =  (HelloService) ctx.getBean("myClient");

System.out.println(proxy.sayHello("stevezong"));

}

}


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
移动开发工程师
手记
粉丝
18
获赞与收藏
134

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消