使用Maven工具进行创建
在操作之前请确认电脑是否安装了Java和Maven
准备工作
使用maven命令查看安装信息mvn -v
使用java命令查看安装信息java -version
创建项目
参照官网:https://jersey.java.net/download.html
这里说明了使用maven创建项目的两种方式
- Grizzly 2 HTTP server container
- Servlet container
这里只讲述使用Grizzly 2 HTTP server container创建,完整的maven命令如下:
mvn archetype:generate \
-DarchetypeArtifactId=jersey-quickstart-grizzly2 \
-DarchetypeGroupId=org.glassfish.jersey.archetypes \
-DinteractiveMode=false \
-DgroupId=com.yan.liu \
-DartifactId=RESTfullProject \
-Dpackage=com.yan.liu \
-DarchetypeVersion=2.25.1
DarchetypeArtifactId
和DarchetypeGroupId
为jersey框架的固定形式- DinteractiveMode:为false代表无需交互
- DgroupId:组ID
- DartifactId:项目名称
- Dpackage:包名
- DarchetypeVersion:jersey框架的版本号,目前最新为2.25.1(2017.3)
注:命令中符号 \ 代表换行的意思,创建可使用如下长的完整的
mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false -DgroupId=com.yan.liu -DartifactId=RESTfullProject -Dpackage=com.yan.liu -DarchetypeVersion=2.25.1
接下来,在电脑上找个文件夹存放该项目,在该文件夹下执行maven命令,结果截图,如需要相关下载,会自动下载
执行完成后,在文件夹下会生成该项目,如图
目录结构分析
使用IntelliJ IDEA打开该项目,目录结构如下:
项目生成了示例代码,稍后我们进行分析。
测试运行
找到Main方法,运行,成功启动如图显示
通过请求工具Advanced REST client进行请求测试,地址:http://localhost:8080/myapp/myresource
,会得到”Got it!”
代码分析
Main.java
package com.yan.liu;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import java.io.IOException;
import java.net.URI;
/**
* Main class.
* */
public class Main {
// Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI = "http://localhost:8080/myapp/";
/**
* Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
* @return Grizzly HTTP server.
*/
public static HttpServer startServer() {
// create a resource config that scans for JAX-RS resources and providers
// in com.yan.liu package
final ResourceConfig rc = new ResourceConfig().packages("com.yan.liu");
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}
/**
* Main method.
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
final HttpServer server = startServer();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
System.in.read();
server.stop();
}
}
开启HttpServer
main方法执行时,首先调用
startServer()
方法去开启grizzly http server在
startServer()
方法中final ResourceConfig rc = new ResourceConfig().packages("com.yan.liu");
表示创建一个资源配置扫描jax - rs resources 和 providers,所谓的resources 可以理解为需要请求的类和方法。这里使用packages()
方法进行包的扫描,也可以指定类的示例,例如:final ResourceConfig rc = new ResourceConfig(MyResource.class);
参数为不定参数,可设置多个。GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
创建并启动一个新的类型为HttpServer
实例。注:createHttpServer()
有第三个类型为boolean的参数,表示是否开启服务,默认为true,如果设置成false,可用server.start();
开启。这里我们将修改为 false,目的是将开启与创建进行分离
修改main方法
由于当前的main方法为单线程,运行后hold不住,将其进行修改
原 main()
public static void main(String[] args) throws IOException {
final HttpServer server = startServer();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
System.in.read();
server.stop();
}
修改后main()
public static void main(String[] args) throws Exception {
final HttpServer server = startServer();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
server.shutdownNow();
}));
server.start();
System.out.println(String.format("Jersey app started available at %s\n", BASE_URI));
Thread.currentThread().join();
}
说明:
- addShutdownHook()方法表示java虚拟机在关闭时候会执行该回调,关闭http server , stop()方法已经过时,将使用shutdownNow()进行关闭
- server.start(),由于之前创建http server的方式改为
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc,false);
所以这里需要将其进行开启。 Thread.currentThread().join();
线程整合修改后的完整代码
package com.yan.liu;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import java.io.IOException;
import java.net.URI;
/**
* Main class.
*
*/
public class Main {
// Base URI the Grizzly HTTP server will listen on
public static final String BASE_URI = "http://localhost:8080/myapp/";
/**
* Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
* @return Grizzly HTTP server.
*/
public static HttpServer startServer() {
// create a resource config that scans for JAX-RS resources and providers
// in com.yan.liu package
final ResourceConfig rc = new ResourceConfig().packages("com.yan.liu");
// final ResourceConfig rc = new ResourceConfig(MyResource.class);
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc,false);
}
/**
* Main method.
* @param args
* @throws IOException
*/
public static void main(String[] args) throws Exception {
final HttpServer server = startServer();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
server.shutdownNow();
}));
server.start();
System.out.println(String.format("Jersey app started available at %s\n", BASE_URI));
Thread.currentThread().join();
}
}
待