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. 使用ILmerge合并Exe、Dll文件的帮助类

    原文:使用ILmerge合并Exe.Dll文件的帮助类 using System; using System.Collections.Generic; using System.Text; using ...

  2. Anyterm - Introduction

    Anyterm - Introduction Anyterm

  3. RCP开发中错误:java.lang.RuntimeException: WARNING: Prevented recursive attempt to activate part...

    在做RCP的eclipse插件开发时,启动管理软件界面时,总是报如下错误 : !ENTRY org.eclipse.ui.workbench 4 0 2012-05-25 18:44:21.306 ! ...

  4. HDU 4022 Bombing (map + multiset)

    题意: 在x,y坐标范围为10 ^ -9 ~~ 10 ^ 9的坐标轴之中,有 10W个点(注意有些点可能在同一坐标上),然后有10W个询问,处理询问按照输入顺序处理,对于每个询问a,b    a == ...

  5. GOJ1150(矩阵快速幂)

    sum Time Limit: 1000ms Problem Description: 给定a和n,计算a+aa+aaa+aaaa+...+a...a(n个a) 的和. Input: 测试数据有多组, ...

  6. java获取当前日期时间代码总结

    1.获取当前时间,和某个时间进行比较.此时主要拿long型的时间值.  方法如下: 要使用 java.util.Date .获取当前时间的代码如下  代码如下 复制代码 Date date = new ...

  7. ubuntu14.04中 gedit 凝视能显示中文,而source insight中显示为乱码的解决的方法

    1.乱码显示情况: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcjc3NjgzOTYy/font/5a6L5L2T/fontsize/400/fill/ ...

  8. <EditText /> This text field does not specify an inputType or a hint

    我是一个警告,xml代码是: <EditText android:id="@+id/str_ipaddress" android:layout_width="wra ...

  9. JS 昵称,手机号,邮箱判断

    <script type="text/javascript"> var leyou = document.getElementById('J-leyou'), _nam ...

  10. [WPF]DataGrid C#添加右键弹出选择菜单

    private void dataGrid_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { ContextMenu cont ...