java web 教程_Java Web服务教程

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 {

	@WebMethod
	public 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 Tutorial

    JAX-WS教程
  2. JAX-WS Web Service Deployment on Tomcat

    Tomcat上的JAX-WS Web服务部署
  3. SOAP Web Service Example using Eclipse and Apache Axis

    使用Eclipse和Apache Axis的SOAP Web服务示例
  4. 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.

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 Tutorial

    RESTEasy教程
  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 PageJAX-RS Oracle Page

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

java web 教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<>人民邮电出版社的教程哦,所以,好书,你懂的!! 第1章web应用开发简介 1 1.1何为web应用 1 1.1.1web的概念及发展 1 1.1.2web应用程序 2 1.2使用java开发web应用 3 1.2.1面向对象的编程语言 3 1.2.2丰富的框架技术 4 1.2.3xml、css的应用 4 1.2.4使用javascript与ajax提升用户体验 7 1.3多种集成开发环境 9 1.3.1集成开发环境简介 9 1.3.2web应用服务器说明 11 本章小结 14 课后练习 14 第2章 javaee运行及开发环境 15 2.1jdk的下载与安装 15 2.1.1jdk的下载与安装 15 2.1.2配置环境变量 17 >2.2tomcat的下载与安装 19 2.2.1下载并安装tomcat服务器 19 .2.2.2基本配置 22 2.2.3服务器页面介绍 23 2.3eclipse的下载与安装 24 2.3.1eclipse的下载与安装 24 2.3.2熟悉eclipse开发环境 24 2.4项目实战——第一个javaee应用:helloworld 28 2.4.1开始创建第一个javaee应用 28 2.4.2熟悉helloworld框架结构 29 本章小结 32 课后练习 32 第3章jsp和servlet 33 3.1开发第一个jsp+servlet应用 33 3.1.1创建工程 33 3.1.2编写程序 34 3.1.3部署应用 35 3.2认识jsp 36 3.2.1jsp的工作原理 37 3.2.2jsp注释方式 37 3.2.3jsp声明方式 38 3.2.4jsp表达式的应用 39 3.2.5jsp的脚本段 39 3.2.6jsp的编译指令 40 3.2.7jsp的动作指令 41 3.2.8jsp的内置对象 43 3.3认识servlet 46 3.3.1servlet的开发 46 3.3.2使用httpservlet 47 3.3.3servlet的生命周期 49 3.3.4load-on-startupservlet 50 3.4自定义标签库 51 3.5预处理用户请求:filter 53 3.6使用listener 56 3.7项目实战——用户登录 59 本章小结 62 课后练习 63 第4章sql与jdbc 64 4.1sql 64 4.1.1sql概述 64 4.1.2ddl与dml简介 64 4.1.3sql使用方法 65 4.2jdbc 67 4.2.1jdbc概述 67 4.2.2jdbc驱动程序 68 4.2.3使用jdbc读取数据 69 4.3项目实战——存储图书信息 73 本章小结 78 课后练习 79 第5章struts2框架基础 80 5.1mvc框架 80 5.1.1model1与model2 80 5.1.2mvc设计模式 81 5.1.3struts2框架的mvc架构 82 5.2struts2概览 84 5.2.1struts2的工作流程 84 5.2.2struts2的简单应用 85 5.3struts2基础 87 5.3.1action详解 88 5.3.2结果与视图 91 5.3.3struts.xml的配置 94 5.4值栈与ognl表达式 100 5.5struts2的标签库 103 5.5.1控制标签 103 5.5.2数据标签 104 5.5.3表单标签 105 5.5.4非表单ui标签 107 本章小结 108 课后练习 109 第6章struts2高级应用 110 6.1拦截器 110 6.1.1拦截器工作机制 110 6.1.2拦截器及拦截器栈的应用 111 6.1.3自定义拦截器 115 6.2类型转换器 117 6.2.1struts2内置类型转换器 117 6.2.2引用类型的转换方式 117 6.2.3特殊对象的类型转换 118 6.2.4类型转换的错误处理 121 6.3输入校验 123 6.3.1输入校验的必要性 123 6.3.2编程方式实现输入校验 125 6.3.3应用struts2输入校验框架 128 6.3.4自定义校验器 130 6.4国际化 131 6.4.1国际化实现原理 131 6.4.2准备国际化资源文件 131 6.4.3调用国际化资源文件 134 6.5上传和下载 135 6.5.1文件上传的实现原理 135 6.5.2struts2文件上传实现方式 136 6.5.3struts2文件下载实现方式 141 本章小结 143 课后练习 144 第7章 struts2中应用模板语言 145 7.1模板语言简介 145 7.2应用velocity 146 7.2.1velocity基础 146 7.2.2struts2对velocity的支持 153 7.3应用freemarker 155 7.3.1freemarker基础 155 7.3.2struts2整合freemarker 166 7.3.3使用struts2标签设计模板 170 7.4freemarker与velocity的比较 171 本章小结 171 课后练习 172 第8章hibernate框架基础 173 8.1orm简介 173 8.1.1应用orm的意义 173 8.1.2流行的orm框架 174 8.2准备hibernate运行环境 174 8.2.1下载与安装hibernate 175 8.2.2hibernate发布包介绍 175 8.3认识hibernate 176 8.3.1hibernate框架结构 176 8.3.2hibernate配置文件 177 8.3.3configuration与sessionfactory 178 8.3.4session类 179 8.3.5hibernate中的关联关系 179 8.3.6hibernate映射文件 180 8.3.7hibernate工作原理 182 8.4项目实战——新闻内容显示 183 本章小结 186 课后练习 187 第9章hibernate查询 188 9.1hibernate的数据检索策略 188 9.1.1立即检索 188 9.1.2延迟检索 189 9.1.3预先检索 190 9.1.4批量检索 191 9.2hibernate的数据查询方式 193 9.2.1hql方式 193 9.2.2qbc方式 194 9.2.3原生sql方式 195 9.3hibernate的关联查询 196 9.3.1一对一关联关系的使用 196 9.3.2一对多、多对一关联关系的使用 197 9.3.3多对多关联关系的使用 199 9.4hibernate过滤 201 9.4.1session过滤 201 9.4.2filter过滤 201 9.5项目实战——客户订单管理 202 本章小结 207 课后练习 207 第10章hibernate性能优化 208 10.1hibernate事务与并发 208 10.1.1什么是事务 208 10.1.2hibernate中的事务处理 209 10.1.3在hibernate中使用jta事务 210 10.1.4并发控制 211 10.2hibernate缓存 213 10.2.1缓存的工作原理 213 10.2.2应用一级缓存 214 10.2.3应用二级缓存 214 10.2.4应用第三方缓存 216 10.3项目实战——借还图书 217 本章小结 224 课后练习 224 第11章spring框架基础 226 11.1spring框架概述 226 11.1.1认识spring框架 226 11.1.2spring框架特点 226 11.1.3spring框架核心架构 227 11.2建立spring开发环境 228 11.2.1下载spring框架 228 11.2.2spring发布包与软件包 229 11.2.3创建spring应用环境 230 11.3bean的装配 231 11.3.1bean基本配置 232 11.3.2为bean添加属性 232 11.3.3简化配置 233 11.4理解spring的核心——ioc 234 11.4.1控制反转 234 11.4.2依赖注入的3种方式 236 11.5beanfactory与applicationcontext 238 11.5.1认识beanfactory 238 11.5.2使用applicationcontext 238 11.6项目实战——spring问候程序 239 本章小结 241 课后练习 241 第12章springaop 242 12.1aop基础 242 12.1.1aop与oop的比较 242 12.1.2aop的核心概念 243 12.1.3java动态代理与aop 244 12.1.4springaop简介 245 12.2使用spring的通知 246 12.2.1beforeadvice 246 12.2.2afterreturningadvice 248 12.2.3methodinterceptor 249 12.2.4throwadvice 250 12.3使用spring的切入点 251 12.3.1静态切入点 251 12.3.2动态切入点 253 12.4springaop的代理工厂 253 12.4.1选择合适的代理 253 12.4.2proxyfactory 254 12.4.3proxyfactorybean 254 12.5项目实战——输出日志 256 本章小结 258 课后练习 259 第13章 springjavaee持久化数据访问 260 13.1spring对dao模式的支持 260 13.1.1统一的数据访问异常 260 13.1.2通用的数据访问模板及抽象支持类 261 13.2spring的jdbc 262 13.2.1为什么需要jdbctemplate 262 13.2.2通过jdbcdaosupport使用jdbctemplate 263 13.2.3jdbctemplate提供的常用数据操作方法 264 13.3spring中的事务处理 265 13.3.1spring事务处理概述 266 13.3.2编程式事务处理 266 13.3.3声明式事务处理 267 13.3.4标注式事务处理 268 13.4项目实战——公司人事管理 269 本章小结 276 课后练习 276 第14章spring与struts2、hibernate框架的整合基础 277 14.1spring与struts2的整合方式 277 14.1.1struts2应用的扩展方式 277 14.1.2spring插件的应用 278 14.2spring和hibernate的整合 279 14.2.1spring对hibernate的支持 279 14.2.2管理sessionfactory 279 14.2.3hibernate的dao实现 281 14.2.4使用hibernatetemplate 281 14.2.5管理hibernate事务 282 14.3项目实战——学生成绩查询系统 283 本章小结 292 课后练习 293 第15章图书馆管理系统 294 15.1系统功能解析 294 15.2系统数据库设计 295 15.2.1数据库分析 295 15.2.2数据表关联关系分析 298 15.3系统框架搭建 300 15.3.1创建工程 300 15.3.2工程目录结构 300 15.4系统代码实现 302 15.4.1数据库连接的实现 303 15.4.2工具类的实现 304 15.4.3管理员登录与退出实现 306 15.4.4系统管理实现 313 15.4.5图书管理实现 319 15.4.6读者管理实现 329 15.4.7借还管理实现 333 15.4.8前台功能实现 336 本章小结 342
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值