Welcome to the Java Web Services Tutorial. Here we will learn about web services, useful concepts in web services and then different types of API we have in Java to create web services.
欢迎使用Java Web Services教程 。 在这里,我们将学习Web服务 , Web服务中的有用概念以及Java中用于创建Web服务的不同类型的API。
什么是网络服务 (What is a Web Service)
In simple words, services that can be accessed over network are called web services. So how does it differ from web application, they are also services that are accessed over network. There are few attributes that clarifies this difference.
Web applications are meant for users and to be accessed in browser having human readable format whereas web services are meant for applications to access data in the format of XML, JSON etc. Web应用程序供用户使用,并在具有人类可读格式的浏览器中进行访问,而Web服务则供应用程序以XML,JSON等格式访问数据。
Web applications always use HTTP/HTTPS protocol whereas traditional web services use SOAP protocol. Recently REST is getting popularity that is an architecture style and almost all times run on HTTP/HTTPS protocol. Web应用程序始终使用HTTP / HTTPS协议,而传统的Web服务使用SOAP协议。 最近,REST越来越流行,它是一种体系结构样式,几乎所有时间都在HTTP / HTTPS协议上运行。
Web applications are not meant for reusability whereas this is one of the benefit of web services. A single web service can be used by different kinds of applications. Web应用程序并非旨在提供可重用性,但这是Web服务的优势之一。 单个Web服务可由不同类型的应用程序使用。
Web application can access web services to access some data or to perform some tasks, web services can’t access web applications to fetch some data. Web应用程序可以访问Web服务以访问某些数据或执行某些任务,Web服务不能访问Web应用程序以获取一些数据。
Web applications are capable to maintain user session, web services are stateless. Web应用程序能够维护用户会话,Web服务是无状态的。
I hope above differences are good enough to clear any confusion with web applications and web services. Both are different concepts and meant for different purpose.
SOAP: SOAP stands for Simple Object Access Protocol. SOAP is an XML based industry standard protocol for designing and developing web services. Since it’s XML based, it’s platform and language independent. So our server can be based on JAVA and client can be on .NET, PHP etc. and vice versa. SOAP:SOAP代表简单对象访问协议。 SOAP是用于设计和开发Web服务的基于XML的行业标准协议。 由于它基于XML,因此与平台和语言无关。 因此,我们的服务器可以基于JAVA,客户端可以基于.NET,PHP等,反之亦然。
REST: REST is an architectural style for developing web services. It’s getting popularity recently because it has small learning curve when compared to SOAP. Resources are core concepts of Restful web services and they are uniquely identified by their URIs. REST:REST是用于开发Web服务的体系结构样式。 由于与SOAP相比,它的学习曲线很小,因此最近变得越来越流行。 资源是Restful Web服务的核心概念,它们由其URI唯一标识。
Java Web服务 (Java Web Services)
Java provides it’s own API to create both SOAP as well as REST web services.
Java提供了自己的API来创建SOAP和REST Web服务。
JAX-WS: JAX-WS stands for Java API for XML Web Services. JAX-WS is XML based Java API to build web services server and client application. JAX-WS:JAX-WS代表XML Web Services的Java API。 JAX-WS是基于XML的Java API,用于构建Web服务服务器和客户端应用程序。
JAX-RS: Java API for RESTful Web Services (JAX-RS) is the Java API for creating REST web services. JAX-RS uses annotations to simplify the development and deployment of web services. JAX-RS:用于RESTful Web服务的Java API(JAX-RS)是用于创建REST Web服务的Java API。 JAX-RS使用注释来简化Web服务的开发和部署。
Both of these APIs are part of standard JDK installation, so we don’t need to add any jars to work with them. Both of these APIs use annotations very heavily.
Hello World JAX-WS应用程序 (Hello World JAX-WS Application)
Let’s create a very simple Hello World JAX-WS application.
让我们创建一个非常简单的Hello World JAX-WS应用程序。
TestService.java
TestService.java
package com.journaldev.jaxws.service;import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.xml.ws.Endpoint;@WebService @SOAPBinding(style = SOAPBinding.Style.DOCUMENT) public class TestService {@WebMethodpublic String sayHello(String msg){return "Hello "+msg;}public static void main(String[] args){Endpoint.publish("https://localhost:8888/testWS", new TestService());} }
That’s it. Just run this application and our Hello World JAX-WS SOAP web service is published. Below image shows the invocation of this JAX-WS web service through SOAP UI.
而已。 只需运行此应用程序,我们的Hello World JAX-WS SOAP Web服务就会发布。 下图显示了通过SOAP UI对该JAX-WS Web服务的调用。
That’s it for a very basic tutorial of JAX-WS web service. Below are some of the articles you should read for better understanding of SOAP web services and JAX-WS.
JAX-WS Web Service Deployment on Tomcat Tomcat上的JAX-WS Web服务部署
SOAP Web Service Example using Eclipse and Apache Axis 使用Eclipse和Apache Axis的SOAP Web服务示例
Apache Axis 2 Web Services Tutorial Apache Axis 2 Web服务教程
Hello World JAX-RS应用程序 (Hello World JAX-RS Application)
Jersey is the reference implementation of JAX-RS API, it’s not part of standard JDK and we have to include all the required jars. Best way is to use Maven build, so create a simple Dynamic web project and then convert it to Maven in Eclipse.
That’s all for a quick introduction of java web services, finally if you are preparing for any interview then go through Web Services Interview Questions.