ITeye博客

如何建立一个WebService项目

rayln · 2011-10-24 11:09 · WebSevice · 开源软件 · Markdown

(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]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。