---
title: "如何建立一个WebService项目"
author: "rayln"
date: "2011-10-24T11:09:07Z"
tags: []
categories: ["开源软件"]
slug: "webservice"
draft: false
lastmod: "2011-09-27T10:47:32Z"
id: 1181286
url: "https://rayln.iteye.com/blog/1181286"
---

(1) 首先新建一个项目,导入Spring和axis包 (2) 修改web.xml文件 \[code="xml"] contextConfigLocation /WEB-INF/applicationContext.xml contextConfigLocation org.springframework.web.context.ContextLoaderListener axis org.apache.axis.transport.http.AxisServlet 2 axis /ws/* \[/code] (3) 建立applicationContext.xml \[code="xml"]\[/code] (4) 配置一个wsdd文件,名称为server-config.wsdd,放在webRoot/WEB-INF/ \[code="xml"] http://xml.apache.org/axis/wsdd/ \[/code] (5) 编写JaxRpcMathService类 \[code="java"]public class JaxRpcMathService extends ServletEndpointSupport implements MathService { private MathService mathService; protected void onInit() { this.mathService = (MathService) getWebApplicationContext().getBean(MathService.BEAN\_ID); } public int add(int x, int y) { return mathService.add(x,y); } public int subtract(int x, int y) { return mathService.subtract(x,y); } public MathVO testVO(MathVO vo) { return mathService.testVO(vo); } }\[/code] (6) 编写MathService接口类 \[code="java"]public interface MathService { String BEAN\_ID = "MathBean"; public int add(int x, int y); public int subtract(int x, int y); public MathVO testVO(MathVO vo); }\[/code] (7) 编写MathServiceImpl实现类 \[code="java"]public class MathServiceImpl implements MathService{ public int add(int x, int y) { return x+y; } public int subtract(int x, int y) { return x-y; } public MathVO testVO(MathVO vo) { System.out.println(vo.toString()); return vo; } }\[/code] (8) 客户端代码生成 通过eclipse打开\[b]\[color=olive]Open Run Dialog\[/color]\[/b] 在main class中填入\[b]\[color=olive]org.apache.axis.wsdl.WSDL2Java\[/color]\[/b] 在arguments中填入参数 \[b]\[color=olive]-p src.com.test.vo http://localhost:8080/wsdemo1/ws/MathService?WSDL\[/color]\[/b] 启动tomcat,然后点击run生成代码 (9) 代码生成完毕后编写客户端调用TestMathService类 \[code="java"]public class TestMathService { public static void main(String\[] args) { JaxRpcMathServiceService service = new JaxRpcMathServiceServiceLocator(); JaxRpcMathService client = service.getMathService(); System.out.println(client.add(1, 2)); } } \[/code]