我有一个应用于Tomcat 8.5的Jersey 2应用程序。当我导航到应用程序URL时,我得到HTTP 404.请帮助。我已经看到了关于相同的多个问题,但我无法弄清楚我的设置有什么问题。日志中也没有错误。
我的应用:
package com.mydomain;
import javax.json.Json;
import javax.json.JsonObject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/")
public class MyClass {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response defaultEntry() {
JsonObject result = (Json.createObjectBuilder()
.add("test", "OK")
.build());
return Response.status(200).entity(result).build();
}
}
web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.mydomain.MyClass</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
该应用已部署到${CATALINA_HOME}/webapps
。应用程序中有一个名为myapp
的目录。我使用的网址是localhost:8080/myapp
。
请帮忙。
编辑:
我附上了日志的内容:
catalina.out
:
05-Sep-2016 14:09:44.698 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.4
05-Sep-2016 14:09:44.699 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jul 6 2016 08:43:30 UTC
05-Sep-2016 14:09:44.699 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.4.0
05-Sep-2016 14:09:44.699 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Mac OS X
05-Sep-2016 14:09:44.699 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 10.10.5
05-Sep-2016 14:09:44.699 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: x86_64
05-Sep-2016 14:09:44.699 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /Library/Java/JavaVirtualMachines/jdk1.8.0_71.jdk/Contents/Home/jre
05-Sep-2016 14:09:44.700 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_71-b15
05-Sep-2016 14:09:44.700 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
05-Sep-2016 14:09:44.700 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /Users/ksushenko/projects/apache-tomcat-8.5.4
05-Sep-2016 14:09:44.700 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /Users/ksushenko/projects/apache-tomcat-8.5.4
05-Sep-2016 14:09:44.700 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/Users/ksushenko/projects/apache-tomcat-8.5.4/conf/logging.properties
05-Sep-2016 14:09:44.700 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
05-Sep-2016 14:09:44.700 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
05-Sep-2016 14:09:44.700 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/Users/ksushenko/projects/apache-tomcat-8.5.4
05-Sep-2016 14:09:44.701 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/Users/ksushenko/projects/apache-tomcat-8.5.4
05-Sep-2016 14:09:44.701 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/Users/ksushenko/projects/apache-tomcat-8.5.4/temp
05-Sep-2016 14:09:44.701 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/ksushenko/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
05-Sep-2016 14:09:44.784 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
05-Sep-2016 14:09:44.803 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
05-Sep-2016 14:09:44.805 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
05-Sep-2016 14:09:44.806 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
05-Sep-2016 14:09:44.806 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 346 ms
05-Sep-2016 14:09:44.822 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
05-Sep-2016 14:09:44.822 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.4
05-Sep-2016 14:09:44.837 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /Users/ksushenko/projects/apache-tomcat-8.5.4/webapps/myapp.war
05-Sep-2016 14:09:45.310 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars 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.
05-Sep-2016 14:09:45.790 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /Users/ksushenko/projects/apache-tomcat-8.5.4/webapps/myapp.war has finished in 952 ms
05-Sep-2016 14:09:45.790 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /Users/ksushenko/projects/apache-tomcat-8.5.4/webapps/docs
05-Sep-2016 14:09:45.799 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /Users/ksushenko/projects/apache-tomcat-8.5.4/webapps/docs has finished in 9 ms
05-Sep-2016 14:09:45.799 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /Users/ksushenko/projects/apache-tomcat-8.5.4/webapps/examples
05-Sep-2016 14:09:45.924 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /Users/ksushenko/projects/apache-tomcat-8.5.4/webapps/examples has finished in 125 ms
05-Sep-2016 14:09:45.924 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /Users/ksushenko/projects/apache-tomcat-8.5.4/webapps/host-manager
05-Sep-2016 14:09:45.937 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /Users/ksushenko/projects/apache-tomcat-8.5.4/webapps/host-manager has finished in 13 ms
05-Sep-2016 14:09:45.937 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /Users/ksushenko/projects/apache-tomcat-8.5.4/webapps/manager
05-Sep-2016 14:09:45.996 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /Users/ksushenko/projects/apache-tomcat-8.5.4/webapps/manager has finished in 59 ms
05-Sep-2016 14:09:45.996 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /Users/ksushenko/projects/apache-tomcat-8.5.4/webapps/ROOT
05-Sep-2016 14:09:46.004 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /Users/ksushenko/projects/apache-tomcat-8.5.4/webapps/ROOT has finished in 8 ms
05-Sep-2016 14:09:46.005 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [http-nio-8080]
05-Sep-2016 14:09:46.009 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [ajp-nio-8009]
05-Sep-2016 14:09:46.009 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1203 ms
答案 0 :(得分:1)
使用Jersey 2和Servlet 3,您实际上并不需要web.xml,注释应该足够了。尝试从项目中删除web.xml。
答案 1 :(得分:0)
查看示例Jax-RS Servlet 3.0 maven Tomcat 通常,对于新的Servlet 3.0,您不需要web.xml来加载应用程序。但是,您需要一个扩展Application的类:
package com.vvirlan.java.ws;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import com.vvirlan.java.ws.resources.Hello;
@ApplicationPath("/rest")
public class MyApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> returnValue = new HashSet<Class<?>>();
returnValue.add(Hello.class);
return returnValue;
}
}