微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebService系列中CXF方式(二)

1.设置构建

<dependency>

    <groupId>org.apache.ws.xmlschema</groupId>

    <artifactId>xmlschema-core</artifactId>

    <version>2.0.3</version>

</dependency>

<dependency>

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

    <artifactId>cxf</artifactId>

    <version>2.6.2</version>

</dependency>

<dependency>

    <groupId>org.apache.neethi</groupId>

    <artifactId>neethi</artifactId>

    <version>3.0.2</version>

</dependency>

<dependency>

    <groupId>wsdl4j</groupId>

    <artifactId>wsdl4j</artifactId>

    <version>1.6.2</version>

</dependency>

<dependency>

    <groupId>org.apache.geronimo.specs</groupId>

    <artifactId>geronimo-jaxws_2.2_spec</artifactId>

    <version>1.1</version>

</dependency>

<dependency>

    <groupId>org.apache.geronimo.specs</groupId>

    <artifactId>geronimo-ws-Metadata_2.0_spec</artifactId>

    <version>1.1.3</version>

</dependency>       

2.编写服务

@WebService(name = "DataService",targetNamespace = DataServiceConstants.TARGET_NAMESPACE)

@WebServiceClient(name = "DataServerService",targetNamespace = DataServiceConstants.TARGET_NAMESPACE)

publicinterface DataService {

    @WebMethod(operationName = "GetTopGroup",action = DataServiceConstants.SOAP_ACTION_PREFIX

           + "GetTopGroup")

    @WebResult(name = "GetTopGroupResult",partName = "GetTopGroupResult",targetNamespace = DataServiceConstants.TARGET_NAMESPACE)

    String getTopGroup(

           @WebParam(name = "account",targetNamespace = DataServiceConstants.TARGET_NAMESPACE) String account,

           @WebParam(name = "password",targetNamespace = DataServiceConstants.TARGET_NAMESPACE) String password);

}

 

2.1:确认参数在xml文件名称正确,你需要使用,因为java接口在.class文件中不存储参数名称,所以@WebParam注解是必须的,因此如果你不使用这个注解,参数将被命名为arg0 :

@WebParam(name = "account",targetNamespace = DataServiceConstants.TARGET_NAMESPACE) String account;

 

2.2:在实现类上的@WebService注解使CXF知道使用那个接口来创建WSDL,我们示例中是DataService接口

 

3.发布服务

3.1:简单发布

DataService   implementor = new DataService(); 

String address = "http://localhost:9000/DataService"; 

Endpoint.publish(address,implementor);

 

3.2:你可以添加日志拦截器,进行发布

DataServiceimplementor = new DataService();  

JaxWsServerfactorybean svrFactory = new JaxWsServerfactorybean();  

svrFactory.setServiceClass(DataService.class);  

svrFactory.setAddress("http://localhost:9000/DataService");  

svrFactory.setServiceBean(implementor);  

svrFactory.getininterceptors().add(new LoggingInInterceptor());  

svrFactory.getoutInterceptors().add(new LoggingOutInterceptor());  

svrFactory.create();

 

4.访问服务

JaxWsProxyfactorybean factory = new JaxWsProxyfactorybean();  

factory.getininterceptors().add(new LoggingInInterceptor());  

factory.getoutInterceptors().add(new LoggingOutInterceptor());  

factory.setServiceClass(DataService.class);  

factory.setAddress("http://localhost:9000/DataService");  

DataServiceclient = (DataService) factory.create();  

 

String reply = client.sayHi("HI");  

System.out.println("Server said: " + reply);  

System.exit(0);  

 

总述:访问服务的同时,可以采用创建型模式中的工厂模式去设计,通过工厂类去创建服务,并访问服务;

publicclass DataServiceClientFactory implements factorybean<DataService> {

 

    String address;

 

    publicvoid setAddress(String address) {

       this.address = address;

    }

 

    @Override

    public DataService getobject() throws Exception {

       JaxWsProxyfactorybean factory = new JaxWsProxyfactorybean();

       factory.setServiceClass(DataService.class);

       factory.setAddress(address);

       if (factory.getProperties() == null) {

           Map<String,Object> properties = new HashMap<String,Object>();

           factory.setProperties(properties);

       }

       factory.getProperties().put("set-jaxb-validation-event-handler",

              "false");

       DataService service = (DataService) factory.create();

       return service;

    }

 

    @Override

    public Class<?> getobjectType() {

       return DataService.class;

    }

 

    @Override

    publicboolean isSingleton() {

       returntrue;

    }

}

 

 

5spring配置,spring配置文件中配置要发布的web服务:

<beans xmlns:jaxws="http://cxf.apache.org/jaxws"    xsi:schemaLocation="http://www.springframework.org/schema/beans

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

<import resource="classpath:meta-inf/cxf/cxf.xml" />

<import resource="classpath:meta-inf/cxf/cxf-extension-soap.xml" />

 

<jaxws:server id="dataServiceServer"

       serviceClass="com.dingli.component.server.dataservice.DataService"

       address="http://localhost/DataService">

       <jaxws:serviceBean>

           <ref bean="dataserviceBean" />

       </jaxws:serviceBean>

    </jaxws:server>

 

<!-- 另一种方式endpoint,server不同的是显示用户wsdl不同 

<jaxws:endpoint      id="dataService"     implementor="com.cxf.impl.DataServiceImpl"     address="http://localhost/DataService" />  

  --> 

</beans>

 

6:webservice注解详解

jax-ws是java开发webservice的标准API:

 

(1).javax.jws中常用的注解:

a.@Webservice:将一个类声明为webservice。

b.@Webparam:指定webservice的参数

c.@WebResul:指定webservice的返回值。

d.@WebMethod:指定某个方法为webservice对外提供的方法

 

(2).javax.jws.soap中常用的注解:

@SOAPBinding:将某个接口绑定为webservice

 

7:WebService的调用过程:

8.JAXB(java API for XML Bing):

JAXB是一种java对象和xml数据格式之间相互转换的API。

(1).JAXB的作用:

Java Object ——> JAXB ——> XML

(2).JAXB的工作原理:

(3).JAXB的常用注解:

a.@XmlRootElement:将java类映射为XML的根节点。

b.@XmlElement:将属性方法映射为XML的节点。

c.@XmlAccessorType:指明映射那些方法或者字段。

d.@XmlTransient:指明那些方法或者字段不需要和xml进行映射。

e.@XmlJavaTypeAdapter:Xml和Java对象的转换。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