gpt4 book ai didi

java - 启动 jax-rs 服务器时出错

转载 作者:行者123 更新时间:2023-12-01 12:40:21 25 4
gpt4 key购买 nike

尝试在本地启动 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)

我的类(class)如下所示;

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();
}
}

我注意到很多人都遇到过类似的错误,但是针对个人情况给出了很多不同的建议,这使得我很难找到该错误的解决方案。有没有人发现我的方法有任何明显的错误,如果是的话,你能帮我解决它吗?

感谢您的宝贵时间

最佳答案

也用 @Path 注释 ExposedFunctions

@Path("/")
public class ExposedFunctions {

编辑:类是根资源,而方法是子资源。您必须两者兼而有之。

关于java - 启动 jax-rs 服务器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25164116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com