web service

[color=green][size=medium]首先定义个接口Hello.java



Java代码
package ch1;

public interface Hello {

public String sayHello();
}

package ch1;

public interface Hello {

public String sayHello();
} 然后实现这个接口, 实现类为:HelloImpl.java



Java代码
package ch1;

public class HelloImpl implements Hello{

public String sayHello() {
return "hello";
}

}

package ch1;

public class HelloImpl implements Hello{

public String sayHello() {
return "hello";
}

} 通过CXF把这个web service发表出去:

package ch1;

Java代码
import org.apache.cxf.frontend.ServerFactoryBean;

public class Test {

public static void main(String[] args)
{
HelloImpl impl = new HelloImpl();
ServerFactoryBean factory = new ServerFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("http://localhost:8080/Hello");
factory.setServiceBean(impl);factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.create();
}
}

import org.apache.cxf.frontend.ServerFactoryBean;

public class Test {

public static void main(String[] args)
{
HelloImpl impl = new HelloImpl();
ServerFactoryBean factory = new ServerFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("http://localhost:8080/Hello");
factory.setServiceBean(impl);factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.create();
}
} 访问这个web service的wsdl文件 http://localhost:8080/Hello?wsdl

Html代码
<wsdl:definitions name="Hello" targetNamespace="http://ch1/">
<wsdl:types>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://ch1/">
<xsd:element name="sayHello" type="tns:sayHello"/>
<xsd:complexType name="sayHello">
<xsd:sequence/>
</xsd:complexType>
<xsd:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xsd:complexType name="sayHelloResponse">
<xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloPortType">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloSoapBinding" type="tns:HelloPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Hello">
<wsdl:port binding="tns:HelloSoapBinding" name="HelloPort">
<soap:address location="http://localhost:8080/Hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

<wsdl:definitions name="Hello" targetNamespace="http://ch1/">
<wsdl:types>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://ch1/">
<xsd:element name="sayHello" type="tns:sayHello"/>
<xsd:complexType name="sayHello">
<xsd:sequence/>
</xsd:complexType>
<xsd:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xsd:complexType name="sayHelloResponse">
<xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloPortType">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloSoapBinding" type="tns:HelloPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Hello">
<wsdl:port binding="tns:HelloSoapBinding" name="HelloPort">
<soap:address location="http://localhost:8080/Hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
web service的服务器端把这个wsdl暴露出来, 客户端可以根据这个wsdl来访问客户端。
客户端通过ClientProxyFactoryBean类创建实例, 然后调用实例的方法。
Java代码
public class TestClient
{

public static void main(String[] args)
{
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("http://localhost:8080/Hello");
Hello hello = (Hello) factory.create();
System.out.println("invoke sayHello()...");
hello.sayHello();
System.exit(0);
}
}

public class TestClient
{

public static void main(String[] args)
{
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(Hello.class);
factory.setAddress("http://localhost:8080/Hello");
Hello hello = (Hello) factory.create();
System.out.println("invoke sayHello()...");
hello.sayHello();
System.exit(0);
}
} 运行这个java文件可以在客户端看到结果:
invoke sayHello()...
hello


这样我们就可以访问这个发表出去的web service了


也可以在eclipse 的 run->Launch the web service Explorer 来观察已经发表的web servic


还可以添加拦截器

Java代码
factory.getInInterceptors().add(new LoggingInInterceptor());

factory.getInInterceptors().add(new LoggingInInterceptor());Java代码
factory.getOutInterceptors().add(new LoggingOutInterceptor());

factory.getOutInterceptors().add(new LoggingOutInterceptor()); [/size][/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值