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对象的转换。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tony168hongweigan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值