Java Web Services Up and Running 2nd阅读笔记
快速入门
web service是如何发展起来的?
为了解决开发的易用性,轻量级,语言无关性。
XML-RPC 基于HTTP传输,XML格式的文本,轻量级
重量级的Distributed Object Architecture 如J2EE, 基于java RMI (远程方法调用) , import java.util.List; public interface BenefitsService extends java.rmi.Remote { public List getBenefits(Emp emp) throws RemoteException; }
client端维护一个stub, 客户端需要服务端一样的POJO类, 使用marshaling/unmarshalingWeb Service的简化操作
客户端和服务端基于HTTP协议交换XML数据,不要求相同的语言实现,你可以是面向对象语言,面向过程函数,或者其他方式。
non-java types ---> convert to XML ---> Convert to Java,
为何REST-STYLE SERVICES很流行?**
为了降低逐渐增加的SOAP的复杂性
内容载体更丰富
REST and SOAP are quite different. SOAP is a messaging protocol in which the messages
are XML documents, whereas REST is a style of software architecture for distributed
hypermedia systems, or systems in which text, graphics, audio, and other media are
stored across a network and interconnected through hyperlinks.
HTTP既是传输协议,又是消息系统。所负载的消息通过MIME(Multipurpose Internet Mail Extension) type指定不同的格式,如text/html, application/octet-stream, audio/mpeg3等等
HTTP提供返回状态码表明请求的处理结果
对于Rest风格的web service系统,Client端做两件事:
a) 通过URL命名目标资源
b) 指定verb (HTTP method)
Table 1-2. HTTP verbs and their CRUD operations
HTTP verb | CRUD operation |
---|---|
POST | Create |
GET | Read |
PUT | Update |
DELETE | Delete |
The Restful web service APIs include:
• HttpServlet and its equivalents (e.g., JSP scripts)
• JAX-RS, which has various implementations
• Restlet, which is similar in style to JAX-RS
• JAX-WS @WebServiceProvider, which is a relatively low-level API
@GET, @POST @PUT @DELETE
JAW-WS is an API used mostly for SOAP-based services but can be used for REST-style
services as well.
annotation | scope |
---|---|
@WebServiceProvider | for both SOAP/RESTFUL, lower-level API |
@WebService | for SOAP (JAX-WS) only |
@GET, @POST, @PUT, @DELETE | for JAX-RS/ Restlet |