WebService简单使用
1.WebService是什么?
WebService是一个基于Web的服务,使服务端的资源能够被客户端访问。
实质上WebService是一种跨编程语言和操作系统平台的远程调用技术或者说一种规范。
刚开始接触WebService时,会感到迷惑。WebService和Struts2、SpringMVC似乎在功能上都差不多。 但其实还是有本质差别的,Struts2和SpringMVC都是基于框架的,属于MVC中的Controller,是封装了针对传统Http请求的处理,对客户端的请求没有太多要求,按照正常发送Http请求即可。
而WebService更多的是一种概念。是基于语言的基础上的,不同的语言有不同的实现,传统的WebService是基于Http + xml片段的,消息传递是封装在xml中,采用wsdl描述请求与相应数据,采用soap协议交换数据。
在开发过程中,可能还会产生一个疑问,看起来WebService要实现的功能,我使用Struts2和SpringMVC的的接口也都能实现啊,为什么还要用?
这个问题,我猜如果以前都使用的SpringMVC这种框架,现在要来使用WebService开发,绝对不是自己选择的吧。大概率都是别人要求的,技术妥协。。。。。
个人感觉WebService用起来不是很爽,可能现在WebService的使用更多的是考虑到对.net这种开发的支持吧。
目前WebService大致分为两种,Soap WebService 与 Restful WebService。
相对而言,SOAP协议属于复杂的、重量级的协议,当前随着Web2.0的兴起,表述性状态转移(Representational State Transfer,REST)逐步成为一个流行的架构风格。REST是一种轻量级的Web Service架构风格,其实现和操作比SOAP和XML-RPC更为简洁,可以完全通过HTTP协议实现,还可以利用缓存Cache来提高响应速度,性能、效率和易用性上都优于SOAP协议。REST架构对资源的操作包括获取、创建、修改和删除资源的操作正好对应HTTP协议提供的GET、POST、PUT和DELETE方法,这种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性。REST架构尤其适用于完全无状态的CRUD(Create、Read、Update、Delete,创建、读取、更新、删除)操作。
基于REST的软件体系结构风格(Software Architecture Style)称之为面向资源体系架构(Resource-oriented Architecture,ROA)。按照REST原则设计的软件、体系结构,通常被称为“REST式的”(RESTful)。
服务器端采用J2EE,客户端采用JSP、Flex、JavaFX、AIR等可以直接调用Servlet,其他的实现技术基本上不能直接调用,但是无论是那种客户端,对于基于SOAP的Web服务或者基于RESTful Web服务务都是支持的,如AJAX的 XMLHttpRequest、Flex的HTTPService等。
2.WebService能做什么?
WebService能够做到跨语言、跨操作系统平台进行远程调用技术。
3.WebService基本用法
3.1 简单的 WebService 服务发布
3.1.1 服务定义
@WebService
public interface HelloWS {
@WebMethod
public String sayHello(String name);
}
3.1.2 服务实现
@WebService
public class HelloWSImpl implements HelloWS {
@Override
public String sayHello(String name) {
System.out.println("server sayHello: " + name);
return "Hello " + name;
}
}
3.1.3 服务发布
public class ServerTest {
public static void main(String[] args) {
String address = "http://192.168.1.139:8989/wsDemo/hellows";
Endpoint.publish(address, new HelloWSImpl());
System.out.println("发布成功");
}
}
3.2 简单的客户端访问服务端
3.2.1 步骤一
根据wsdl文件(http://192.168.1.139:8989/wsDemo/hellows?wsdl)生成客户端文件,我这里使用的IDEA,所以可以直接在文件目录上右键
3.2.2 步骤二
3.2.3 步骤三
3.2.4 调用生成的客户端代码,访问服务端方法
public class ClientTest {
public static void main(String[] args) {
HelloWSImplService factory = new HelloWSImplService();
HelloWSImpl helloWSImpl = factory.getHelloWSImplPort();
System.out.println(helloWSImpl.getClass());
String resulr = helloWSImpl.sayHello("Zard");
System.out.println(resulr);
}
}
4.WebService的CXF框架使用
4.1 导入CXF的依赖jar包,我这里使用的maven
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<!-- cxf -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 使用CXF时,采用JaxWsServerFactoryBean发布服务时需要 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.8</version>
</dependency>
4.2 服务端定义与实现如3.1.1、3.1.2,服务端发布如下(也可以采用原始的Endpoint来发布)
public class Service {
public static void main(String[] args) {
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
factoryBean.setServiceClass(OrderServiceImpl.class);
factoryBean.setAddress("http://localhost/services/orderService");
Server server = factoryBean.create();
server.start();
System.out.println("服务启动成功");
}
}
4.3 客户端从服务端获取数据如下,这种情况需要客户端顶一个与服务端定义一样的借口
public class ClientForCXF {
public static OrderService getInterFace(){
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setServiceClass(OrderService.class);
factoryBean.setAddress("http://localhost/services/orderService");
return (OrderService) factoryBean.create();
}
public static void main(String[] args) {
OrderService service = getInterFace();
Order order = service.getOrder(5);
System.out.println(order.toString());
}
}
5.WebService结合Spring的使用
5.1 配置Web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<!-- Spring启动 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- CXFServlet -->
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5.2 配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<context:component-scan base-package="rest" use-default-filters="true"/>
<context:component-scan base-package="soap" use-default-filters="true"/>
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- soap webService -->
<jaxws:endpoint id="orderService" implementorClass="soap.OrderServiceImpl" address="/orderService"/>
<!-- restful webService -->
<jaxrs:server id="rs1" address="/rs">
<jaxrs:serviceBeans>
<ref bean="restComponent"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
</jaxrs:providers>
</jaxrs:server>
</beans>
5.3 配置Tomcat
添加Tomcat容器,然后把项目发布上去。
6.WebService、Spring、Restful的结合使用
在applicationContext中已经设置了Restful
<!-- restful webService -->
<jaxrs:server id="rs1" address="/rs">
<jaxrs:serviceBeans>
<ref bean="restComponent"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
</jaxrs:providers>
</jaxrs:server>
cxf Restful 服务端的定义:
@Component("restComponent")
public class RestfulTest1 {
@GET
@Path("/getOrder")
@Produces({MediaType.APPLICATION_JSON})
public Order getOrder(){
return new Order(1024L, "Zard", "蜂蜜柚子茶", 2);
}
}
结合web.xml,applicationContext.xml与服务端,Restful的最终访问路径为:
http://localhost/services/rs/getOrder
返回结果为:
{
"orderId": 1024,
"customer": "Zard",
"itemName": "蜂蜜柚子茶",
"itemCount": 2
}
详细我已经上传到Github上了:
不使用CXF的基础情况
使用CXF集成Spring以及Restful实例,也包含了原始的Soap