spring com.sun.jersey.api.container.ContainerException:ResourceConfig实例不包含任何根资源类

时间:2016-04-15 05:55:27

标签: java spring rest spring-mvc

我刚刚开始了我的第一个简单的春季计划。我正在尝试使用带弹簧的Jersey框架制作Rest API。它是一个非常基本的简单程序,用于获取国家/地区列表。但是,它会抛出错误。我已经完成了其他解决方案,但都没有。

Web.xml中

<web-app id="WebApp_ID" version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <display-name>Spring MVC Application</display-name>

   <servlet>
       <servlet-name>HelloWeb</servlet-name>
      <servlet-class>
     org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
     <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
  <servlet-name>HelloWeb</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>

<servlet>  
    <servlet-name>jersey-serlvet</servlet-name>  
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>         
    <init-param>  
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>  
      <param-value>true</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
</servlet>  

<servlet-mapping>  
    <servlet-name>jersey-serlvet</servlet-name>  
    <url-pattern>/rest/*</url-pattern>  
</servlet-mapping> 

</web-app>

我得到的错误是

SEVERE: StandardWrapper.Throwable
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
 at com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:99)
at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1359)
at com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:180)
at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:799)
at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:795)
at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)

我的节目

package org.arpit.java2blog.jaxrs;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.arpit.java2blog.bean.Country;


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

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Country> getCountries() {
    System.out.println("Getting countries");
    List<Country> listOfCountries = new ArrayList<Country>();
    listOfCountries = createCountryList();
    return listOfCountries;
}

@GET
@Path("{id: \\d+}")
@Produces(MediaType.APPLICATION_JSON)
public Country getCountryById(@PathParam("id") int id) {
    List<Country> listOfCountries = new ArrayList<Country>();
    listOfCountries = createCountryList();

    for (Country country : listOfCountries) {
        if (country.getId() == id)
            return country;
    }

    return null;
}

// Utiliy method to create country list.
public List<Country> createCountryList() {
    Country indiaCountry = new Country(1, "India");
    Country chinaCountry = new Country(4, "China");
    Country nepalCountry = new Country(3, "Nepal");
    Country bhutanCountry = new Country(2, "Bhutan");

    List<Country> listOfCountries = new ArrayList<Country>();
    listOfCountries.add(indiaCountry);
    listOfCountries.add(chinaCountry);
    listOfCountries.add(nepalCountry);
    listOfCountries.add(bhutanCountry);
    return listOfCountries;
}
}

URL

http://localhost:8080/HelloWeb/rest/countries

这个缺少什么?

1 个答案:

答案 0 :(得分:0)

您没有在web.xml中配置Jersey servlet(ServletContainer)来获取任何资源类。所以泽西开始没有注册资源。

使用web.xml通常的方法是设置一个init-param,告诉Jersey扫描某个包,注明用@Path@Provider注释的类。为此,只需添加以下内容

<servlet>  
    <servlet-name>jersey-serlvet</servlet-name>  
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>  
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>the.package.you.want.to.scan</param-value>
    </init-param>       
    ...
</servlet>