Home > database >  Glassfish server could not locate resource simple jax-rs Hello World
Glassfish server could not locate resource simple jax-rs Hello World

Time:06-01

Just made a simple REST API using jax-rs and used maven for build/packaging.

Code structure is as below post compilation:

.
├── pom.xml
├── src
│   └── main
│       └── java
│           ├── Hello.java
│           └── WEB-INF
│               └── web.xml
└── target
    ├── classes
    │   └── com
    │       └── satyam
    │           └── Hello.class
    ├── generated-sources
    │   └── annotations
    ├── maven-archiver
    │   └── pom.properties
    ├── maven-status
    │   └── maven-compiler-plugin
    │       └── compile
    │           └── default-compile
    │               ├── createdFiles.lst
    │               └── inputFiles.lst
    ├── satyam
    │   ├── META-INF
    │   └── WEB-INF
    │       ├── classes
    │       │   └── com
    │       │       └── satyam
    │       │           └── Hello.class
    │       └── lib
    │           ├── activation-1.1.jar
    │           ├── javaee-api-8.0.1.jar
    │           └── javax.mail-1.6.2.jar
    └── satyam.war


My pom.xml is:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.satyam</groupId>
  <artifactId>satyam</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
  <name>mypackage</name>

  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>8.0.1</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>satyam</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

My web.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 
         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://java.sun.com/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

My Hello.java is:

package com.satyam;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("hello")
public class Hello {

    @GET
    @Produces("text/plain")
    public String sayHello()
    {
        return "Hello, World!";
    }
}

When I try to open: http://localhost:8080/satyam/rest/hello or http://localhost:8080/rest/hello

I get error: HTTP Status 404 - Not Found

Some more info about how I created the project and things I modified:

  • Project created using maven archetype: maven-archetype-webapp // as stated in maven website
  • Re-name src/main/webapp to src/main/java otherwise my java files were not compiling
  • Added only dependency for javax myself in pom.xml rest is as it is.
  • I have started glassfish server from command line as: /opt/glassfish4/bin/asadmin start-domain --verbose
  • And I am deploying my war file as: /opt/glassfish4/bin/asadmin deploy /home/satyam/proj/target/satyam.war

CodePudding user response:

Not sure if this is the issue but the url pattern in the web.xml seems strange to me. I would change

<url-pattern>/rest/*</url-pattern> to <url-pattern>/rest</url-pattern>

If you still have issues try to see what Glassfish itself has by directly accessing admin panel and logs. I would suggest connecting to port 4848 (admin console) and checking your mappings there.

http://localhost:4848 (user: admin, pass: adminadmin)

Also you can take a look at in the domain logs. They should be in this directory:

cd $GLASSFISH/domains

There you should see file structure like {domainName}/logs.

CodePudding user response:

By looking at the glassfish server application section I found the difference between a working web app and a non working jax-rs web app.

Basically Module javax.ws.rs.core.Application was missing in non working scenario. As I have stated in a comment section. Thanks @Yordan Boev for suggesting to look into admin panel.

Later extracted the *.war file and found that web.xml is not at all present in it.

In order to fix that I have to modify the directory structure as below:

Earlier when things were not working directory structure was as below:

.
├── pom.xml
├── src
│   └── main
│       └── java
│           ├── Hello.java
│           └── WEB-INF
│               └── web.xml

Now in order to make things work I had to put "WEB-INF" inside "webapp" directory and I have to bring "webapp" directory one level up i.e. it should be present under "main" directory as shown below:

.
├── pom.xml
├── src
│   └── main
│       ├── java
│       │   └── Hello.java
│       └── webapp
│           └── WEB-INF
│               └── web.xml
  • Related