java创建服务端类跟客户端类_Java创建WebService服务端及客户端

目的:实现发布WebService服务并使用该服务

WebService服务端 构架图

0818b9ca8b590ca3270a3433284dd417.png

1、创建出项目时 添加 Xfire Web Service

项目属性 -> MyEclipse ->Add Xfire Web Service

0818b9ca8b590ca3270a3433284dd417.png

这步可以默认 -> 下一步

这时需要 选中 XFire 1.2 HTTP Client Libraries

0818b9ca8b590ca3270a3433284dd417.png

2、定义接口与实现类

package com.accp.Inter;

/**

* 定义接口

* @author jaylian

* 2009年3月27日13:26:51

*/

public interface Ihello {

public String hello();

}

package com.accp.Impl;

/**

* 实现类

* @author jaylian

* 2009年3月27日13:29:32

*/

import com.accp.Inter.Ihello;

public class ImplHello implements Ihello {

public String hello() {

return "Hello Accp !!";

}

}

3、services.xml 中的配置

ACCP

com.accp.Inter.Ihello

com.accp.Impl.ImplHello

Available Services:

ACCP [wsdl]

-

<

wsdl:definitionstargetNamespace

="

http://Inter.accp.com

"

xmlns:tns

="

http://Inter.accp.com

"

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://Inter.accp.com

">

-

<

xsd:elementname

="

hello

">

<

xsd:complexType

/>

xsd:element

>

-

<

xsd:elementname

="

helloResponse

">

-

<

xsd:complexType

>

-

<

xsd:sequence

>

<

xsd:element

maxOccurs

="

1

"

minOccurs

="

1

"

name

="

out

"

nillable

="

true

"

type

="

xsd:string

"/>

xsd:sequence

>

xsd:complexType

>

xsd:element

>

xsd:schema

>

wsdl:types

>

-

<

wsdl:messagename

="

helloRequest

">

<

wsdl:part

name

="

parameters

"

element

="

tns:hello

"/>

wsdl:message

>

-

<

wsdl:messagename

="

helloResponse

">

<

wsdl:part

name

="

parameters

"

element

="

tns:helloResponse

"/>

wsdl:message

>

-

<

wsdl:portTypename

="

ACCPPortType

">

-

<

wsdl:operationname

="

hello

">

<

wsdl:input

name

="

helloRequest

"

message

="

tns:helloRequest

"/>

<

wsdl:output

name

="

helloResponse

"

message

="

tns:helloResponse

"/>

wsdl:operation

>

wsdl:portType

>

-

<

wsdl:bindingname

="

ACCPHttpBinding

"

type

="

tns:ACCPPortType

">

<

wsdlsoap:binding

style

="

document

"

transport

="

http://schemas.xmlsoap.org/soap/http

"/>

-

<

wsdl:operationname

="

hello

">

<

wsdlsoap:operation

soapAction

=""/>

-

<

wsdl:inputname

="

helloRequest

">

<

wsdlsoap:body

use

="

literal

"/>

wsdl:input

>

-

<

wsdl:outputname

="

helloResponse

">

<

wsdlsoap:body

use

="

literal

"/>

wsdl:output

>

wsdl:operation

>

wsdl:binding

>

-

<

wsdl:servicename

="

ACCP

">

-

<

wsdl:portname

="

ACCPHttpPort

"

binding

="

tns:ACCPHttpBinding

">

<

wsdlsoap:address

location

="

http://localhost:8080/WebService/services/ACCP

"/>

wsdl:port

>

wsdl:service

>

wsdl:definitions

>

这表示你的WebService发布成了。

6、你还可以再MyEclipse中测试:

0818b9ca8b590ca3270a3433284dd417.png

最后又输出: out(string): Hello Accp !!

------------------------------------------------------------------

创建WebService客户端

1、New Web Services Project

project Name : WebClient

注意:Framwork:选中中间的 XFire

-> 下一步  默认即可

-> 下一步  选中第二项 XFire 1.2 HTTP Client Libraries

-> 完成

2、创建客户端接口及类

package com.accp.Inter;

/**

* 客户端接口

* @author jaylian

* 2009年3月27日14:03:06

*/

public interface Ihello {

public String hello();

}

package com.accp.Impl;

import java.net.MalformedURLException;

import org.codehaus.xfire.XFire;

import org.codehaus.xfire.XFireFactory;

import org.codehaus.xfire.client.XFireProxyFactory;

import org.codehaus.xfire.service.Service;

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

import com.accp.Inter.Ihello;

public class HelloWord {

public String getHello() {

Service serviceModel = new ObjectServiceFactory()

.create(Ihello.class);

XFire xfire = XFireFactory.newInstance().getXFire();

XFireProxyFactory factory = new XFireProxyFactory(xfire);

Ihello client = null;

try {

client = (Ihello) factory.create(serviceModel, serviceUrl);

} catch (MalformedURLException e) {

}

String result = "";

try {

result = client.hello();

} catch (Exception e) {

e.printStackTrace();

}

System.out.println(result);

return result;

}

public static void main(String[] args) {

new HelloWord().getHello();

}

}

这里使用了一个Java类,测试获取的webservice服务信息控制台输出:-------------Hello Accp !!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值