Jersey 2 + Maven + Tomcat + IntelliJ IDEA 搭建RESTful服务

时间:2023-03-09 03:15:05
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.

Jersey 2 + Maven + Tomcat +  IntelliJ IDEA 搭建RESTful服务

1. 加入web框架和maven框架

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

Jersey 2 + Maven + Tomcat +  IntelliJ IDEA 搭建RESTful服务

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"即可。Jersey 2 + Maven + Tomcat +  IntelliJ IDEA 搭建RESTful服务

7. 在输出中加入库文件

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

Jersey 2 + Maven + Tomcat +  IntelliJ IDEA 搭建RESTful服务

Jersey 2 + Maven + Tomcat +  IntelliJ IDEA 搭建RESTful服务

8. 运行Tomcat

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

Jersey 2 + Maven + Tomcat +  IntelliJ IDEA 搭建RESTful服务