Simple JavaEE HTML GET/POST application

331 views Asked by At

I'm just starting out with JavaEE (I'm decently fluent in JavaSE) and am having trouble wrapping my brain around all the new things that are required to make even the simplest of applications. Right now I am trying to use JAX-RS annotations to generate a simple "Hello World" html page in IntelliJ using Glassfish 4. I've searched around for the proper use of these annotations, and it seems like I'm doing it correctly, but I keep getting a 404 no matter where I navigate in localhost. I'm starting to think I'm missing vital components in my code, and I don't know enough about JavaEE to know what I'm missing (perhaps it could be something in the web xml file, which I don't know too much about). Here is the code that I wrote, minus the imports:

@LocalBean
@Stateless
@Path("/hello")
@Produces("text/html")
public class Hello {

    @GET
    @Path("/world")
    public String printHelloWorld() {
        return "<html lang=\"en\"><body><h1>Hello, World!</h1></body></html>";
    }
}

The server itself is up and running and the application seems to deploy correctly. The default URL that is set up at launch time is "http://localhost:8080/HelloWorld_war_exploded/", so my understanding is that I should be going to http://localhost:8080/HelloWorld_war_exploded/hello/world to display the message.

EDIT: Here is my XML file, which I did not change at all:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
</web-app>

Upon looking at Lutz's comment, I've investigated the base URL issue and am currently viewing the following link: http://www-01.ibm.com/support/knowledgecenter/SSAW57_8.5.5/com.ibm.websphere.nd.doc/ae/twbs_jaxrs_configwebxml.html?cp=SSAW57_8.5.5%2F1-3-0-28-2-0-1

I will update accordingly.

1

There are 1 answers

1
Paul Samsotha On BEST ANSWER

You need to configure Jersey (the JAX-RS implementation in Glassfish) in your web.xml. You currently only have JSF configuration

<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>the.package.where.your.resources.are</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

The <url-mapping> is the base for your Jersey app. So you would use

http://localhost:8080/HelloWorld_war_exploded/api/hello/world

If you want to use Standard JAX-RS configuration, you can do

<servlet>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>

<servlet-mapping>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

This will scan your entire classpath for resources, so you don't need to specify the package, like the previous configuration.

Or you can use Java code

@javax.ws.rs.ApplicationPath("/api")
public class RestApplication extends javax.ws.rs.core.Application {

}

Leaving this class empty will also scan the entire classpath for your resources. Or you can add your classes explicitly

@javax.ws.rs.ApplicationPath("/api")
public class RestApplication extends javax.ws.rs.core.Application {
     @Override
     public Set<Class<?>> getClasess() {
         Set<Class<?>> classes = new HashSet<>();
         classes.add(Hello.class);
         return classes;
     }
}