服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|

服务器之家 - 编程语言 - JAVA教程 - 创建Jersey REST 服务,基于Maven的实现

创建Jersey REST 服务,基于Maven的实现

2020-11-20 10:36Java教程网 JAVA教程

下面小编就为大家带来一篇创建Jersey REST 服务,基于Maven的实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

基于JavaSE形式的REST服务

创建项目

我们首选使用 archetypeGroupId 为 org.glassfish.jersey.archetypes 的原型,archetypeArtifactId为 jersey-quickstart-grizzly2 的原型,创建REST服务项目,使用IDEA创建项目如下:

创建Jersey REST 服务,基于Maven的实现

点击OK后,使用该原始模型创建项目。

运行服务

项目创建好后,原始模型已经默认创建了一个REST服务,我们可以直接启动REST服务,进入项目的根目录,执行如下命令构建和启动服务:

mvnpackage

mvnexec:java

会启动REST服务,可以随时通过回车键停止服务,输出如下:

六月 19, 2017 11:12:23 下午 org.glassfish.grizzly.http.server.NetworkListener start

信息: Started listener bound to [localhost:8080]

六月 19, 2017 11:12:23 下午 org.glassfish.grizzly.http.server.HttpServer start

信息: [HttpServer] Started.

Jersey app started with WADL available at http://localhost:8080/myapp/application.wadl

Hit enter to stop it…

还提供了 WADL,通过访问 application.wadl 可以获取当前REST服务公布的接口:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<resources base="http://localhost:8080/myapp/">
 
        <resource path="myresource">
 
            <method id="getIt" name="GET">
 
                <response>
 
                    <representation mediaType="text/plain"/>
 
                </response>
 
            </method>
 
        </resource>
 
    </resources>

 

访问服务

可以直接访问 http://localhost:8080/myapp/myresource 就可以访问REST服务,直接访问REST服务,会输出 Got it! 。

项目说明

启动服务的命令 mvn exec:java,该命令实际调用了 exec-maven-plugin 插件定义的一个值为 java 的 goal ,用以触发mainClass中的main函数,插件配置如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<plugin>
 
     <groupId>org.codehaus.mojo</groupId>
 
     <artifactId>exec-maven-plugin</artifactId>
 
     <version>1.2.1</version>
 
     <executions>
 
            <execution>
 
                  <goals>
 
                      <goal>java</goal>
 
                 </goals>
 
            </execution>
 
      </executions>
 
      <configuration>
 
           <mainClass>org.drsoft.rest.Main</mainClass>
 
      </configuration>
 
 </plugin>

 

REST服务类为 MyResource,其 @Path 中定义了资源路径,@GET中定义了GET方法getIt(),@Produces中定义了响应的类型为普通字符串,示例代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Path("myresource")
 
public class MyResource {
 
  
 
    @GET
 
    @Produces(MediaType.TEXT_PLAIN)
 
    public String getIt() {
 
        return "Got it!";
 
    }
 
}

 

REST服务的单元测试类MyResourceTest,在单元测试类中,在执行单元测试前需要启动服务,并使用Jersey Client中定义的方法来调用REST服务,示例代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class MyResourceTest {
 
    private HttpServer server;
 
    private WebTarget target;
 
    @Before
 
    public void setUp() throws Exception {
 
        // start the server
 
        server = Main.startServer();
 
        // create the client
 
        Client c = ClientBuilder.newClient();
 
  
 
        // uncomment the following line if you want to enable
 
        // support for JSON in the client (you also have to uncomment
 
        // dependency on jersey-media-json module in pom.xml and Main.startServer())
 
        // --
 
        // c.configuration().enable(new org.glassfish.jersey.media.json.JsonJaxbFeature());
 
  
 
        target = c.target(Main.BASE_URI);
 
    }
 
  
 
    @After
 
    public void tearDown() throws Exception {
 
        server.stop();
 
    }
 
  
 
    @Test
 
    public void testGetIt() {
 
        String responseMsg = target.path("myresource").request().get(String.class);
 
        assertEquals("Got it!", responseMsg);
 
    }
 
}

 

基于Servlet容器服务

创建项目

我们首选使用 archetypeGroupId 为 org.glassfish.jersey.archetypes 的原型,archetypeArtifactId为 jersey-quickstart-webapp 的原型,创建REST服务项目,使用 IDEA 创建项目如下:

创建Jersey REST 服务,基于Maven的实现

 

运行服务

由于这个是Web项目,没有main函数,因此必须部署到Servlet容器中,才能将其运行,我们需要配置Tomcat,IDEA的配置如下:

点击 Run菜单的 Edit Configuration,在打开的窗体中增加 Tomcat 服务配置,指定Tomcat 的安装目录,并设置当前站点的部署的虚拟目录名称,如下:

创建Jersey REST 服务,基于Maven的实现

 

创建Jersey REST 服务,基于Maven的实现

点击OK后,就配置好Servlet容器,可以运行服务了

访问服务

服务启动后,我们可以访问 http://localhost:8080/RESTWebAPP/webapi/myresource 来调用REST服务,会输出 Got it!

项目说明

Web根目录的名称为webapp,默认的Servlet容器版本为2.5,并且配置了WEB-INF/web.xml文件来配置REST服务,web.xml配置如下:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="UTF-8"?>
 
<!-- This web.xml file is not required when using Servlet 3.0 container,
 
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
 
<web-app version="2.5" 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">
 
    <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>org.drsoft.rest</param-value>
 
        </init-param>
 
        <load-on-startup>1</load-on-startup>
 
    </servlet>
 
    <servlet-mapping>
 
        <servlet-name>Jersey Web Application</servlet-name>
 
        <url-pattern>/webapi/*</url-pattern>
 
    </servlet-mapping>
 
</web-app>

以上这篇创建Jersey REST 服务,基于Maven的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