Java - 访问Tomcat / Jersey网址给出404 Not Found

时间:2016-06-04 14:18:37

标签: java xml rest tomcat jersey

过去两个小时我一直在努力解决这个问题。我已经看过很多不同的StackOverflow答案了(很多问题都是这样的),但其中没有一个能为我工作。

我有一个简单的'CountryResources'类,它使用@Path("/countries"),但当我导航到localhost:8080/restservices/countries时,它只会给我404错误。

CountryResource类,位于worldmap.services包中:

package worldmap.services;

import model.Country;
import model.CountryService;
import model.ServiceProvider;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/countries")
public class CountryResource {

    @GET
    @Produces("application/json")
    public String getCountries() {
        System.out.println("Opgehaald");
        CountryService cs = ServiceProvider.getWorldService();
        JsonArrayBuilder jab = Json.createArrayBuilder();

        for(Country c : cs.getAllCountries()) {
            JsonObjectBuilder job = Json.createObjectBuilder();
            job.add("code", c.getCode());
            job.add("name", c.getName());
            job.add("continent", c.getContinent());
            job.add("region", c.getRegion());
            job.add("surface", c.getSurface());
            job.add("population", c.getPopulation());
            job.add("government", c.getGovernment());

            jab.add(job);
        }
        JsonArray array = jab.build();
        return array.toString();
    }

}

Web XML:

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

<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         metadata-complete="false">

    <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>worldmap.services</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/restservices/*</url-pattern>
    </servlet-mapping>

</web-app>

1 个答案:

答案 0 :(得分:0)

已发现问题:Maven没有将程序的依赖项复制到目标的lib文件夹。将不得不尝试找出如何解决这个问题。