启动jax-rs服务器时出错

时间:2014-08-06 15:16:42

标签: java rest jax-rs

尝试在本地启动我的网络服务时出现以下错误

SEVERE: The ResourceConfig instance does not contain any root resource classes.
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)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:172)
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:264)
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:246)
    at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:117)
    at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:92)
    at com.swiped.main.ServerStart.main(ServerStart.java:14)

我的课程如下:

public class ExposedFunctions {

    private static Logger logger = Logger.getLogger(ExposedFunctions.class);
    private DBFactoryController controller = new DBFactoryController();

    public ExposedFunctions(){
        if(controller!= null){
            controller.buildCache();
        }
    }

    @GET
    @Path("/add/{username}/{password}/{email}/{firstname}/{lastname}")
    @Produces(MediaType.TEXT_PLAIN)
    public String register(@PathParam("username") String username, @PathParam("password") String password, @PathParam("email") String email, @PathParam("firstname") String firstname, @PathParam("lastname") String lastname) {
        if(StringUtilities.stringEmptyOrNull(username, password, email, firstname, lastname)){
            logger.error("String was null or empty when registering");
        }

        RegistrationStatus status = controller.register(username, password, email, firstname, lastname);
        return status.getValue();
    }
}

我的网页xml如下;

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>com.swiped.service</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</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>com.swiped.service</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>
</web-app> 

我把我的主要课程全部打开,看起来如下;

@SuppressWarnings("restriction")
public static void main(String[] args){
    try{
        HttpServer server = HttpServerFactory.create(BASE_URI);
        server.start();
        System.out.println("Press Enter to stop the server. ");
        System.in.read();
        server.stop(0);
    }catch(Exception e){
        e.printStackTrace();
    }
}

我注意到很多人都有类似的错误,但是对于个别情况给出了很多不同的建议,这使我很难找到这个错误的解决方案。有没有人看到我的方法有任何明显错误,如果有,请你帮我解决一下吗?

感谢您的时间

1 个答案:

答案 0 :(得分:1)

使用ExposedFunctions注释@Path

@Path("/")
public class ExposedFunctions {

编辑:该类是根资源,而方法是子资源。你必须同时拥有两者。