IMLC.ME
Search…
en-US
How to serve one jersey resource or jetty servlet for different path
Today I'm trying to make an endpoint accessible in different paths. Such as I can go to http://localhost/ and http://localhost/hello for a same hello message.
I do got some solutions.
I'm using Jersey over Jetty. But whether or not you're using Jetty servlet or writing Jersey Resource, I think you will get some ideas.
Ps: I copy the code from my GitLab Snippets and will keep it update. So If you find something wrong, pls comment in GitLab

Using the regular expression in Jersey Resource

1
package me.imlc.example.jetty.multiplehandler;
2
3
import com.sun.jersey.spi.container.servlet.ServletContainer;
4
import me.imlc.example.jetty.multplehandler.HelloResource;
5
import org.apache.http.client.methods.HttpGet;
6
import org.apache.http.impl.client.CloseableHttpClient;
7
import org.apache.http.impl.client.HttpClients;
8
import org.apache.http.util.EntityUtils;
9
import org.eclipse.jetty.server.Server;
10
import org.eclipse.jetty.servlet.ServletContextHandler;
11
import org.eclipse.jetty.servlet.ServletHolder;
12
import org.glassfish.jersey.server.ResourceConfig;
13
import org.junit.AfterClass;
14
import org.junit.BeforeClass;
15
import org.junit.Test;
16
17
import javax.ws.rs.GET;
18
import javax.ws.rs.Path;
19
import javax.ws.rs.Produces;
20
import javax.ws.rs.core.MediaType;
21
import java.util.Objects;
22
23
import static org.junit.Assert.assertEquals;
24
25
public class RegexpResourceTest {
26
27
private static Server server;
28
private static CloseableHttpClient client = HttpClients.createDefault();
29
30
@BeforeClass
31
public static void beforeAll() throws Exception {
32
ResourceConfig rc = new ResourceConfig(RegexpResource.class);
33
34
ServletHolder holder = new ServletHolder(new ServletContainer(rc));
35
36
server = new Server(8080);
37
38
ServletContextHandler contextHandler = new ServletContextHandler(server, "/*");
39
contextHandler.addServlet(holder, "/*");
40
41
server.start();
42
}
43
44
@Test
45
public void canAccessByRootPath() throws Exception {
46
HttpGet get = new HttpGet("http://localhost:8080/hello");
47
String content = EntityUtils.toString(client.execute(get).getEntity());
48
assertEquals(content, "Hello, Lynn.");
49
}
50
51
@Test
52
public void canAccessByServicePath() throws Exception {
53
HttpGet get = new HttpGet("http://localhost:8080/service/hello");
54
String content = EntityUtils.toString(client.execute(get).getEntity());
55
assertEquals(content, "Hello, Lynn.");
56
}
57
58
@AfterClass
59
public static void afterAll() throws Exception {
60
if(Objects.nonNull(server)) server.stop();
61
if(Objects.nonNull(client)) client.close();
62
}
63
64
@Path("/{a:(.*)|service}")
65
public static class RegexpResource {
66
@GET
67
@Produces(MediaType.TEXT_PLAIN)
68
public String hello() {
69
return "Hello, Lynn.";
70
}
71
}
72
}
Copied!

Specify multiple paths for one servlet

