无法访问 JAX-RS api

巴里

我看了很多,但似乎无法让我的 JAX-RS API 工作。

我有一个部署到 tomcat(在 docker 中运行)的 WAR 文件,我可以访问欢迎页面,但是如果我尝试访问 REST API url,我总是会收到 404 NotFound 响应。

也许有人可以查看我的东西并告诉我是否有任何遗漏或错误(将不胜感激):

例子.java

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

}

示例应用程序.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)。

迪娜·博格丹

你应该打电话/rest/example/all而不是/rest/example.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章