0

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...

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Julez
  • 1,010
  • 22
  • 44
  • Can you try removing the “/WEB-INF” portion of your return string, and just send “/index.jsp” or “index.jsp”? I don’t know the MVC spec too well, but I think it wants a web path (relative to the context root) rather than a file path. HTH, Andy – Andy McCright Jan 31 '18 at 13:28
  • I tried removing the "/WEB-INF" portion and it's the same; it prints the returned string instead of the contents of that string which represents as the file name of the template. – Julez Feb 11 '18 at 09:08

2 Answers2

0

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.

chkal
  • 5,598
  • 21
  • 26
  • Thanks for the reply. I'll look into it and will get back for the result. – Julez Feb 21 '18 at 08:53
  • I've added the dependencies above. I updated my ResourceConfig to register OzarkFeature.class - there is no OzarkFeature but OzarkJerseyFeature. Upon running, console has this log: WARNING: A registered provider org.mvcspec.ozark.jersey.bootstrap.OzarkJerseyFeature is constrained (via @ConstrainedTo) to SERVER runtime but does not implement any provider interface usable in the runtime. Due to constraint configuration problems the provider org.mvcspec.ozark.jersey.bootstrap.OzarkJerseyFeature will be ignored. – Julez Feb 25 '18 at 10:47
  • Still, I can't make it work. I have uploaded the complete codes to my GitHub: https://github.com/julianjupiter/first-jsr371-mvc-app – Julez Feb 25 '18 at 10:48
  • Sorry, didn't find time yet to have a deeper look at the app. But could you try to also register `OzarkCoreFeature`? I never tried to run Ozark in embedded Grizzly. But you should really try to post on ozark-users to reach more people regarding this. – chkal Feb 27 '18 at 22:12
  • Looking forward to your mail! :-) – chkal Mar 03 '18 at 07:36
0

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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • I believe @ApplicationPath is applicable to WAR and application server. I tried it and didn't work. https://github.com/julianjupiter/first-jsr371-mvc-app – Julez Sep 18 '18 at 01:22