1
package me.imlc.example.jetty.multiplehandler;
2
3
import com.sun.jersey.spi.container.servlet.ServletContainer;
4
import org.apache.http.client.methods.HttpGet;
5
import org.apache.http.impl.client.CloseableHttpClient;
6
import org.apache.http.impl.client.HttpClients;
7
import org.apache.http.util.EntityUtils;
8
import org.eclipse.jetty.server.Server;
9
import org.eclipse.jetty.servlet.ServletContextHandler;
10
import org.eclipse.jetty.servlet.ServletHolder;
11
import org.glassfish.jersey.server.ResourceConfig;
12
import org.junit.AfterClass;
13
import org.junit.BeforeClass;
14
import org.junit.Test;
15
16
import javax.ws.rs.GET;
17
import javax.ws.rs.Path;
18
import javax.ws.rs.Produces;
19
import javax.ws.rs.core.MediaType;
20
import java.util.Objects;
21
22
import static org.junit.Assert.assertEquals;
23
24
public class MultipleServletTest {
25
26
private static Server server;
27
private static CloseableHttpClient client = HttpClients.createDefault();
28
29
@BeforeClass
30
public static void beforeAll() throws Exception {
31
ResourceConfig rc = new ResourceConfig(RegexpResourceTest.RegexpResource.class);
32
33
ServletHolder holder = new ServletHolder(new ServletContainer(rc));
34
35
server = new Server(8080);
36
37
ServletContextHandler contextHandler = new ServletContextHandler(server, "/*");
38
contextHandler.addServlet(holder, "/*");
39
contextHandler.addServlet(holder, "/service/*");
40
41
server.start();
42
}
43
44
@Test
45
public void canAccessByRootPath() throws Exception {
46
HttpGet get = new HttpGet("http://localhost:8080/hello");
47
String content = EntityUtils.toString(client.execute(get).getEntity());
48
assertEquals(content, "Hello, Lynn.");
49
}
50
51
@Test
52
public void canAccessByServicePath() throws Exception {
53
HttpGet get = new HttpGet("http://localhost:8080/service/hello");
54
String content = EntityUtils.toString(client.execute(get).getEntity());
55
assertEquals(content, "Hello, Lynn.");
56
}
57
58
@AfterClass
59
public static void afterAll() throws Exception {
60
if(Objects.nonNull(server)) server.stop();
61
if(Objects.nonNull(client)) client.close();
62
}
63
64
@Path("/hello")
65
public static class MultipleServletResource {
66
@GET
67
@Produces(MediaType.TEXT_PLAIN)
68
public String hello() {
69
return "Hello, Lynn.";
70
}
71
}
72
}
Copied!

Serve one servlet in different context handler

1
package me.imlc.example.jetty.multiplehandler;
2
3
import com.sun.jersey.spi.container.servlet.ServletContainer;
4
import org.apache.http.client.methods.HttpGet;
5
import org.apache.http.impl.client.CloseableHttpClient;
6
import org.apache.http.impl.client.HttpClients;
7
import org.apache.http.util.EntityUtils;
8
import org.eclipse.jetty.server.Handler;
9
import org.eclipse.jetty.server.Server;
10
import org.eclipse.jetty.servlet.ServletContextHandler;
11
import org.eclipse.jetty.servlet.ServletHolder;
12
import org.glassfish.jersey.server.ResourceConfig;
13
import org.junit.AfterClass;
14
import org.junit.BeforeClass;
15
import org.junit.Test;
16
17
import javax.ws.rs.GET;
18
import javax.ws.rs.Path;
19
import javax.ws.rs.Produces;
20
import javax.ws.rs.core.MediaType;
21
import java.util.Objects;
22
23
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
24
25
import static org.junit.Assert.assertEquals;
26
27
public class MultipleContextHandlerTest {
28
private static Server server;
29
private static CloseableHttpClient client = HttpClients.createDefault();
30
31
@BeforeClass
32
public static void beforeAll() throws Exception {
33
ResourceConfig rc = new ResourceConfig(MultipleContextHandlerResource.class);
34
35
ServletHolder holder = new ServletHolder(new ServletContainer(rc));
36
37
server = new Server(8080);
38
39
ServletContextHandler rootContext = new ServletContextHandler(server, "/*");
40
rootContext.addServlet(holder, "/*");
41
42
ServletContextHandler serviceContext = new ServletContextHandler(server, "/service/*");
43
serviceContext.addServlet(holder, "/*");
44
45
ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();
46
contextHandlerCollection.setHandlers(new Handler[]{
47
rootContext, serviceContext
48
});
49
50
server.setHandler(contextHandlerCollection);
51
52
server.start();
53
}
54
55
@Test
56
public void canAccessByRootPath() throws Exception {
57
HttpGet get = new HttpGet("http://localhost:8080/hello");
58
String content = EntityUtils.toString(client.execute(get).getEntity());
59
assertEquals(content, "Hello, Lynn.");
60
}
61
62
@Test
63
public void canAccessByServicePath() throws Exception {
64
HttpGet get = new HttpGet("http://localhost:8080/service/hello");
65
String content = EntityUtils.toString(client.execute(get).getEntity());
66
assertEquals(content, "Hello, Lynn.");
67
}
68
69
@AfterClass
70
public static void afterAll() throws Exception {
71
if(Objects.nonNull(server)) server.stop();
72
if(Objects.nonNull(client)) client.close();
73
}
74
75
@Path("/hello")
76
public static class MultipleContextHandlerResource {
77
@GET
78
@Produces(MediaType.TEXT_PLAIN)
79
public String hello() {
80
return "Hello, Lynn.";
81
}
82
}
83
}
Copied!
Last modified 1yr ago