Java rest API返回HTTP状态404

时间:2015-11-16 11:49:33

标签: java web-services jersey-2.0

我有一个简单的REST API,我试图让它运行,它给我一个HTTP 404错误。 AFAICS它看起来是正确但不是...... Tomcat正在运行,我让Eclipse浏览器提出了HTTP404。 Tomcat没有帮助解决所有错误,但它们都只是警告,但如果需要我可以在这里弹出它们,但会使问题变得混乱。

我认为它与球衣格式,xml或两者都有关,但不确定。

*****更新了问题以包含xml更新和tomcat错误。******

我的课程是:

package WS;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

//annotations
@Path("/book")
public class Book{
    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayHelloXML(){
        String response = "<?xml version='1.0'?><hello>Hello there</hello>";
        return response;
    }
}

我的Xml因此:(注意我认为我使用的是球衣2.0,所以我不需要在这里做任何事情吗?)

<?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" id="WebApp_ID" version="3.1">
  <display-name>RestApi</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>
        <!-- The package where your resource classes are -->
        <param-value>WS</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>MyApplication</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>

...我导入的libs是这样的: enter image description here

  

SEVERE:为servlet MyApplication分配异常   抛出java.lang.ClassNotFoundException:   org.glassfish.jersey.servlet.ServletContainer at   org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)     在   org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)     在   org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:520)     在   org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:501)     在   org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:120)     在   org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1095)     在   org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:817)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)     在   org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)     在   org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)     在   org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:673)     在   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1500)     在   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1456)     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(未知来源)

当我点击http://localhost:8080/PK_WS/api/

时,我在浏览器中返回错误
  

javax.servlet.ServletException:实例化servlet类时出错   org.glassfish.jersey.servlet.ServletContainer     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)     org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)     org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)     org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)     org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:673)     org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1500)     org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1456)     java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)     java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)     org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     java.lang.Thread.run(未知来源)

     

root cause java.lang.ClassNotFoundException:   org.glassfish.jersey.servlet.ServletContainer     org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)     org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)     org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)     org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)     org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)     org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:673)     org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1500)     org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1456)     java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)     java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)     org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     java.lang.Thread.run(未知来源)

1 个答案:

答案 0 :(得分:1)

这在web.xml中是不够的。您仍然需要配置Jersey,因为它是处理JAX-RS处理的运行时。您可以使用web.xml

执行此操作
<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>
        <!-- The package where your resource classes are -->
        <param-value>WS</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>MyApplication</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

或没有web.xml

@ApplicationPath("/api")
public class MyApplication extends ResourceConfig {
    public MyApplication() {
        packages("WS");
    }
}

使用这两个选项,/api将成为应用网址前缀。你可以改变它。

有关详细信息,请查看Jersey用户指南中的Servlet Based Deployment