当前位置: 首页 > 知识库问答 >
问题:

TomCat上的JAX-RS总是返回404

杭胜
2023-03-14

我已经看了很多教程,但我无法使我的简单JAX-RS应用程序工作。Tomcat总是返回错误404。

要获取类的应用程序:

package api;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/resources")
public class MyApplication extends Application {
    // All request scoped resources and providers
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<>();
        classes.add(AuthenticationEndpoint.class);
        return classes;
    }
}

我的JAX-RS文件:

package api;

@Path("/hello")
public class AuthenticationEndpoint {

    @GET
    public Response getUser() {
        return Response.status(200).entity("getUser is called").build();
    }
 }

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>MSM-Master</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
     <servlet-name>MyApplication</servlet-name>
     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
           <param-name>jersey.config.server.provider.packages</param-name>
           <param-value>api</param-value>
        </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>MyApplication</servlet-name>
      <url-pattern>/resources</url-pattern>
  </servlet-mapping>
</web-app>

http://localhost:8080/msm-master/resources/hello/

编辑:已添加Tomcat控制台输出:

    Dez 14, 2017 4:32:39 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNUNG: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:MSM-Master' did not find a matching property.
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server version:        Apache Tomcat/8.0.30
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server built:          Dec 1 2015 22:30:46 UTC
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Server number:         8.0.30.0
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: OS Name:               Windows 7
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: OS Version:            6.1
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Architecture:          amd64
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Java Home:             C:\Program Files\Java\jre1.8.0_66
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: JVM Version:           1.8.0_66-b18
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: JVM Vendor:            Oracle Corporation
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: CATALINA_BASE:         C:\Users\u016975\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: CATALINA_HOME:         C:\Program Files\Apache Software Foundation\Tomcat 8.0
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dcatalina.base=C:\Users\u016975\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 8.0
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dwtp.deploy=C:\Users\u016975\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Djava.endorsed.dirs=C:\Program Files\Apache Software Foundation\Tomcat 8.0\endorsed
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.VersionLoggerListener log
INFORMATION: Command line argument: -Dfile.encoding=Cp1252
Dez 14, 2017 4:32:39 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFORMATION: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_66\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:/Program Files/Java/jre1.8.0_66/bin/server;C:/Program Files/Java/jre1.8.0_66/bin;C:/Program Files/Java/jre1.8.0_66/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Java\jdk1.8.0_66\bin;C:\Program Files\Git\cmd;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Users\u016975\AppData\Roaming\npm\;C:\Users\u016975\apache-ant-1.9.7\bin;C:\Users\u016975\AppData\Local\Android\android-sdk\tools;C:\Users\u016975\AppData\Local\Android\android-sdk\platform-tools;C:\windows\system32; C:\windows; C:\windows\System32\Wbem;C:\Applications\Atlassian\atlassian-plugin-sdk-6.2.14\bin;C:\Users\u016975\AppData\Local\Apps\cURL\bin;C:\Applications\Atlassian\atlassian-plugin-sdk-6.2.15\bin;node_modules\npm;C:\Program Files\nodejs\;C:\Users\u016975\apache-ant-1.9.7\bin;C:\Users\u016975\AppData\Local\Android\android-sdk\tools;C:\Users\u016975\AppData\Local\Android\android-sdk\platform-tools;C:\windows\system32; C:\windows; C:\windows\System32\Wbem;C:\Applications\Atlassian\atlassian-plugin-sdk-6.2.14\bin;C:\Users\u016975\AppData\Local\Apps\cURL\bin;C:\Applications\Atlassian\atlassian-plugin-sdk-6.2.15\bin;node_modules\npm;C:\Users\u016975\AppData\Roaming\npm;C:\Program Files\Microsoft VS Code\bin;C:\Users\u016975\Downloads\eclipse-jee-mars-1-win32-x86_64\eclipse;;.
Dez 14, 2017 4:32:39 PM org.apache.coyote.AbstractProtocol init
INFORMATION: Initializing ProtocolHandler ["http-nio-8080"]
Dez 14, 2017 4:32:39 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFORMATION: Using a shared selector for servlet write/read
Dez 14, 2017 4:32:39 PM org.apache.coyote.AbstractProtocol init
INFORMATION: Initializing ProtocolHandler ["ajp-nio-8009"]
Dez 14, 2017 4:32:39 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFORMATION: Using a shared selector for servlet write/read
Dez 14, 2017 4:32:39 PM org.apache.catalina.startup.Catalina load
INFORMATION: Initialization processed in 621 ms
Dez 14, 2017 4:32:39 PM org.apache.catalina.core.StandardService startInternal
INFORMATION: Starting service Catalina
Dez 14, 2017 4:32:39 PM org.apache.catalina.core.StandardEngine startInternal
INFORMATION: Starting Servlet Engine: Apache Tomcat/8.0.30
Dez 14, 2017 4:32:46 PM org.apache.jasper.servlet.TldScanner scanJars
INFORMATION: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Dez 14, 2017 4:32:46 PM org.apache.catalina.core.StandardContext startInternal
SCHWERWIEGEND: Error during ServletContainerInitializer processing
javax.servlet.ServletException: java.lang.InstantiationException: org.glassfish.tyrus.server.TyrusServerConfiguration
    at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:88)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5244)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.InstantiationException: org.glassfish.tyrus.server.TyrusServerConfiguration
    at java.lang.Class.newInstance(Unknown Source)
    at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:74)
    ... 8 more
