怎么创建web服务

1.先创建一个接口
package com;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService()
@SOAPBinding(style =Style.RPC)
public interface ICalculator {

@WebMethod
int add(int i,int j);

}

注意:jdk1.6u17以下的版本,如果没有@SOAPBinding(style =Style.RPC),就会出错。有两种方法可以解决,一是升级JDK到版本jdk1.6u17或以后;二是加上@SOAPBinding(style =Style.RPC)。

2.实现该接口

package com;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;


@WebService(endpointInterface="com.ICalculator")
@SOAPBinding(style =Style.RPC)
public class Calculator implements ICalculator{
public Calculator(){}

@WebMethod(operationName="add", action="urn:Add")
public int add(int i, int j){
int k = i +j ;
System.out.println(i + "+" + j +" = " + k);
return k;
}
}

3.把该实现类发布为服务
package com;

import javax.xml.ws.Endpoint;

public class PubliserSer {

public static void main(String args[]){

Endpoint.publish("http://127.0.0.1:8989/myweb", new Calculator());//new Calculator()指的是实现类
System.out.println("The Server is started");

}

}

4.创建客户端调用该服务
package com;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class Test2 {

/**
* @param args
*/
public static void main(String[] args) {


try {
URL url=new URL("http://127.0.0.1:8989/myweb?wsdl");
QName qn=new QName("http://com/", "CalculatorService");//第一个参数指的是包名称,第二个参数是“实现类名+Service”
Service s=Service.create(url,qn);

ICalculator sm=(ICalculator)s.getPort(ICalculator.class);//这里面的ICalculator和@WebService(endpointInterface="com.ICalculator")现对应,必须是接口
System.out.println(sm.add(1, 9));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

}


5.相关API说明

(1)Endpoint类的静态方法
publish

public static Endpoint publish(java.lang.String address,
java.lang.Object implementor)

Creates and publishes an endpoint for the specified implementor object at the given address.

The necessary server infrastructure will be created and configured by the JAX-WS implementation using some default configuration. In order to get more control over the server configuration, please use the create(String,Object) and publish(Object) methods instead.

Parameters:
address - A URI specifying the address and transport/protocol to use. A http: URI MUST result in the SOAP 1.1/HTTP binding being used. Implementations may support other URI schemes.
implementor - The endpoint implementor.
Returns:
The newly created endpoint.
Throws:
java.lang.SecurityException - If a java.lang.SecurityManger is being used and the application doesn't have the WebServicePermission("publishEndpoint") permission.


(2).Service类的静态方法
在客户端创建一个webservice服务实例
create

public static Service create(java.net.URL wsdlDocumentLocation,
javax.xml.namespace.QName serviceName)

Creates a Service instance. The specified WSDL document location and service qualified name MUST uniquely identify a wsdl:service element.

Parameters:
wsdlDocumentLocation - URL for the WSDL document location for the service
serviceName - QName for the service
Throws:
WebServiceException - If any error in creation of the specified service.

(3).Service类的静态方法,在客户端得到服务类的代理,然后对其进行操作
getPort

public <T> T getPort(java.lang.Class<T> serviceEndpointInterface)

The getPort method returns a proxy. The parameter serviceEndpointInterface specifies the service endpoint interface that is supported by the returned proxy. In the implementation of this method, the JAX-WS runtime system takes the responsibility of selecting a protocol binding (and a port) and configuring the proxy accordingly. The returned proxy should not be reconfigured by the client.

Parameters:
serviceEndpointInterface - Service endpoint interface.
Returns:
Object instance that supports the specified service endpoint interface.
Throws:
WebServiceException -

* If there is an error during creation of the proxy.
* If there is any missing WSDL metadata as required by this method.
* If an illegal. serviceEndpointInterface is specified.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值