`
huainansto
  • 浏览: 27570 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

web service

    博客分类:
  • CXF
阅读更多

首先定义个接口Hello.java

 

package ch1;

public interface Hello {

	public String sayHello();
}

 然后实现这个接口, 实现类为:HelloImpl.java

 

package ch1;

public class HelloImpl implements Hello{

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

}

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

 package ch1;

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

<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类创建实例, 然后调用实例的方法。
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

  还可以添加拦截器
  
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
   
2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics