0

I have seen similar questions here but the solution does not work for me so I think mine is a different issue.

I have created a simple rest service with netbeans and when I deploy it from within netbeans, it works on url http://localhost:8080/callback/callback/test but once I build a war file and deploy it in the same Tomcat instance, it returns error 404 resource not found. I am not sure what to do so that Tomcat recognizes the URL.

My Apllication Config:

import java.util.Set;
import javax.ws.rs.core.Application;

@javax.ws.rs.ApplicationPath("callback")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        addRestResourceClasses(resources);
        return resources;
    }

    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(com.orig.callback.Origafric.class);
    }

}

My service class:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;


@Path("/test")
public class Origafric {


    public Origafric() {
    }

    /**
     * Retrieves representation of an instance of com.orig.callback.Origafric
     *
     * @param provider
     * @param clientAccount
     * @param productName
     * @param phoneNumber
     * @param value
     * @param providerMetadata
     * @return an instance of java.lang.String
     * @throws java.io.IOException
     */
    @GET
    @Produces("application/json")
    public List<ValidationResult>  getTransactions(@QueryParam("provider") String provider,
            @QueryParam("clientAccount") String clientAccount, @QueryParam("productName") String productName,
            @QueryParam("phoneNumber") String phoneNumber, @QueryParam("value") String value,
            @QueryParam("providerMetadata") String providerMetadata) throws IOException {
        List<ValidationResult> list = new ArrayList<>();
        ValidationResult res = new ValidationResult();
        res.setProvider(provider);
        res.setClientAccount(clientAccount);
        res.setProductName(productName);
        res.setPhoneNumber(phoneNumber);
        res.setValue(value);
        res.setProviderMetadata(providerMetadata);
        list.add(res);
        String homeDir = System.getProperty("user.home") + File.separator + "callback";
        File f = new File(homeDir);
        if (!f.exists()) {
            f.mkdir();
        }
        File logfile = new File(f.getAbsolutePath() + File.separator + "results.log");
        FileWriter out = new FileWriter(logfile, true);
        for (ValidationResult el : list) {
            out.write(el.getProvider() + "\n");
            out.write(el.getClientAccount() + "\n");
            out.write(el.getProductName() + "\n");
            out.write(el.getPhoneNumber() + "\n");
            out.write(el.getValue() + "\n");
            out.write(el.getProviderMetadata() + "\n");
            out.flush();
        }
        return list;
    }

}

And my web.xml

<?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">

    <display-name>RestExample</display-name>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>de.jay.jersey.first</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

ValidationResult is a getter setter for my values. What am I missing here?

My Libraries Directory: From Netbeans

My war lib directory: enter image description here

Stanley Mungai
  • 4,044
  • 30
  • 100
  • 168
  • what is the name of war file? you are naming it as ROOT.war right? – best wishes Feb 03 '18 at 08:57
  • Nope the war file is `callback.war` – Stanley Mungai Feb 03 '18 at 09:00
  • How do you deploy the WAR file? Maven? Tomcat Web Application Manager? – braunpet Feb 03 '18 at 09:15
  • @braunpet Tomcat web Application Manager. – Stanley Mungai Feb 03 '18 at 11:28
  • I guess that there are libraries missing in the WAR file. Can you please extend your question in order to show the contents of `WEB-INF/lib`. And please double-check that deploying the WAR file was successful. You cannot redeploy using the Web App Manager. – braunpet Feb 03 '18 at 11:52
  • @braunpet I have added both Libraries screenshots from the IDE and from the war lib directory – Stanley Mungai Feb 03 '18 at 12:13
  • @ErrorNotFoundException I don't think, it is related to missing libraries instead could you post screenshot from browser when you run it from Netbeans ? and when you run it directly – Ravi Feb 03 '18 at 12:18
  • @Ravi If `jersey-container-servlet` was missing, it would have been a `404`. @ErrorNotFoundException I don't see a JSON provider in your classpath. Once your `404` problem is solved, you will run into a `500` problem because of that. – braunpet Feb 03 '18 at 16:08
  • @braunpet If any library is missing, you should get classnotfound or related exception .Not 404. This means, all lib was available but, it was unable to find resource on specific url. – Ravi Feb 03 '18 at 16:11
  • @Ravi Library `jersey-container-servlet` contains a class that implements interface `ServletContainerInitializer`, which is found by the Servlet container during start-up. If the library is missing, the whole Jersey sub-system just doesn't start, which will result in a `404` once you hit any URL of the Jersey-based application. There will be no `ClassNotFoundException` because nobody is actively looking for any Jersey class. – braunpet Feb 03 '18 at 16:48

2 Answers2

0

Please see your web.xml configuration

  <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

As per above configuration, your URL mapping is /rest but in url there is no 'rest' is available.

Can you change servlet mapping with * and try,

<servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/*</url-pattern>
</servlet-mapping>
Jamsheer
  • 3,673
  • 3
  • 29
  • 57
0

Just change the URL to http://localhost:8080/callback/test

This appears to be the clear path. I don't understand how you could access it on the URL posted.