尝试在本地启动我的Web服务时出现以下错误
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();
}
}
我的Web 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();
}
}
我注意到很多人都犯过类似的错误,但是针对个人情况给出了很多不同的建议,这使我很难找到解决该错误的方法。有人认为我的方法明显有问题吗?如果是,请您帮我解决吗?
谢谢你的时间
最佳答案
也用ExposedFunctions
注释@Path
。
@Path("/")
public class ExposedFunctions {
编辑:类是根资源,而方法是子资源。您必须同时拥有。
关于java - 启动jax-rs服务器时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25164116/