View Engine for Grizzly, Jersey, and Ozark MVC

367 views Asked by At

I'm trying to create an MVC app using Java EE MVC API implementation - Ozark. I'm using Grizzly as embedded web server. Of course, with Ozark is a JAX-RS implementaion - Jersey.

This is my server class with run method being invoked by main method:

public class GrizzlyServer {    
    private static final Logger LOGGER = LoggerFactory.getLogger(GrizzlyServer.class);
    private static final String APP_NAME = "Hello World";
    private static URI baseUri;
    private static final String PROTOCOL = "http://";
    private static final String HOST = "localhost";
    private static final String PATH = "app/";
    private static final int DEFAULT_PORT = 8080;

    private GrizzlyServer() {

    }

    private static int port(String[] args) {
        if (args.length > 0) {
            String port = args[0];          
            try {
                return Integer.valueOf(port);
            } catch (NumberFormatException exception) {
                LOGGER.error("Invalid port number {}", port);
                LOGGER.error("Default port number {} will be used", DEFAULT_PORT);
            }
        }

        return DEFAULT_PORT;
    }

    public static HttpServer startServer(int port) {
        final ResourceConfig rc = new ApplicationResourceConfig();
        baseUri = UriBuilder.fromUri(PROTOCOL + HOST).port(port).path(PATH).build();
        HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(baseUri, rc);

        return httpServer;
    }


    public static void run(String[] args) throws IOException {
        int port = port(args);
        try {
            final HttpServer server = startServer(port);
            LOGGER.info("{} started with WADL available at {}application.wadl", APP_NAME, baseUri);
            LOGGER.info("Hit Enter to stop it...");
            System.in.read();
            server.shutdown();
        } catch (IOException exception) {
            LOGGER.error("{}", exception.getMessage());
            LOGGER.error("Exit...");
            System.exit(1);
        }
    }
}

This is my controller:

@Controller
@Path("hello-world")
public class HelloWorldController {

    @GET
    public String index() {
        return "/WEB-INF/index.jsp";
    }
}

My app structure looks like this:

java
resources
web
..WEB-INF
  ..index.jsp

If I run my app and open my browser, http://localhost:8080/app/hello-world, it displays the return string od my method index instead the contents of index.jsp:

/WEB-INF/index.jsp

These are the relevant dependencies (Jersey version: 2.26):

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<!-- Dependency Injection -->
<dependency>
    <groupId>org.glassfish.hk2</groupId>
    <artifactId>hk2-api</artifactId>
    <version>2.5.0-b50</version>
    <exclusions>
        <exclusion>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
</dependency>
<!-- MVC -->
<dependency>
    <groupId>org.glassfish.ozark</groupId>
    <artifactId>ozark</artifactId>
    <version>1.0.0-m02</version>
</dependency>

Any help will be highly appreciated. Thanks...

2

There are 2 answers

1
Gonzalo Ferreyra On

You should define a class registering the jax-rs application with @ApplicationPath.

5
chkal On

It is a bit difficult to tell what's wrong with your setup without seeing the full project. However, there are a few things that I would like to note:

First you should update to the latest version of the spec and the RI. The latest version includes a special module for Jersey support. So you should use these dependencies:

<dependency>
    <groupId>javax.mvc</groupId>
    <artifactId>javax.mvc-api</artifactId>
    <version>1.0-pr</version>
</dependency>
<dependency>
    <groupId>org.mvc-spec.ozark</groupId>
    <artifactId>ozark-jersey</artifactId>
    <version>1.0.0-m03</version>
</dependency>

The second thing: There is a special folder in MVC for storing views, so you should place the JSP file in WEB-INF/views/. If the full path of your view is /WEB-INF/views/index.jsp, you just have to return index.jsp from your controller method, because view names are relative to the view folder.

And the fact that your controller just renders the view name instead of the view content usually means that the required JAX-RS providers are not registered correctly. So you may have to register it manually. Not sure if this is caused by the fact that you are using embedded Grizzly. So you may have to add OzarkFeature.class to the classes returned from Application#getClasses().

I hope this helps. Feel free to join the ozark-users mailing list if you have any further questions.