问题
I have already looked at many tutorials, but I can't get my simple JAX-RS application to work. Tomcat always returns Error 404.
The Application to get Classes:
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;
}
}
My JAX-RS file:
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>
Under "Web Project Settings" my Context root is: "MSM-Master"
The url I tried to call was:
http://localhost:8080/MSM-Master/resources/hello/
Edit: added Tomcat Console output:
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:
@ApplicationPath
provided by JAX – RS
which is used to develop a restful web services application with out web.xml
. So you can delete your web.xml
Then try overriding 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;
}
}
来源:https://stackoverflow.com/questions/47816101/jax-rs-on-tomcat-always-returns-404