Caused by: java.lang.NoSuchMethodException: org.glassfish.tyrus.server.TyrusServerConfiguration.<init>()
    at java.lang.Class.getConstructor0(Unknown Source)
    ... 10 more

Dez 14, 2017 4:32:46 PM org.apache.catalina.core.StandardContext startInternal
SCHWERWIEGEND: Context [/MSM-Master] startup failed due to previous errors
Dez 14, 2017 4:32:46 PM org.apache.coyote.AbstractProtocol start
INFORMATION: Starting ProtocolHandler ["http-nio-8080"]
Dez 14, 2017 4:32:46 PM org.apache.coyote.AbstractProtocol start
INFORMATION: Starting ProtocolHandler ["ajp-nio-8009"]
Dez 14, 2017 4:32:46 PM org.apache.catalina.startup.Catalina start
INFORMATION: Server startup in 6457 ms

共有1个答案

段干跃
2023-03-14

jax-rs提供的@applicationpath用于开发一个没有web.xml的restful web服务应用程序。以便删除web.xml

然后尝试重写getproperties

@ApplicationPath("/resources")
public class MyApplication extends Application {
    // All request scoped resources and providers
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<>();
        classes.add(AuthenticationEndpoint.class);
        return classes;
    }

 @Override
    public Map<String, Object> getProperties() {
    final Map<String, Object> properties = new HashMap<>();
    properties.put("jersey.config.server.provider.packages",
                   "api");//api-->here mention your yourpackage.to.scan"
    return properties;
}
}
 类似资料:
  • 我有一个基本的REST web服务可以工作,但我确实有一个问题。下面是一个简短的代码片段。

  • 我正在尝试用Resteasy实现一个RESTFULL服务。当我试图在Tomcat Server7.0中运行时,iam出现异常。请在下面找到详细信息。

  • JAX-RS (JSR 311) 是一个社区驱动的标准用于使用 Java 构建 RESTful Web 服务。

  • 问题内容: 我想知道如何对方法返回List的JSON数组,例如: 我想知道如何在我的方法中接收List参数类型 我已经阅读了有关JaxbContext的文章,我了解它如何为我提供帮助。 问题答案: 使用JAXB时,支持两种类型。第一个是元素列表,第二个是元素定界字符串(“常规” XML值或属性,使用某些定界符将其解析为列表)。第一个似乎是您想要的(“数组”)。 有关参考,请参见:http : //

  • 问题内容: 如何使用Jersey / JAX-RS框架以XML / JSON文档形式返回a并不是很明显。它已经支持s了,但是当涉及到s时,就没有了。即使将嵌入到包装器类中,XML模式中也没有类型。 关于如何在Jersey中将地图编组为XML / JSON文档的任何实用建议? 问题答案: 我知道它的回复很晚,但是我希望有一天它能对某人有所帮助:)我应用的最简单,最快的修复方法是 输出:{“ 1”:“

  • 问题内容: 到目前为止,我将通过以下方式返回html我的主页: 我想要做的是返回home.html本身,而不是复制其内容并返回字符串。 我该怎么做呢?谢谢 :) 问题答案: 您可以只返回或的实例,JAX-RS会做正确的事。