Tomcat unable to find resource for rest web service

时间:2018-02-03 08:46:47

标签: java rest tomcat netbeans jax-rs

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

2 个答案:

答案 0 :(得分:0)

请参阅您的web.xml配置

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

根据上述配置,您的网址映射为/ rest,但在网址中没有“休息”。是可用的。

你能用*改变servlet映射吗?

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

答案 1 :(得分:0)

只需将 URL 更改为 http://localhost:8080/callback/test

这似乎是一条清晰的道路。我不明白您如何通过发布的 URL 访问它。