参考资料:html
1. http://blog.sina.com.cn/s/blog_a00f56270102w2e2.html - WebService简介+只用JDK实现java
2. http://www.cnblogs.com/linxiaoyang/p/4167016.html - JDK实现+Intellij辅助web
3. http://www.cnblogs.com/wenjingu/p/4094180.html - WebService开发汇总spring
4. 推荐: http://blog.csdn.net/rongbo_j/article/details/50958111浏览器
从名字上看,以前一直觉得Web Service就是一般提的Web应用,毕竟两者都是对外提供web服务的。但实际上仍是有区别的,区别为Web Service和Web Application比较合适。spring-mvc
Web Service更趋向于后台服务。mvc
实现Web Service的框架有不少,好比JAX-WS, Axis, Axis2, CXF等,有代码优先的也有文档优先的。app
本着简单+功能强的原则,这里不涉及具体细节,好比如何编写wsdl(实际上也不会,也不必会这个),只是单纯的实现须要的功能便可。框架
------------------------------------------------ 分割线 ------------------------------------------------jsp
Part 1: JDK实现Web Service
JDK提供JAX-WS(Java API for XML Web Service)来实现Web Service。至关之简单。
直接上代码!
1. server端
编写Web Service
@WebService public interface HelloJWS { @WebMethod public String execute(String msg); }
@WebService(endpointInterface = "server.com.cnblogs.helloyz.HelloJWS") public class HelloJWSImpl implements HelloJWS { @Override public String execute(String msg) { System.out.println("Welcome! " + msg); return "success"; } }
对外发布
public class PublishService { public static void main(String[] args) { HelloJWS jws = new HelloJWSImpl(); String address = "http://localhost:9000/ws/HelloJWS"; Endpoint.publish(address,jws); System.out.println("publish success"); } }
运行main函数,Web Service发布成功!是否是很简单?!
浏览器访问发布地址:http://localhost:9000/ws/HelloJWS/?wsdl,能够获得以下wsdl文档。
PS: wsdl文档语法不懂,也不想学这个。
以上方式就属于所谓的"代码优先",根据代码生成wsdl文件。而"文档优先"方式在适应需求变化,维护等方面要优于"代码优先"。
不过,不必非采用"文档优先",只要功能实现就OK,何况不用专门编写wsdl(也曾想经过xsd生成wsdl,但实际上还要专门编写xsd...)。
2. client端
为了方便,将server端和client端弄到一个工程里了。如下几步是在建立的client包里操做。
生成本地Web Service代码
调出命令行窗口,执行wsimport -s . http://localhost:9000/ws/HelloJWS/?wsdl,生成本地client代码。
编写代码调用Web Service
public class Client { public static void main(String[] args) { HelloJWSImplService service = new HelloJWSImplService(); HelloJWS jws = service.getHelloJWSImplPort(); String result = jws.execute("message from MARS"); System.out.println("client execute status: " + result); } }
运行main,结果以下:
Source Code get here
Part 2: Web容器发布Web Service
有很多人认为单纯用JDK实现WebService,不便于后期的管理和维护,并且可扩展性也不好。不敢苟同。
根据上面的Demo可知,web service的编写和发布都是能够单独集中在一块儿的。好比,在一个@WebService里,你能够实现多个@WebMethod;在一个main方法里,你能够集中发布多个Web Service。因此不存在上面提到的问题。
使用Web容器来发布Web Service,我的以为主要仍是考虑到Web Service做为Web服务的一种,理应由Web容器来统一管理,跟随主流。另外,相比于JDK那种经过main方法来发布服务,仍是利用容器管理更稳定些。
废话少说,直述build过程:(利用Intellij IDEA 14.1.4)
1. server端
新建WebServices工程,下载21个jar包,同时生成server sample code。包结构以下:
21个jar包在lib中。将Part 1的server端代码放到src下。设置Tomcat为WebService容器,并尝试发布。
大体梳理下WebService发布过程:
1. Tomcat启动,加载web.xml.
实例化监听WSServletContextListener。此过程当中加载/WEB-INF/sun-jaxws.xml。
web.xml
<listener> <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class> </listener> <servlet> <description>JAX-WS endpoint</description> <display-name>WSServlet</display-name> <servlet-name>WSServlet</servlet-name> <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>WSServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
sun-jaxws.xml
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'> <endpoint name='HelloWorld' implementation='com.cnblogs.helloyz.HelloWorld' url-pattern='/services/HelloWorld'/> </endpoints>
接着初始化WSServlet,用于拦截/services/*请求(界定WebService和WebApplication)
2. 根据加载的sun-jaxws.xml,建立用于发布WebService的endpoint(此处用于集中管理endpoint)。
2. client端
新建WebServices Client工程,下载21个jar包。和Part 1相似,生成client code。
Source Code get here
Part 3: Web Service与Spring集成
又一参考:http://www.springbyexample.org/examples/simple-spring-web-services.html
原本想按照Spring-WS的文档,实际操做遍。可是通过前先后后一天半的时间发现怎么都绕不过“文档优先”的Web Service发布方式。原觉得经过使用@EndPoint和@PayloadRoot就能够,但仍是须要预先定义好的wsdl。
同时也发现GitHub上托管的Spring Web Service项目大部分代码已经两年多没更新过,网上的讨论也不多,足以说明Spring Web Service并不怎么经常使用。
不过Web Service和Spring的整合,仍是有必定必要的,好比将Web Service同Spring MVC整合在一块儿,让Web Service调用Spring MVC的某些功能。
用IDEA建立Spring MVC工程。调整后的结构以下(忽略target)。
将Web Service整合到Spring MVC中就要保证两者均能正常工做,同时能够交互(好比Web Service调用Spring MVC里的功能)。
列出一些关键点:
1. spring-mvc-servlet.xml
1 <context:component-scan base-package="com.cnblogs.helloyz"/> 2 3 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 4 <property name="prefix" value="/WEB-INF/pages/"/> 5 <property name="suffix" value=".jsp"/> 6 </bean> 7 8 <bean id="jaxWsServiceExporter" class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter"> 9 <property name="baseAddress" value="http://localhost:9000/services/"/> 10 </bean>
在这个xml里添加SimpleJaxWsServiceExporter,用于发布Web Service。
2. HelloWorld.java
@Component @WebService(serviceName = "hello-service") public class HelloWorld { @Autowired private HelloController helloController; @WebMethod public String sayHelloWorldFrom(String from) { String result = "Hello, world, from " + from; System.out.println(result); return result; } @WebMethod public String sayHello(String from, String to) { String result = from + ":\"Hello\", " + to; System.out.println(result); System.out.println("------------------"); helloController.prepareForWebService(); return result; } }
全部WebService必须由Spring容器管理,也才能经SimpleJaxWsServiceExporter对外发布。
此外,注入helloController来验证Web Service同Spring MVC的交互。
启动Tomcat进行验证。
1. Spring MVC功能正常。
2. 访问http://localhost:9000/services/hello-service?wsdl,Web Service发布成功。
3. 同Part 1,编写client端代码。
Client.java
public class Client { public static void main(String[] args) { HelloService helloService = new HelloService(); HelloWorld helloWorld = helloService.getHelloWorldPort(); String result = helloWorld.sayHello("Mars", "Earth"); System.out.println(result); System.out.println("-------------"); result = helloWorld.sayHelloWorldFrom("Mars"); System.out.println(result); } }
运行main,结果以下:
Web Service功能正常;Web Service调用Spring MVC功能成功。
Source Code get here
Done!