前言:项目中需要用到WebService来发起多平台之间的请求和回应,以前对WebService也不甚了解,然后先度娘了概念,这里再描述一下“能使得运行在不同机器上的应用不通过第三方软件和硬件就可以进行数据交换”,供以后的自己翻查。
下面主要记录一下自己在使用JDK1.6+apache tomcat7创建WebService的过程。
第一步:配置tomcat7环境,需要下载以下jar包jaxb-impl-2.1.2.jar、jaxws-api-2.1.jar,我在附件中提供下载,然后在
建立文件夹endorsed,然后将jar包放进去,这个是因为,如果缺少这两个jar包的话,在启动tomcat的时候以下错误
第二步:建立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,然后在浏览器地址中输入以下内容
StarOder为我的项目名,wsdl为webservice的一种描述语言。
最后,如果成功的话,会出现以下内容