您的位置:首页 > 运维架构 > Tomcat

restful web service [helloworld] [intellij idea] [JavaEE5] [Maven] [Tomcat]

2017-10-17 19:06 726 查看

维基百科 restful web 服务介绍

https://en.wikipedia.org/wiki/Representational_state_transfer

表征状态转移(REST)或RESTful Web服务是在因特网上的计算机系统之间提供互操作性的一种方式。符合REST的Web服务允许请求系统使用统一和预定义的一组无状态操作访问和操作Web资源的文本表示。存在其他形式的Web服务,它们暴露了自己的任意操作集,如WSDL和SOAP。[1]

“Web资源”首先在万维网上定义为通过其URL标识的文档或文件,但是今天它们具有更加通用和抽象的定义,涵盖可以被识别,命名,寻址或处理的任何东西或实体无论如何,在网上。在RESTful Web服务中,向资源URI发出的请求将引发可能是XML,HTML,JSON或其他定义格式的响应。响应可以确认已经对所存储的资源进行了一些改变,并且它可以提供与其他相关资源或资源集合的超文本链接。使用HTTP,最常见的是,可用的操作类型包括由HTTP方法 GET,POST,PUT,DELETE等预定义的操作。

通过使用无状态协议和标准操作,REST系统旨在通过重新使用可以管理和更新的组件来快速实现性能,可靠性和增长能力,而不会影响系统的整体运行。

术语表述性状态转移被引入,并在2000年定义罗伊菲尔丁在他的博士论文。[2] [3] Fielding使用REST来设计HTTP 1.1和统一资源标识符(URI)。[4] [5] [6]该术语旨在唤起设计良好的Web应用程序的行为形象:它是Web资源(虚拟状态机)的网络,用户通过选择进行应用程序链接,例如/user/tomGET或DELETE(状态转换)等操作,导致下一个资源(表示应用程序的下一个状态)被传送给用户供其使用。

开始项目

intellij idea

http://blog.csdn.net/qqduxingzhe/article/details/76971479

maven windows安装

http://blog.csdn.net/qqduxingzhe/article/details/78230610

设置Maven中央仓库 国内镜像 [本地仓库]

http://blog.csdn.net/qqduxingzhe/article/details/78268646



next



next

finish

解决intellij idea maven 下载lib 慢的问题

http://www.jianshu.com/p/9a1322d9b6e4

选中项目名称,右键 add frameworks support,

添加web application

Javaee version:Javaee 5(选择)



此时项目结构



修改pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<groupId>com.test</groupId>
<artifactId>restful_demo_01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging><!-- 当前为web项目,需要打包为war -->
<name>restful_demo_01</name>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet-core -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.26-b03</version><!-- 2.26可能出个异常,替换为2.26-b03 -->
</dependency>

</dependencies>

</project>


修改web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.example</param-value>
<!-- javaee5,2.5版本,需要   ==》  指定蓝色java文件夹下的包名,一定要有包名 -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>


添加helloworld.java

intellij idea,生成(不推荐生成)

package com.example;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
* @Path 指URI的访问路径,指定的类,必须至少有一个方法
*/
@Path("hello")
public class HelloWorld {

/**
* @GET,指get方式访问
* @Produces,指产出类型,有json、xml、text等等
*
* @return
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getClichedMessage() {
return "restful demo 01 hello";
}

}


配置tomcat



部署war包



访问 http://localhost:8080/hello



纳闷问题

访问http://localhost:8080/index.jsp,会报404,资源不存在

参考来源

java restful web service 【pdf

https://cn.bing.com/search?q=java+restful+web+service%E5%AE%9E%E6%88%98+pdf

Java Restful Web Service实战》源代码

https://github.com/feuyeux/jax-rs2-guide

相关点

http://www.xlgps.com/article/23816.html

Tomcat 版本和javaEE版本的關系
Tomcat版本    Servlet/JSP版本   JavaEE版本
5.0        2.3/1.9            1.3
5.5        2.4/2.0            1.4
6.0        2.5/2.1            5.0
7.0        3.0/2.2            6.0


servlet 3.x 就选择 jersey-container-servlet maven包

https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet

Jersey Container Servlet
Jersey core Servlet 3.x implementation


servlet 2.x 就选择 jersey-container-servlet-core maven包

https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet-core

Jersey Container Servlet Core
Jersey core Servlet 2.x implementation


end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: