cxf+webservice(java)

来源:互联网 发布:2017全国双创数据报告 编辑:程序博客网 时间:2024/06/05 07:56

       上一篇(spring+cxf+jaxrs)讲了利用cxf实现rest,现在讲一讲cxf对web services(ws)的支持.

        熟悉一些ws,ws 可以将应用程序转换为网络应用程序,换句话说,一个ws服务必须提供一个不同客户端(包括不同语言)的访问入口(web访问-http,使用浏览器或者http调用等).这里就要了解的是web service的三大要素:

soap(simple object access protocol)-简单对象访问协议,数据交换协议规范 -- 客户端如何被解析访问,返回数据如何解析接收

wsdl(web services description language)-ws描述性语言,ws语言框架(规范) -- 定义服务内容(暴露接口)

uddi(universal description discovery and integration)- 通用描述,发现与集成服务 -- 目录服务(ws管理-注册与搜索)

        ws的实现平台xml+http, 数据的结构载体xml,通讯协议http.官网上对cxf支持的概述-Generating WSDL from Java classes and generating Java classes from WSDL(cxf-index),

        换句话说,cxf可以将java代码转换成wsdl(可以理解为数据中介,能被各种客户端识别)供其他客户端访问(把自己的产品发布成ws服务),同时也支持通过解析wsdl生成java代码(根据别人的产品需求(wsdl格式展示)生成自己的代码--此处仅仅用此例子解释其作用,个人理解...),解释完基本概况,就是码代码,实现上述功能.

简单ws服务(官网照抄a simple jax-ws service)

pom.xml

<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxrs</artifactId><version>3.0.0-milestone1</version></dependency>
HelloWorld

@WebServicepublic interface HelloWorld {    String sayHi(String text);}
HelloWorldImpl

@WebService(endpointInterface = "cxf.test.ws.service.impl.HelloWorld",            serviceName = "HelloWorld")public class HelloWorldImpl implements HelloWorld {    public String sayHi(String text) {        System.out.println("sayHi called");        return "Hello " + text;    }}
启动服务server

/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */package cxf.test.ws.service.impl;import javax.xml.ws.Endpoint;public class Server {    protected Server() throws Exception {        // START SNIPPET: publish        System.out.println("Starting Server");        HelloWorldImpl implementor = new HelloWorldImpl();        String address = "http://localhost:9000/helloWorld";        Endpoint.publish(address, implementor);        // END SNIPPET: publish    }    public static void main(String args[]) throws Exception {        new Server();        System.out.println("Server ready...");        Thread.sleep(5 * 60 * 1000);        System.out.println("Server exiting");        System.exit(0);    }}
启动服务,访问http://localhost:9000/helloWorld?wsdl  返回wsdl

<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --><definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.ws.test.cxf/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.service.ws.test.cxf/" name="HelloWorld"><types><xsd:schema><xsd:import namespace="http://impl.service.ws.test.cxf/" schemaLocation="http://localhost:9000/helloWorld?xsd=1"/></xsd:schema></types><message name="sayHi"><part name="parameters" element="tns:sayHi"/></message><message name="sayHiResponse"><part name="parameters" element="tns:sayHiResponse"/></message><portType name="HelloWorld"><operation name="sayHi"><input wsam:Action="http://impl.service.ws.test.cxf/HelloWorld/sayHiRequest" message="tns:sayHi"/><output wsam:Action="http://impl.service.ws.test.cxf/HelloWorld/sayHiResponse" message="tns:sayHiResponse"/></operation></portType><binding name="HelloWorldImplPortBinding" type="tns:HelloWorld"><soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><operation name="sayHi"><soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation></binding><service name="HelloWorld"><port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding"><soap:address location="http://localhost:9000/helloWorld"/></port></service></definitions>
访问http://localhost:9000/helloWorld

client访问

service访问

public final class Client {    private static final QName SERVICE_NAME     = new QName("http://server.hw.demo/", "HelloWorld");private static final QName PORT_NAME     = new QName("http://server.hw.demo/", "HelloWorldPort");    private Client() {    }     public static void main(String args[]) throws Exception {        Service service = Service.create(new URL("http://localhost:9000/helloWorld"),SERVICE_NAME);        // Endpoint Address        String endpointAddress = "http://localhost:9000/helloWorld";        service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);        // If web service deployed on Tomcat (either standalone or embedded)        // as described in sample README, endpoint should be changed to:        // String endpointAddress = "http://localhost:8080/java_first_jaxws/services/hello_world";        // Add a port to the Service                HelloWorld hw = service.getPort(HelloWorld.class);        System.out.println(hw.sayHi("World"));}}}

      通过cxf的wsdl2java生成客户端访问,下载cxf包(cxf3.1.2下载),cmd 进入bin目录,执行

       wsdl2java –d http://localhost:9000/helloWorld?wsdl

      将生成代码拷入jar工程,新建client访问

package demo.hw.server;public class HelloWorldClient {public static void main(String[] args) {HelloWorld_Service servie = new HelloWorld_Service();HelloWorld port = servie.getHelloWorldImplPort();port.sayHi("hello");port.sayHiToUser(new User());}}

或者执行 wsdl2java –d -client http://localhost:9000/helloWorld?wsdl

生成客户端HelloWorld_HelloWorldImplPort_Client访问

wsdl2java -p com -d src -all  aa.wsdl

-p  指定其wsdl的命名空间,也就是要生成代码的包名:

-d  指定要产生代码所在目录

-client 生成客户端测试web service的代码

-server 生成服务器启动web  service的代码

-impl 生成web service的实现代码

-ant  生成build.xml文件

-all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.

详细用法见:wsdl






原创粉丝点击