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.
分享到:
相关推荐
创建Web服务创建Web服务创建Web服务创建Web服务
创建 Web 服务 构建代理和测试客户机 将应用程序打包以进行部署 在 WebSphere Application Server 上部署 将 Web 服务发布到 UDDI 注册中心 验证 Speed-start 发布 发现和测试服务 结束语 参考资料 关于作者 相关...
学web Service(web服务)和.net remoting必看的一本书 它绝对可以提高你的.net水平,收2分咯! 下完请下第二部分才能解压
应用服务器的搭建--创建Web服务器
C# Web服务高级编程——使用.NET Remoting和ASP.NET创建Web服务 源代码
2022年创建Web服务器程序.ppt
其为使用ASP创建WEB服务网的外文文献
创建Web服务.pptx
ASP-NET创建Web服务之发布和部署.doc
创建Web服务(1).ppt
创建Web服务(1).pptx
nodejs创建web服务器之hello world程序.docx
1,创建Web服务; 2,测试Web服务; 3,生成服务代码类; 4,生成服务代理程序,即DLL文件; 5,本地Web服务的调用; 6,远程调用Web服务; 7,Web服务的综合应用 注:内附有详细代码。桔子的香味,只有吃了才知道!
day2 day3 创建web服务器 模块化 包.xmind
不要轻易下载,只有两个文件,都在这里:https://blog.csdn.net/fukaiit/article/details/102756238 我只是为了自己需要时下载方便
PHP Cookbook读书笔记 &ndash; 第15章创建Web服务.docx
主要介绍了.NET中创建Web服务实现方法,有需要的朋友可以参考一下