无法访问JAX-RS API

时间:2019-05-12 13:05:38

标签: java jax-rs

我看起来很多,但似乎无法使我的JAX-RS API正常工作。

我有一个部署到tomcat的WAR文件(在docker中运行),并且能够访问欢迎页面,但是如果尝试访问REST API网址,我总是会收到404 NotFound Response。

也许有人可以看我的东西,并告诉我是否有任何缺失或错误(非常感谢):

Example.java

@Path("/example")
@Transactional
public class Example {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getExample() {
        return "good";
    }

}

ExampleApplication.java

@ApplicationPath("/rest")
public class ExampleApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> endpoints = new HashSet<Class<?>>();
        endpoints.add(Example.class);
        return endpoints;
    }

}

Web.xml(在Jax-RS教程中提到,如果我有Application类,则不需要servlet映射。

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>presentation</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
   <filter>
        <filter-name>UrlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
        <init-param>
            <param-name>logLevel</param-name>
            <param-value>DEBUG</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>UrlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

urlrewrite.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
        "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">

<urlrewrite>
    <!-- do not redirect references to static resources -->
    <rule>
        <from>/assets/*</from>
        <to last="true">-</to>
    </rule>
    <!-- do not redirect references to REST api -->
    <rule>
        <from>/rest/*</from>
        <to last="true">-</to>
    </rule>
    <!-- redirect everything else -->
    <rule match-type="regex" enabled="true">
        <condition type="request-filename" operator="notfile"/>
        <condition type="request-filename" operator="notdir"/>
        <condition type="request-uri" operator="notequal">.*(\.html|\.js|\.css|\.ico)$</condition>
        <from>^/(.*)$</from>
        <to last="true">/index.html</to>
    </rule>
</urlrewrite>

我的邮递员要求: https://imgur.com/a/TO9FGua

在这里您可以看到映射有效,因为/ rest / *以外的所有内容都映射到了欢迎页面(我已经使用/ somethingelse / sad / asd对其进行了尝试)。

1 个答案:

答案 0 :(得分:2)

您应该致电/rest/example/all而不是/rest/example