基于Java的webservice创建与jax-ws方式调用

来源:互联网 发布:mac mysql 启动失败 编辑:程序博客网 时间:2024/05/19 21:01

一、创建(服务端)

 

建立普通类,代码:

package com.jeefw.controller.sys;import javax.jws.WebMethod;import javax.jws.WebService;import javax.xml.ws.Endpoint;@WebServicepublic class TestwebService {@WebMethod(operationName="sayHello")public String sayHello(){return "Hello world!";}@WebMethod(operationName="getSum")public int getSum(int a,int b){return a+b;}public static void main(String [] args){Endpoint.publish("http://localhost:8083/HelloWorld", new TestwebService());System.out.println("success!");}}


浏览器键入http://localhost:8083/HelloWorld


二、调用(客户端)

利用jax框架生成的客户端

1、选择new->Other->web service client


2.选择所依赖的工程,选择JAX-WS框架,点击下一步


3.WSDL的地址填入下面界面的URL栏里,选择或者新建一个包,其他默认


4.点击确认后即可生成相应的文件如下


5、建立测试类Test.java

package webservice;public class Test {public static void main(String[] args) { TestwebServiceService test = new TestwebServiceService(); TestwebService hello = test.getTestwebServicePort();  String a  = hello.sayHello(); System.out.println(a); System.out.println("2 + 5 = "+hello.getSum(2, 5));}}


输出结果



webService调用成功


1 0