Per comment to post test (note: using standalone grizzly container, not tomcat)
@Path("/email")
public class EmailResource {
@GET
@Path("{id}/{emailAddress : (.+)?}")
public Response getEmail(@PathParam("id") String id,
@PathParam("emailAddress") String email) {
StringBuilder builder = new StringBuilder();
builder.append("id: ").append(id).append("\n");
builder.append("email: ").append(email).append("\n");
return Response.ok(builder.toString()).build();
}
}
Standalone server
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import org.glassfish.grizzly.http.server.HttpServer;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
public class Main {
private static int getPort(int defaultPort) {
//grab port from environment, otherwise fall back to default port 9998
String httpPort = System.getProperty("jersey.test.port");
if (null != httpPort) {
try {
return Integer.parseInt(httpPort);
} catch (NumberFormatException e) {
}
}
return defaultPort;
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/api").port(getPort(8080)).build();
}
public static final URI BASE_URI = getBaseURI();
protected static HttpServer startServer() throws IOException {
ResourceConfig resourceConfig
= new PackagesResourceConfig("jersey1.stackoverflow.standalone");
System.out.println("Starting grizzly2...");
return GrizzlyServerFactory.createHttpServer(BASE_URI, resourceConfig);
}
public static void main(String[] args) throws IOException {
// Grizzly 2 initialization
HttpServer httpServer = 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();
httpServer.stop();
}
}
Note: the resource class is in jersey1.stackoverflow.standalone
. To run the server, just run the Main
class. The resource class will be autodiscovered based on new PackagesResourceConfig("jersey1.stackoverflow.standalone")
Using this dependency
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-grizzly2</artifactId>
<version>${jersey-version}</version> <!-- 1.9 -->
</dependency>
Maven archytype can be retreived from
Group id: com.sun.jersey.archetypes
Artifact id: jersey-quickstart-grizzly2
Version: 1.9
Curl
curl -v http://localhost:8080/api/email/5/emailpart1/part2@example.org
Result:
id: 5
email: emailpart1/part2@example.org