Jersey 2 + Maven + Tomcat + IntelliJ IDEA 搭建RESTful服务
本文参考以下内容:
[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服务的更多相关文章
- Maven + Jetty + Jersey搭建RESTful服务
IntelliJ IDEA + Maven + Jetty + Jersey搭建RESTful服务 本文参考以下内容: 使用Jersey实现RESTful风格的webservice(一) Starti ...
- jersey2.26+spring5+jpa一步步搭建restful服务
前言 首先,为什么想选择Jersey做restful服务呢?我个人比较喜欢它的插件化设计,可以方便的注入自己的全局处理逻辑.再一个就是可以生成wadl描述文件,供查询服务方法.所以在学习spring的 ...
- 【Jersey】IntelliJ IDEA + Maven + Jetty + Jersey搭建RESTful服务
本文参考以下内容: 使用Jersey实现RESTful风格的webservice(一) Starting out with Jersey & Apache Tomcat using Intel ...
- IntelliJ IDEA + Maven + Jetty + Jersey搭建RESTful服务
这次参考的是这个博客,完全按照这个我这里会出一些问题,一会再说就是了. https://www.cnblogs.com/puyangsky/p/5368132.html 一.首先新建一个项目,选择Ja ...
- elipse + maven + tomcat + springMVC环境搭建
1. java JDK安装 http://jingyan.baidu.com/article/b24f6c82c989da86bfe5dab2.html 2.eclipse安装 http://jing ...
- Maven+Spirng+Mybatis+CXF搭建WebService服务
https://jingyan.baidu.com/article/39810a23b1de2fb637fda66c.html
- [转]php,使用Slim和Medoo搭建简单restful服务
本文转自:https://www.jianshu.com/p/32b6d0284d97 关于rest和orm框架的组合使用,几年前我就写过使用Slim+Notorm来搭建restful服务,不过看到N ...
- 基于jersey和Apache Tomcat构建Restful Web服务(一)
基于jersey和Apache Tomcat构建Restful Web服务(一) 现如今,RESTful架构已然成为了最流行的一种互联网软件架构,它结构清晰.符合标准.易于理解.扩展方便,所以得到越来 ...
- Jersey+Spring+Maven环境搭建
第一步:创建一个Maven工程.加入Jersey.Spring.Jersey+Spring的依赖包,以及内嵌的Tomcat7插件: pom.xml文件如图所示: <project xmlns=& ...
随机推荐
- 理解css margin
前提是对display:block元素 1 margin对没有width属性的元素,能影响其宽度,对于有width的则不起作用: 高度方面不受影响 此特性可用来排版 2 margin 与百分比 普通的 ...
- Quartz2D 编程指南(四)位图与图像遮罩、CoreGraphics 绘制 Layer
概览 图形上下文 路径 颜色与颜色空间 变换 图案 阴影 渐变 透明层 Quartz 2D 中的数据管理 位图与图像遮罩 CoreGraphics 绘制 Layer 位图与图像遮罩 简介 位图与图像遮 ...
- 使用NUGet自动下载(还原)项目中使用的包
签出完整项目后,在解决方案名称上点右键,选择"启用NuGet程序包还原",如下图: 出现询问,当然要点是:是 当完成后,会发现在解决方案中,多出".nuget" ...
- Jenkins搭建的几个坑记下
坑一 安装cobertura插件后,一定要在工程代码的pom.xml文件里添加如下插件配置: <plugin> <groupId>org.codehaus.mojo</g ...
- IIS发布网站
IIS发布网站几个问题 1.部署步骤:http://jingyan.baidu.com/article/3065b3b6e5becdbecff8a4d5.html Win7下IIS报503Servic ...
- SQL 一条SQL语句 统计 各班总人数,男女各总人数 ,各自 男女 比例 (转)
select sClass 班级,count(*) 班级学生总人数, sum(case when sGender=0 then 1 else 0 end) 女生人数, sum(case when ...
- asp.net mvc 5 web api 关于Requested resource does not support options 问题
1.用visual studio 2015 建立一个 web api 应用程序.记住这是一个 web api 应用. 2.新建一个web api . 3.用C#访问,代码如下:[没有问题,返回正确] ...
- deep learning...深入学习深度学习 --工具篇
Caffe( http://caffe.berkeleyvision.org/ )是一个清晰而高效的深度学习框架,其作者是博士毕业于UC Berkeley的贾扬清( http://daggerfs.c ...
- SQL用先进先出存储过程求出库数量
create table t( id ,), name ),--商品名称 j int, --入库数量 c int, --出库数量 jdate datetime --入库时间 ) ,,'2007-12- ...
- Java 线程的转换及状态
线程的状态转换是线程控制的基础. 线程状态总的可分为五大状态:分别是生.死.可运行.运行.等待/阻塞.用一个图来描述如下: 1.新建状态(New):新创建了一个线程对象. 2.就绪状态(Runnabl ...