JavaWeb中使用JDK自带的webService

前言:项目中需要用到WebService来发起多平台之间的请求和回应,以前对WebService也不甚了解,然后先度娘了概念,这里再描述一下“能使得运行在不同机器上的应用不通过第三方软件和硬件就可以进行数据交换”,供以后的自己翻查。

       下面主要记录一下自己在使用JDK1.6+apache tomcat7创建WebService的过程。

 

第一步:配置tomcat7环境,需要下载以下jar包jaxb-impl-2.1.2.jar、jaxws-api-2.1.jar,我在附件中提供下载,然后在

 建立文件夹endorsed,然后将jar包放进去,这个是因为,如果缺少这两个jar包的话,在启动tomcat的时候以下错误

错误 写道
Caused by: java.lang.NoClassDefFoundError: com/sun/xml/bind/CycleRecoverable

 

第二步:建立webservice对象,万恶的helloworld

package dwz.business.webservice;

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

@WebService
@SOAPBinding(style = Style.RPC, use = Use.LITERAL)
public class HelloWorld {

	@WebMethod
	@WebResult
	public String sayHelloWord(@WebParam String name) {
		return "My name is " + name;
	}

}

 

第三步:在WEB-INF路径下新建sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>   
    <endpoint
    name='TestService'
    implementation='dwz.business.webservice.HelloWorld'
    binding="http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/"
    url-pattern='/jdkService'/>
</endpoints>

 

第四步:在web.xml中增加以下内容

	<listener>          
	    <listener-class>  
	            com.sun.xml.ws.transport.http.servlet.WSServletContextListener   
	    </listener-class>  
  </listener> 
  
    <servlet>  
        <servlet-name>TestService</servlet-name>  
        <servlet-class>  
            com.sun.xml.ws.transport.http.servlet.WSServlet   
        </servlet-class>  
    </servlet>  
      
    <servlet-mapping>  
        <servlet-name>TestService</servlet-name>  
        <url-pattern>/jdkService</url-pattern> 
    </servlet-mapping>

 

第五步:lib包中还需要一个jaxws-rt-2.0EA3.jar,附件中有下载

 

第六步:启动tomcat,然后在浏览器地址中输入以下内容

URL 写道
http://localhost:8080/StarOrder/jdkService?wsdl

 StarOder为我的项目名,wsdl为webservice的一种描述语言。

 

最后,如果成功的话,会出现以下内容

写道
<definitions targetNamespace="http://webservice.business.dwz/" name="HelloWorldService"><types/><message name="sayHelloWord"><part name="arg0" type="xsd:string"/></message><message name="sayHelloWordResponse"><part name="return" type="xsd:string"/></message><portType name="HelloWorld"><operation name="sayHelloWord" parameterOrder="arg0"><input message="tns:sayHelloWord"/><output message="tns:sayHelloWordResponse"/></operation></portType><binding name="HelloWorldPortBinding" type="tns:HelloWorld"><soap12:binding transport="http://www.w3.org/2003/05/soap/bindings/HTTP/" style="rpc"/><operation name="sayHelloWord"><soap12:operation soapAction=""/><input><soap12:body use="literal" namespace="http://webservice.business.dwz/"/></input><output><soap12:body use="literal" namespace="http://webservice.business.dwz/"/></output></operation></binding><service name="HelloWorldService"><port name="HelloWorldPort" binding="tns:HelloWorldPortBinding"><soap12:address location="http://localhost:8080/StarOrder/jdkService"/></port></service></definitions>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值