我的WebService学习第一课

  接触到webservce有一段时间了,终于有机会来专门学习一下原理。
  闲话不多说,第一课先来做一个简单的JAX Demo,然后再来总结一下。


 
  服务器的建立:
  1. 接口 - IMyService.java

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public interface IMyService {
public int add(int a,int b);
public int minus(int a, int b);
}

2. 实现类 - MyServiceImpl.java

import javax.jws.WebService;

@WebService(endpointInterface="www.pintn.service.IMyService")
public class MyServiceImpl implements IMyService {

@Override
public int add(int a, int b) {
System.out.println(a + "+" + b + "=" + (a+b));
return a+b;
}

@Override
public int minus(int a, int b) {
System.out.println(a + "-" + b + "=" + (a-b));
return a-b;
}
}

3 开启服务 - MyServer.java
public class MyServer {
public static void main(String[] args) {
String address = "http://localhost:9999/ns";
Endpoint.publish(address, new MyServiceImpl());
}
}

4. 在同一个工程里建立一个简单的测试client类 - TestClient.java

public class TestClient {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:9999/ns?wsdl");
QName qname = new QName("http://service.pintn.www/","MyServiceImplService");
Service service = Service.create(url, qname);

IMyService ms = service.getPort(IMyService.class);
System.out.println(ms.add(12, 13));
} catch (MalformedURLException e) {
e.printStackTrace();
}

}
}

以上是一个最简单的webservice模型了。以下是总结要点:
1. 发布服务的方法,Endpoint.publish(String address, Object implementor),包含两个参数:访问wsdl地址,实现类实例
2. 接口的定义,需要加annotation,标明是webservice接口。如:
       @WebService(必加)
      @SOAPBinding(style = Style.RPC) (与我的java版本有关,看视频里是不需要加的,但是自己做的时候不加会报错,留待接下来的学习中解答)

    实现类也要加annotation,注明对应的webservice接口。如:
       @WebService(endpointInterface="www.pintn.service.IMyService")

问题:
这个测试的client类,还是和server端在一起的。但是webservice的特点在于client与server没有语言、环境上的依赖,怎么来解决这个问题呢?

 

猜你喜欢

转载自dongqiang1989-126-com.iteye.com/blog/2002842