java web 教程

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服务。 因此,它与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应用程序供用户使用,并在具有人类可读格式的浏览器中进行访问,而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.

我希望上述差异足以消除与Web应用程序和Web服务的任何混淆。 两者都是不同的概念,并且具有不同的用途。

Web服务的类型 (Types of Web Services)

There are two types of web services.

Web服务有两种类型。

  1. 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等,反之亦然。
  2. 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服务。

  1. 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服务服务器和客户端应用程序。
  2. 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.

这两个API都是标准JDK安装的一部分,因此我们不需要添加任何jar即可使用它们。 这两个API都非常频繁地使用注释。

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服务教程。 以下是您应该阅读的一些文章,以更好地理解SOAP Web服务和JAX-WS。

  1. JAX-WS TutorialJAX-WS教程
  2. JAX-WS Web Service Deployment on TomcatTomcat上的JAX-WS Web服务部署
  3. SOAP Web Service Example using Eclipse and Apache Axis使用Eclipse和Apache Axis的SOAP Web服务示例
  4. Apache Axis 2 Web Services TutorialApache 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.

Jersey是JAX-RS API的参考实现,它不是标准JDK的一部分,我们必须包含所有必需的jar。 最好的方法是使用Maven构建,因此创建一个简单的Dynamic Web项目,然后在Eclipse中将其转换为Maven。

Here is the final pom.xml file having required dependencies.

这是具有必需依赖项的最终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>

Now add Jersey servlet to our deployment descriptor web.xml as front controller.

现在,将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.journaldev.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>

Above two steps are required for initial setup, below is our Hello World JAX-RS service class.

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

package com.journaldev.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;}
}

Just export it as WAR file and then access it in the browser as shown in below image.

只需将其导出为WAR文件,然后在浏览器中对其进行访问,如下图所示。

You can change the last part of URL and returned message will change accordingly.

您可以更改URL的最后一部分,返回的消息也将相应更改。

You can see how easy it was to create RESTful web service using JAX-RS API. However there is more to it, follow below articles to learn more.

您可以看到使用JAX-RS API创建RESTful Web服务是多么容易。 但是,还有更多内容,请按照以下文章了解更多信息。

  1. Restful Web Services宁静的Web服务
  2. RESTEasy TutorialRESTEasy教程
  3. Jersey Tutorial泽西教程

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.

这就是快速介绍Java Web服务的全部内容 ,最后,如果您准备进行任何面试,请阅读Web服务面试问题 。

References: JAX-WS Oracle Page, JAX-RS Oracle Page

参考: JAX-WS Oracle Page , JAX-RS Oracle Page

翻译自: https://www.journaldev.com/9191/java-web-services-tutorial

java web 教程

java web 教程_Java Web服务教程相关推荐

  1. java web环境_java web

    HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException HTTP ...

  2. java servlet 教程_Java Servlet完全教程

    Servlet 是一些遵从Java Servlet API的Java类,这些Java类可以响应请求.尽管Servlet可以响应任意类型的请求,但是它们使用最广泛的是响应web方面的请求. Servle ...

  3. java 实现 web 客户端_Java web客户端和服务器端交互的原理

    Java web客户端和服务器端交互的原理 其实HTTP客户端和服务器端的交互原理很简单:即先是浏览器和服务器端建立Socket无状态连接,也就是短连接,然后通过IO流进行报文信息(这个报文是严格遵循 ...

  4. java filter教程_Java Web Filter 过滤器学习教程(推荐)

    一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态 ...

  5. java web应用开发渐进教程_Java Web应用开发渐进教程

    第1章 Java Web概述 1.1 HTTP与Web页 1.1.1 HTTP 1.1.2 静态Web页 1.1.3 动态Web页 1.2 Java服务器端开发相关技术 1.2.1 Servlet技术 ...

  6. c mysql web开发实例教程_java web 开发入门实例

    学习是个技巧活,关键是要找到重点的地方,新手在这方面的坑尤其多.看别人的教程一步一步的跟着做,隔几步就遇到一个新知识点,忍不住就百度往深处了解,一晃半天就过去了. 有的知识点要深入学习的,有的是了解下 ...

  7. java web 乱码_java web乱码解决方法

    设置文件的保存编码格式,这个一般在新建项目时,设置了GBK的编码格式,这时文件的页面编码格式是GBK的,而保存格式是ANSI的,后来把项目及文件的编码格式改为utf-8的编码格式后,依然出现中文乱码, ...

  8. java 分布式同步_Java Web分布式集群搭建(三)——Session同步

    对于一个业务系统的Tomcat集群来说,必须保证同一个用户访问到任一台服务器上都可以维持之前操作的身份.比如在服务器A进行了登陆,那么在服务器B中也要同步该用户已登录的状态,这里就用到了Session ...

  9. java web后台_java web 后台那些事

    java web 后台运行原理 当Web服务器接收到一个HTTP请求时,它会先判断请求内容--如果是静态网页数据,Web服务器将会自行处理,然后产生响应信息:如果牵涉到动态数据,Web服务器会将请求转 ...

最新文章

  1. acme自动证书申请
  2. 针对“永恒之蓝(WannaCry)”攻击紧急处置手册
  3. 《Flask 入门教程》第 6 章:模板优化
  4. 需要自己调研的框架,以及需要学习的内容
  5. param注解报错_mybatis不加@Parm注解报错的解决方案
  6. ie浏览器发送错误报告提示怎么关闭
  7. 为提升效率,阿里取消周报制度?醒醒吧,自动化报表才是出路
  8. 国产特斯拉Model Y已开始向欧洲用户交付
  9. 什么时候出来的_DNF手游官网正版什么时候出来?地下城手游上线日期分享
  10. oracle sql 执行计划分析_从Oracle数据库实验来看索引的常见执行计划
  11. 微软推出免费虚拟太空望远镜软件WWT
  12. VS2013默认打开HTML文件没有设计视图的解决办法
  13. python遍历链表_python实现双向循环链表
  14. 40家央企数字化转型路线图公布(2022最新版)
  15. 哈佛大学公开课-幸福课-个人笔记
  16. 苹果html向上滑动不流畅,苹果手机Safari浏览器下滑动卡顿的问题
  17. 相较于本地渲染,云渲染用起来感觉怎么样?
  18. Typecho权限管理插件 - 权限狗
  19. 转转转转转转转转转转转转转转转转转转转转转转转转转
  20. 使用键盘发布手柄控制信息

热门文章

  1. 1.Web前端之CSS3中3D立方体以及3D轮播图
  2. 主题切换时如何主动去刷新一些资源?
  3. Android ViewPager 里有子ViewPager的事件冲突
  4. excel批量导入数据
  5. 最多只能选择两个多选框的jQuery功能实现
  6. [转载] python选择排序二元选择_选择排序:简单选择排序(Simple Selection Sort)
  7. [转载] python indices_Python numpy.indices() 使用实例
  8. Substance PBR Guide
  9. java获取当前行数
  10. Maximal Binary Matrix CodeForces - 803A (贪心+实现)