Java Web服务完整教程:从入门到精通

欢迎来到Java Web服务教程。在这里,我们将学习有关Web服务的知识,了解Web服务中的一些有用概念,然后介绍Java中用于创建Web服务的不同类型API。

什么是网络服务 shì ?

简而言之,可以通过网络访问的服务被称为Web服务。那么它与Web应用程序有何不同?它们也是可以通过网络访问的服务。有几个属性可以解释这种区别。

  • 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 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 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 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 applications are capable to maintain user session, web services are stateless.

希望上述差异足以清楚地解释网页应用程序和网页服务之间的混淆。它们是不同的概念,用于不同的目的。

网页服务的种类

有两种类型的网络服务。

    SOAP: SOAP代表简单对象访问协议。SOAP是一种基于XML的工业标准协议,用于设计和开发网络服务。由于它是基于XML的,它不依赖于平台和语言。因此,我们的服务器可以基于JAVA,客户端可以是.NET,PHP等,反之亦然。
    REST: REST是一种用于开发网络服务的体系结构风格。近年来,它因其学习曲线相对较小而变得流行起来。资源是Restful网络服务的核心概念,它们通过其URI唯一标识。

Java网络服务

Java提供了自己的API来创建SOAP和REST网络服务。

    JAX-WS: JAX-WS代表Java API for XML Web Services。JAX-WS是基于XML的Java API,用于构建Web服务的服务器和客户端应用程序。
    JAX-RS: Java API for RESTful Web Services(JAX-RS)是用于创建RESTful Web服务的Java API。JAX-RS使用注释来简化Web服务的开发和部署。

这两个API都是标准JDK安装的一部分,所以我们不需要添加任何JAR文件来使用它们。这两个API都大量使用了注解。

你好,世界 JAX-WS 应用程序

让我们创建一个非常简单的Hello World JAX-WS应用程序。TestService.java

package com.Olivia.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 {

	@WebMethod
	public String sayHello(String msg){
		return "Hello "+msg;
	}
	
	public static void main(String[] args){
		Endpoint.publish("https://localhost:8888/testWS", new TestService());
	}
}

就这样。只需运行这个应用程序,我们的Hello World JAX-WS SOAP网络服务就会被发布。下面的图片展示了通过SOAP UI调用这个JAX-WS网络服务。这就是关于JAX-WS网络服务的一个基础教程。以下是一些你应该阅读以更好理解SOAP网络服务和JAX-WS的文章。

    JAX-WS教程
    在Tomcat上部署JAX-WS Web服务
    使用Eclipse和Apache Axis的SOAP Web服务示例
    Apache Axis 2 Web服务教程

你好世界 JAX-RS 应用程序

Jersey是JAX-RS API的参考实现,它不是标准JDK的一部分,我们需要包含所有必需的jar。最好的方式是使用Maven构建,在Eclipse中创建一个简单的动态Web项目,然后将其转换为Maven项目。这是最终的pom.xml文件,包含所需的依赖项。

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>JAX-RS-HelloWorld</groupId>
	<artifactId>JAX-RS-HelloWorld</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	
	<dependencies>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-server</artifactId>
			<version>1.19</version>
		</dependency>
		<dependency>
			<groupId>com.sun.jersey</groupId>
			<artifactId>jersey-servlet</artifactId>
			<version>1.19</version>
		</dependency>
	</dependencies>
	
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

现在将Jersey servlet添加到我们的部署描述符web.xml中作为前端控制器。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xmlns="https://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">
	<display-name>JAX-RS-HelloWorld</display-name>

	<servlet>
		<servlet-name>Jersey REST Service</servlet-name>
		<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
		<init-param>
			<param-name>com.sun.jersey.config.property.packages</param-name>
			<param-value>com.Olivia.jaxrs.service</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>Jersey REST Service</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app>

上述两个步骤是初始设置所必需的,下面是我们的Hello World JAX-RS服务类。

package com.Olivia.jaxrs.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

@Path("/test")
public class TestService {

	@GET
	@Path("/hello/{msg}")
	public String sayHello(@PathParam(value="msg") String msg){
		return "Hello "+msg;
	}
}

只需将其导出为WAR文件,然后按照下面的图片所示在浏览器中访问它。您可以更改URL的最后一部分,并相应地更改返回的消息。您可以看到使用JAX-RS API创建RESTful Web服务有多么简单。然而,还有更多内容,请阅读下方的文章以了解更多信息。

    “RESTful网络服务”
    “RESTEasy教程”
    “Jersey教程”

这就是关于Java Web Services的简介,最后如果你正在准备面试,可以参考Web Services面试题。参考资料:JAX-WS Oracle页面,JAX-RS Oracle页面。

bannerAds