加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

XFire入门实例

发布时间:2020-12-17 01:27:46 所属栏目:安全 来源:网络整理
导读:!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-f

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} /* Style Definitions */ p.MsoNormal,li.MsoNormal,div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} a:link,span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited,span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:1099569772; mso-list-type:hybrid; mso-list-template-ids:2017498380 -427549864 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-start-at:2; mso-level-number-format:japanese-counting; mso-level-text:%1、; mso-level-tab-stop:54.0pt; mso-level-number-position:left; margin-left:54.0pt; text-indent:-36.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->

理论知识看相关资料,在此就不复述,直接进入XFire 开发实践。

一、下载 Xfire ,目前已更新为 CXF ,例子程序用的是 XFire1.2.6

二、?? 添加 xfire-all-1.2.6.jar 及其依赖包到类路径,依赖包在 lib 文件夹中

三、?? 开发 webservice 服务端 HelloWorldService

1 、服务接口:

package hellows;

//Generated by MyEclipse

?

public interface IHelloWorld {

???

??? ????? public String example(String message);

???

}

2 、服务实现类

package hellows;

//Generated by MyEclipse

?

public class HelloWorldImpl implements IHelloWorld {

???

??? public String example(String message) {

?????? System. out .println( this );

?????? return " 你好 , 这是我的第一个 Web Service, 你输入的消息是 :" +

?????? message;

??? }

}

3 Service.xml

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

< beans xmlns = "http://xfire.codehaus.org/config/1.0" >

?

??? < service >

?????? < name > HelloWorld </ name >

?????? < serviceClass > hellows.IHelloWorld </ serviceClass >

?????? < implementationClass > hellows.HelloWorldImpl </ implementationClass >

?????? < style > wrapped </ style >

?????? < use > literal </ use >

?????? < scope > application </ scope >

??? </ service ></ beans >

4 Web.xml

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

< web-app xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" version = "2.5" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee?? http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >

? < servlet >

??? < servlet-name > XFireServlet </ servlet-name >

??? < servlet-class > org.codehaus.xfire.transport.http.XFireConfigurableServlet </ servlet-class >

??? < load-on-startup > 0 </ load-on-startup >

? </ servlet >

? < servlet-mapping >

??? < servlet-name > XFireServlet </ servlet-name >

??? < url-pattern > /services/* </ url-pattern >

? </ servlet-mapping >

? < welcome-file-list >

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

? </ welcome-file-list >

</ web-app >

??? 部署到web 容器,如tomcat, 启动web 容器,在浏览器中输入 http://localhost:8080/HelloWorldService/services/HelloWorld wsd l 即可看到相应的wsdl 文件,其中8080web 容器访问接口。

四、开发 webservice 客户端 HelloWorldClient ,使用服务

使用 webService 服务有几种方式,分别实现如下:

1

import java.net.MalformedURLException;

import javax.wsdl.Service;

import org.codehaus.xfire.XFireFactory;

import org.codehaus.xfire.client.XFireProxyFactory;

import org.codehaus.xfire.service.binding.ObjectServiceFactory;

public class POJOClient {

??? public static void main(String[] args) {

???

?????? org.codehaus.xfire.service.Service srvcModel = new ObjectServiceFactory().create(IHelloWorld.class);

?????? XFireProxyFactory factory = new XFireProxyFactory(

?????? ? XFireFactory.newInstance().getXFire());

?????? String IHelloWorldWorldURL = "http://localhost:8080/HelloWorldService/services/HelloWorld";

?????? IHelloWorld srvc;

?????? try {

?????????? srvc = ( IHelloWorld) factory.create(srvcModel,

?????????? ?????????????????? ??????????????? IHelloWorldWorldURL);

?????????? System.out.println(srvc.example("Robin"));

?????? } catch (MalformedURLException e) {

?????????? // TODO Auto-generated catch block

?????????? e.printStackTrace();

?????? }

??? }

}

此方式需要知道服务接口,因此,为了使用服务必须在客户端创建IHelloWorld 接口

?

public interface IHelloWorld {

???

??? public String example(String message);

???

}

2

import java.net.MalformedURLException;

import java.net.URL;

import org.codehaus.xfire.client.Client;

?

public class HelloWorldClient {

??? public static void main(String[] args) throws MalformedURLException,Exception {

?????? Client client = new Client(new

????????????? URL("http://localhost:8080/HelloWorldService/services/HelloWorld?wsdl"));

????????????? Object[] results = client.invoke("example",new Object[] {"Juliet"});

????????????? System.out.println((String) results[0]);

??? }

}

3 、根据wsdl 文件生成相应的代码,再使用,可以采用MyEclipse 帮我们自动生成,具体操作参见相关资料。

生成完成之后,我们就可以像使用本地类一样使用webservice 服务

package hellows;

?

public class Test {

??? public static void main(String[] args) {

?????? // TODO Auto-generated method stub

?????? HelloWorldClient hw = new HelloWorldClient();

?????? HelloWorldPortType hwp = hw.getHelloWorldHttpPort();

?????? System. out .println(hwp.example( " 游传聪 " ));

??? }

}

五、 JDK1.6 自带 webservice 实现

Server:

服务类

package server;

import javax.jws.WebMethod;

import javax.jws.WebService;

import javax.jws.soap.SOAPBinding;

// 定义 Web 服务类和方法

@WebService(targetNamespace = "http://jdk.study.hermit.org/client")

@SOAPBinding(style = SOAPBinding.Style.RPC)

public class Hello {

@WebMethod

public String sayHello(String name) {

return "hello:" + name;

}

}

启动服务

package server;

?

import javax.xml.ws.Endpoint;

public class StartService {

public static void main(String[] args) {

Endpoint.publish ( "http://localhost:8081/HelloService" , new Hello());

}

}

使用服务

package client;

?

import java.net.MalformedURLException;

import java.net.URL;

import org.codehaus.xfire.XFireFactory;

import org.codehaus.xfire.client.Client;

import org.codehaus.xfire.client.XFireProxyFactory;

import org.codehaus.xfire.service.binding.ObjectServiceFactory;

?

public class HelloClient {

??? public static void main(String[] args) throws MalformedURLException,Exception {

?????? Client client = new Client(new

????????????? URL("http://localhost:8081/HelloService/HelloService?wsdl"));

????????????? Object[] results = client.invoke("sayHello",new Object[] {" 游传聪 "});

????????????? System.out.println((String) results[0]);

?????? ?

??? }

}

运行启动服务的程序StartService ,即可发布服务,不需要其他web 容器的支持,实际上自带有jetty 服务器,发布服务要确保端口没被占用。(相关资源可以在我的资源中找到)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读