ffddani ffddani - 1 year ago 139
reST (reStructuredText) Question

Using Jersey with Grizzly

I want to create a simple REST service, and for that I am using Jersey and Grizzly.

Here is my Service class:

class TestRESTService {

public String test() {
return "{ \"TestField\" : \"TestValue\" }";


And from what I understand here is how I supposed to start it:

ResourceConfig config = new ResourceConfig();
URI serverUri = UriBuilder.fromUri("http://localhost/").port(19748).build();
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(serverUri, config);

But it's not working. :(
I tried to send a request to


using Google Chrome's Postman plugin by adding 'Accept' : 'application/json' as a Http Header, but nothing is returned. The 'hourglass' is just spinning around.

Could you please help me?

Answer Source

This one works for me:

private static String API_PACKAGE = "package where TestRESTService class";

public static final URI BASE_URI = UriBuilder

private static HttpServer initServer() throws IOException {
    System.out.println("Starting grizzly... " + BASE_URI);

    HttpServer httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, new HttpHandler() {
        public void service(Request rqst, Response rspns) throws Exception {

    // Initialize and register Jersey Servlet
    WebappContext context = new WebappContext("GrizzlyContext", "/");
    ServletRegistration registration = context.addServlet(
            ServletContainer.class.getName(), ServletContainer.class);
    registration.setInitParameter(PackagesResourceConfig.PROPERTY_PACKAGES, API_PACKAGE);

    return httpServer;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download