Web Service简单入门示例

    我们一般实现Web Service的方法有非常多种。当中我主要使用了CXF Apache插件和Axis 2两种。

Web Service是应用服务商为了解决每一个问题而提供的在线服务方案,其主要採用了SOAP(Simple Object Access Protocol)协议,传输数据格式使用XML格式来描写叙述。因此也具有跨平台的特性。

web广泛用到的技术:
  1. TCP/IP:通用网络协议。被各种设备使用
  2. HTML标准通用标记语言下的一个应用):通用用户界面,能够使用HTML标签显示数据
  3. Java:写一次能够在不论什么系统执行的通用编程语言,由于java具有跨平台特性
  4. XML标准通用标记语言下的一个子集):通用数据表达语言,在web上传送结构化数据的easy方法
他们的特点是其开放性。跨平台性,开放性正是Web services的基础。

以下是使用CXF Apache的插件实现Web Service的一个简单入门实例





1========新建一个服务接口



package com.clark;



import javax.jws.WebParam;

import javax.jws.WebService;



@WebService

public interface IHelloWorld {

    

    public String sayHello(@WebParam(name="name")String name);

    

    public int plus(int a,int b);

}

2========服务接口实现类



package com.clark.impl;



import com.clark.IHelloWorld;



public class HelloWorldImpl implements IHelloWorld {



    @Override

    public String sayHello(String name) {

        return "Hello Wolrd ,"+name;

    }



    @Override

    public int plus(int a, int b) {

        return a+b;

    }



}



3============服务端



package com.clark.service;



import javax.xml.ws.Endpoint;



import com.clark.impl.HelloWorldImpl;



public class WebServiceApp {

    public static void main(String[] args) {

        System.out.println("web service start");

        HelloWorldImpl implementor = new HelloWorldImpl();

        String address = "http://localhost:8080/IHelloWorld";

        Endpoint.publish(address, implementor);

        System.out.println("web service started");

    }

}



4============client(以下主要是针对Java普通程序的)

package com.clark.client;



import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;



import com.clark.IHelloWorld;



public class HelloWorldClient {

    public static void main(String[] args) {

        JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();

        svr.setServiceClass(IHelloWorld.class);

        svr.setAddress("http://localhost:8080/CXFWebService/service/IHelloWorld");

        IHelloWorld hw = (IHelloWorld) svr.create();

        String name = hw.sayHello(" CXF Apache implements Web Service");

        int result = hw.plus(2, 3);

        System.out.println(name);

        System.out.println(result);

    }

}



4==============client(针对Spring中集成Web Service的Web开发)

package com.clark.web;



import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;



import com.clark.IHelloWorld;



public class HelloWorldClient {

    public static void main(String[] args) {

        System.out.println("Web Service start..........");

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        

        IHelloWorld helloWorld = (IHelloWorld)context.getBean("client");

        

        String name = helloWorld.sayHello("1111111111");

        int result = helloWorld.plus(3, 4);

        System.out.println(name+"  "+result);

        

        System.out.println("Web Service end..........");

    }

}



5============Spring 的 applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?

>

<beans xmlns="http://www.springframework.org/schema/beans"

                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                 xmlns:jaxws="http://cxf.apache.org/jaxws"

                 xsi:schemaLocation="

                       http://www.springframework.org/schema/beans



                       http://www.springframework.org/schema/beans/spring-beans.xsd

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



    <jaxws:endpoint

                              id="helloWorld"

                              implementor="com.clark.impl.HelloWorldImpl"

                              address="/IHelloWorld" />

   

                <bean id="client" class="com.clark.IHelloWorld"

                           factory-bean="clientFactory" factory-method="create"/>

    

                 <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">

                            <property name="serviceClass" value="com.clark.IHelloWorld"/>

                            <property name="address"



                                              value="http://localhost:8080/CXFWebService/service/IHelloWorld"/>

                  </bean>

</beans>



6=============Spring中集成Web Service服务(CXF Servlet的配置),web.xml

<?

xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <display-name>CXFWebService</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

 

  <context-param>

      <param-name>contextConfigLocation</param-name>

      <!-- <param-value>classpath:applicationContext.xml</param-value> -->

      <param-value>WEB-INF/applicationContext.xml</param-value>

  </context-param>

 

  <listener>

      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

 

  <servlet>

                     <servlet-name>CXFServlet</servlet-name>

                     <display-name>CXFServlet</display-name>

                     <servlet-class>

                            org.apache.cxf.transport.servlet.CXFServlet

                     </servlet-class>

                     <load-on-startup>1</load-on-startup>

               </servlet>



               <servlet-mapping>

                      <servlet-name>CXFServlet</servlet-name>

                      <url-pattern>/service/*</url-pattern>

               </servlet-mapping>

</web-app>



7=============启动服务,地址栏输入

http://localhost:8080/CXFWebService/service/IHelloWorld?

wsdl可以看到对应的SOAP协议在规格OK


版权声明:本文博客原创文章,博客,未经同意,不得转载。

Web Service简单入门示例的更多相关文章

  1. Springmvc整合tiles框架简单入门示例(maven)

    Springmvc整合tiles框架简单入门示例(maven) 本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合. 先贴上源码(免积 ...

  2. 【java开发系列】—— spring简单入门示例

    1 JDK安装 2 Struts2简单入门示例 前言 作为入门级的记录帖,没有过多的技术含量,简单的搭建配置框架而已.这次讲到spring,这个应该是SSH中的重量级框架,它主要包含两个内容:控制反转 ...

  3. Java:Web Service初入门

    前言 Web Service技术在我第一次接触,又没有实际使用时完全不理解这是什么.以为是一种类似Spring,Shiro的编程框架.后来渐渐理解,WS(即Web Service缩写)是一种通用的接口 ...

  4. Web Service简单demo

    最近开发因需求要求需要提供Web Service接口供外部调用,由于之前没有研究过该技术,故查阅资料研究了一番,所以写下来记录一下,方便后续使用. 这个demo采用CXF框架进行开发,后续所提到的We ...

  5. Java restful web service 开发入门

    可用的框架有不少,我用的是jersey. 直接上代码,其实,如果你会web service 这个restful的就很好理解了,自己跑一遍就OK了 用到的类 User.java package demo ...

  6. 百度 WebUploader 简单入门示例

    首先一定要引入:jquery.js 然后是webuploader的一个 css和还一个js 三个必须引用. <!DOCTYPE html> <html xmlns="htt ...

  7. spring简单入门示例

    1 控制反转IOC\依赖注入DI,因为翻译的不同,因此有两个名字.       控制反转意思就是说,当我们调用一个方法或者类时,不再有我们主动去创建这个类的对象,控制权交给别人(spring). 依赖 ...

  8. C# 之 Socket 简单入门示例

    这个例子只是简单实现了如何使用 Socket 类实现面向连接的通信. 注意:此例子的目的只是为了说明用套接字写程序的大概思路,而不是实际项目中的使用程序.在这个例子中,实际上还有很多问题没有解决,如消 ...

  9. C# Web Service简单使用

    第一步 打开VS,新建一个项目 第二步  创建一个ASP.NET 空 Web应用程序 我这里用的是VS2017 第三步 添加一个Web 服务(ASMX) 右键解决方案-->添加-->新建项 ...

随机推荐

  1. WOJ 1020

    #include<stdio.h> #include<stdlib.h> int main() { int n,m; int *num,*link; int i,j,t,k=0 ...

  2. freemarker自己定义标签(二)

    freemarker自己定义标签 1.自己定义标签 通过自己定义标签,写一个反复指定字符串 2.实现源代码 <html> <head> <meta http-equiv= ...

  3. SCU 2009(数位dp)

    传送门:Zeros and Ones 题意:求总数位为n包含0和1个数相同且整除k的二进制数的个数. 分析:设dp[pos][num][md]表示还有pos位已包含num个1且模k余md的符合条件的二 ...

  4. hdu1260(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260 分析:简单dp,dp[i]=min(dp[i-1]+a[i],dp[i-2]); #includ ...

  5. C++:抽象基类和纯虚函数的理解

    转载地址:http://blog.csdn.net/acs713/article/details/7352440 抽象类是一种特殊的类,它是为了抽象和设计的目的为建立的,它处于继承层次结构的较上层. ...

  6. 重温delphi之控制台程序:Hello World!

    原文:重温delphi之控制台程序:Hello World! 这二天用c#开发ActiveX时,发现不管怎么弄,c#就是没办法生成ocx的纯正activeX控件,而且还要强迫用户安装巨大的.net f ...

  7. DirectX11 学习笔记9 - 动态顶点缓冲区 和 静态顶点缓冲区

    首先,什么是缓冲区: 缓冲区是.fx文件的影响(.ps .vs还) 一种数据结构,其定义了.为.fx和cpp数据通信文件. 例: //--------------------------------- ...

  8. IOS开发笔记 - 基于wsdl2objc调用webservice

    为了方便在ios下调用webserivce,找来了wsdl2objc这样一个开源的框架来解析webservice方便在ios下引用. 下面做个小例子. 1.首先是用Asp.net搭建一个测试的webs ...

  9. 一步一步学android之事件篇——触摸事件

    触摸事件顾名思义就是触摸手机屏幕触发的事件,当用户触摸添加了触摸事件的View时,就是执行OnTouch()方法进行处理,下面通过一个动态获取坐标的例子来学习OnTouchListener事件,效果如 ...

  10. PL/SQL“ ORA-14551: 无法在查询中执行 DML 操作”解决

    环境 Oracle 11.2.0 + SQL Plus 问题 根据以下要求编写函数:将scott.emp表中工资低于平均工资的职工工资加上200,并返回修改了工资的总人数.PL/SQL中有更新的操作, ...