本文参考以下内容:

[1] Starting out with Jersey & Apache Tomcat using IntelliJ

[2] 【Jersey】IntelliJ IDEA + Maven + Jetty + Jersey搭建RESTful服务

  感谢两位作者。

  网上很多文章都是用Jersey 1 搭建的,不能用Jersey 2的新特性,在此我分享一种Jersey 2的搭建方法。

0. 创建新项目

  在IntelliJ中创建新项目,选择Java Enterprise -> RESTful Web Service -> Setup libery later.

1. 加入web框架和maven框架

  右键单击项目名-> Add Frameworks Support,分别勾选Web Application和Maven。

3. 在maven中加入jersey依赖

  在pom.xml中加入:

 <dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.9</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.9</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.9</version>
</dependency>

  此时,整个pom.xml文档如下。另外使用maven默认的库下载源文件很慢,可以使用国内镜像,方法可见maven国内镜像(maven下载慢的解决方法)

  顺便一提,maven默认的java源值、目标值版本是1.5,可以自行修改成1.8,方法见下面代码。

 <?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>cn.test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<name>test</name> <properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.9</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.9</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.9</version>
</dependency>
</dependencies>
</project>

4. 创建源文件

  在src/java目录下新建包,如com.test.jersey,在包下新建类HelloWorld.java,写上代码:

package com.test.jersey;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces; @Path("/hello")
public class HelloWorld {
//GET注解设置接受请求类型为GET
@GET
//Produces表明发送出去的数据类型为text/plain
// 与Produces对应的是@Consumes,表示接受的数据类型为text/plain
@Produces("text/plain")
public String getMessage() {
return "Hello world!";
}
}

5. 配置servlet

  编辑web/WEB-INF/web.xml,加入代码:

<servlet>
<servlet-name>test</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.test.jersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

6. 配置Tomcat

  点击Run >Edit Configurations… > “+” > Tomcat Server > Local,加入Tomcat,选择Deployment tab, 点击 “+”, 选择唯一的Artifact,点击"OK"即可。

7. 在输出中加入库文件

  选择Project Structure,点击Artifacts,可以右侧Available Elements下面有很多库文件没有包含在输出中。依次双击各个文件即可。

8. 运行Tomcat

  运行Tomcat,在浏览器中输入http://localhost:8080/hello,即可看到以下输出:

Jersey 2 + Maven + Tomcat + IntelliJ IDEA 搭建RESTful服务的更多相关文章

  1. Maven + Jetty + Jersey搭建RESTful服务

    IntelliJ IDEA + Maven + Jetty + Jersey搭建RESTful服务 本文参考以下内容: 使用Jersey实现RESTful风格的webservice(一) Starti ...

  2. jersey2.26+spring5+jpa一步步搭建restful服务

    前言 首先,为什么想选择Jersey做restful服务呢?我个人比较喜欢它的插件化设计,可以方便的注入自己的全局处理逻辑.再一个就是可以生成wadl描述文件,供查询服务方法.所以在学习spring的 ...

  3. 【Jersey】IntelliJ IDEA + Maven + Jetty + Jersey搭建RESTful服务

    本文参考以下内容: 使用Jersey实现RESTful风格的webservice(一) Starting out with Jersey & Apache Tomcat using Intel ...

  4. IntelliJ IDEA + Maven + Jetty + Jersey搭建RESTful服务

    这次参考的是这个博客,完全按照这个我这里会出一些问题,一会再说就是了. https://www.cnblogs.com/puyangsky/p/5368132.html 一.首先新建一个项目,选择Ja ...

  5. elipse + maven + tomcat + springMVC环境搭建

    1. java JDK安装 http://jingyan.baidu.com/article/b24f6c82c989da86bfe5dab2.html 2.eclipse安装 http://jing ...

  6. Maven+Spirng+Mybatis+CXF搭建WebService服务

    https://jingyan.baidu.com/article/39810a23b1de2fb637fda66c.html

  7. [转]php,使用Slim和Medoo搭建简单restful服务

    本文转自:https://www.jianshu.com/p/32b6d0284d97 关于rest和orm框架的组合使用,几年前我就写过使用Slim+Notorm来搭建restful服务,不过看到N ...

  8. 基于jersey和Apache Tomcat构建Restful Web服务(一)

    基于jersey和Apache Tomcat构建Restful Web服务(一) 现如今,RESTful架构已然成为了最流行的一种互联网软件架构,它结构清晰.符合标准.易于理解.扩展方便,所以得到越来 ...

  9. Jersey+Spring+Maven环境搭建

    第一步:创建一个Maven工程.加入Jersey.Spring.Jersey+Spring的依赖包,以及内嵌的Tomcat7插件: pom.xml文件如图所示: <project xmlns=& ...

随机推荐

  1. jQuery File Upload 单页面多实例的实现

    jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload ...

  2. uwsgi 安装配置

    安装uwsgi# pip install uwsgi 配置uwsgi:首先要明确的是,如果你喜欢用命令行的方式(如shell)敲命令,那可以省去任何配置.但是,绝大多数人,还是不愿意记那么长的命令,反 ...

  3. C#中图片新增水印

    /// <summary> /// 在图片上生成图片水印 /// </summary> /// <param name="Path">原服务器图 ...

  4. Ionic + AngularJS

    Ionic Framework Ionic framework is the youngest in our top 5 stack, as the alpha was released in lat ...

  5. Vue 2.0 + Vue Router + Vuex

    用 Vue.js 2.x 与相配套的 Vue Router.Vuex 搭建了一个最基本的后台管理系统的骨架. 当然先要安装 node.js(包括了 npm).vue-cli 项目结构如图所示: ass ...

  6. java类初始化的过程

    在复习Thinking in java的过程中看到了相关内容,顺便整理一下,像下面一样的代码具体的执行顺序(ABCD都是类) public class A { public A(String text ...

  7. [原创]-bash: iostat: command not found解决办法

    [root@testhost ~]# iostat-bash: iostat: command not found IOSTAT 命令不可用,首先确认sysstat包是否安装,sysstat包中包括i ...

  8. 百度富文本编辑器UEDITOR

    前言 http://jingyan.baidu.com/article/a948d65108d7fa0a2dcd2e8d.html 配置<a>测试啊</a>.net mvc4项 ...

  9. MWeb 1.7.1 版发布!支持导出为 RTF 和 Docx、发布到 Evernote 带样式、文档库备份和新网站主题等大量改进!

    编辑器及发布服务改进 增加设置图片宽度和居左.居右.居中的语法. 如:![图片说明-w450](pic.jpg) 这样表示设置图片宽度为 450.其中 -w450 为设置语法,生成 HTML 时会自动 ...

  10. CSS深入理解之margin

      前言   改变容器尺寸   margin改变容器尺寸有以下两个限制条件: 使用于没有设置宽高的普通block元素float/absolute/fixed元素 以及 inlines水平 table- ...