WebService的两种实现方式,Soap WebService、Restful WebService。
RPC refers to Remote Procedure Call. Use of RPC is recommended when there is heavy use of the client/server model. RPC allows for the processing of multiple threads that share a given address. RPC employed on a platform that uses EJB. Web Service used in non-Java platforms when an app wants access. Web Service also is used for synchronization of asynchronous communication.[1]
WebService是一种技术,有两种实现方式:JAX-WS(Java API for XML-Based Service面向消息)、JAX-RS(Java API for Restful WebService面向资源) ps. RESTful 请求常用的方法有以下四种:
GET: 用于查询对象
POST: 用于创建对象
PUT: 用于修改对象
DELETE: 用于删除对象
1. CXF 基于 SOAP 的 WebService 1 2 3 4 5 6 7 8 <!-- IHelloWorld.java --> package top.leezy.www;import javax.jws.WebService;@WebService public interface IHelloWorld { String sayHello (String name) ; }
1 2 3 4 5 6 7 8 <!-- HelloWorldImpl.java --> package top.leezy.www;public class HelloWorldImpl implements IHelloWorld { public String sayHello (String name) { return "Hello " + name; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?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:tx ="http://www.springframework.org/schema/tx" xmlns:jaxws ="http://cxf.apache.org/jaxws" xmlns:cxf ="http://cxf.apache.org/core" xmlns:wsa ="http://cxf.apache.org/ws/addressing" xsi:schemaLocation ="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.1.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" > <import resource ="classpath:META-INF/cxf/cxf.xml" /> <import resource ="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxws:endpoint id ="HelloWorld" implementor ="top.leezy.www.HelloWorldImpl" address ="/sayHello" /> </beans >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns ="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation ="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id ="WebApp_ID" version ="3.1" > <display-name > CXFwebservice</display-name > <welcome-file-list > <welcome-file > index.html</welcome-file > </welcome-file-list > <context-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:top/**/applicationContext.xml</param-value > </context-param > <listener > <listener-class > org.springframework.web.context.ContextLoaderListener</listener-class > </listener > <servlet > <servlet-name > CxfServlet</servlet-name > <servlet-class > org.apache.cxf.transport.servlet.CXFServlet</servlet-class > <load-on-startup > 1</load-on-startup > </servlet > <servlet-mapping > <servlet-name > CxfServlet</servlet-name > <url-pattern > /webservice/*</url-pattern > </servlet-mapping > <filter > <filter-name > encoding</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 > encoding</filter-name > <url-pattern > *.jsp</url-pattern > </filter-mapping > <filter-mapping > <filter-name > encoding</filter-name > <url-pattern > *.do</url-pattern > </filter-mapping > </web-app >
打开浏览器输入:http://127.0.0.1:8081/SoapWebService/webservice
2. CXF 基于 RestFul 的 WebService 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <!-- Config.java --> package top.leezy.www;import java.util.LinkedList;import java.util.List;public class Config { public static List<User> users; static { users = new LinkedList<User>(); User user = new User(); user.setId("123456" ); user.setName("SAKURA" ); users.add(user); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <!-- User.java --> package top.leezy.www;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "User") public class User { private String name; private String id; public String getName () { return name; } public void setName (String name) { this .name = name; } public String getId () { return id; } public void setId (String id) { this .id = id; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 <!-- UserService.java --> package top.leezy.www;import javax.ws.rs.Consumes;import javax.ws.rs.DELETE;import javax.ws.rs.GET;import javax.ws.rs.POST;import javax.ws.rs.PUT;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.PathParam;import javax.ws.rs.QueryParam;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.Response;import javax.ws.rs.core.Response.Status;@Path("/UserService") @Produces({"application/json", "application/xml"}) public class UserService { @GET @Path("/getUser/{id}") @Produces(MediaType.APPLICATION_XML) public User getUser (@PathParam("id") String id) { if (id != null && id.length() > 0 ) { for (User user : Config.users) { if (id.equals(user.getId())) return user; } User add_user = new User(); add_user.setId(id); return add_user; } else { return new User(); } } @POST @Path("/regUser") @Consumes({"application/json", "application/xml"}) public Response regUser (User user) { if (Config.users.contains(user)) { return Response.status(Status.BAD_REQUEST).build(); } else { Config.users.add(user); return Response.ok("id = " + user.getId() + ", name = " + user.getName()).build(); } } @DELETE @Path("/delUser") @Consumes({"application/json", "application/xml"}) public Response delPerson (@QueryParam("id") String id) { User user = new User(); user.setId(id); if (Config.users.contains(user)) { return Response.status(Status.BAD_REQUEST).build(); } else { Config.users.remove(user); return Response.ok(user).build(); } } @PUT @Path("/updateUser") @Consumes({"application/json", "application/xml"}) public Response updateUser (User user) { if (Config.users.contains(user)) { return Response.status(Status.BAD_REQUEST).build(); }else { for (User old_user : Config.users) { if (old_user.equals(user)) { old_user.setName(user.getName()); } } return Response.ok(user).build(); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?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:jaxrs ="http://cxf.apache.org/jaxrs" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd" > <import resource ="classpath:META-INF/cxf/cxf.xml" /> <import resource ="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id ="userService" class ="top.leezy.www.UserService" /> <jaxrs:server id ="rs_server" address ="/restfulService" > <jaxrs:serviceBeans > <ref bean ="userService" /> </jaxrs:serviceBeans > </jaxrs:server > </beans >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns ="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id ="WebApp_ID" version ="3.0" > <display-name > CxfRestWebservice</display-name > <welcome-file-list > <welcome-file > index.html</welcome-file > </welcome-file-list > <context-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:top/**/beans.xml</param-value > </context-param > <listener > <listener-class > org.springframework.web.context.ContextLoaderListener</listener-class > </listener > <servlet > <servlet-name > cxfservlet</servlet-name > <servlet-class > org.apache.cxf.transport.servlet.CXFServlet</servlet-class > <load-on-startup > 1</load-on-startup > </servlet > <servlet-mapping > <servlet-name > cxfservlet</servlet-name > <url-pattern > /webservice/*</url-pattern > </servlet-mapping > </web-app >
打开浏览器输入:http://127.0.0.1:8081/RestFulWebService/webservice/ 使用POSTMan进行测试: RestFul WebService 的JAR包较 Soap WebService 要区别以下几个: -javax.ws.rs-api-2.1.1.jar -cxf-rt-frontend-jaxrs-3.2.7.jar
参考文献: [1]http://www.differencebetween.net/technology/protocols-formats/difference-between-rpc-and-web-service/[[ixzz5YLemRQhd]] [2]https://www.cnblogs.com/zhuyiqizhi/p/6213502.html