【问题标题】:How to get URL of REST endpoint?如何获取 REST 端点的 URL?
【发布时间】:2017-07-28 22:10:44
【问题描述】:

谁能解释一下为什么这个 URL 会返回 404?

http://localhost:8080/HelloWorld-1.0-SNAPSHOT/rest/hat

我应该如何传递这个参数以便传入参数 hat 并在输出中查看 Hello hat!

HelloService.java

package com.sentiment360.helloworld;

public class HelloService {

    String createHelloMessage(String name) {
        return "Hello " + name + "!";
    }

}

HelloWorld.java

package com.sentiment360.helloworld;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

/**
 * A simple REST service which is able to say hello to someone using HelloService Please take a look at the web.xml where JAX-RS
 * is enabled
 *
 * @author gbrey@redhat.com
 *
 */

@Path("/")
public class HelloWorld {
    @Inject
    HelloService helloService;

    @GET
    @Path("/json")
    @Produces({ "application/json" })
    public String getHelloWorldJSON() {
        return "{\"result\":\"" + helloService.createHelloMessage("World") + "\"}";
    }

    @GET
    @Path("/xml")
    @Produces({ "application/xml" })
    public String getHelloWorldXML() {
        return "<xml><result>" + helloService.createHelloMessage("World") + "</result></xml>";
    }

}

JAXActivator.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sentiment360.helloworld;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

/**
 * JAXActivator is an arbitrary name, what is important is that javax.ws.rs.core.Application is extended
 * and the @ApplicationPath annotation is used with a "rest" path.  Without this the rest routes linked to
 * from index.html would not be found.
 */
@ApplicationPath("rest")
public class JAXActivator extends Application {
    // Left empty intentionally
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

我的目录结构:

【问题讨论】:

  • ...为什么不呢?您还没有提到您正在部署到 HelloWorld-1.0-SNAPSHOT 上下文,并且我看不到以 /hat 结尾的路由。
  • 抱歉,我正在通过 WildFly 服务器进行部署,hat 是发送到地狱世界的文本
  • 不,不是。那是路径的一部分。它不是查询参数,也不是 POST 有效负载的一部分。
  • 哦,听起来我没有正确设置路径。我应该如何通过 URL 传递参数以便 REST api 接收它?

标签: java rest wildfly-10


【解决方案1】:

让我们尝试匹配您的请求 URI:

http://localhost:8080/HelloWorld-1.0-SNAPSHOT/rest/hat

  • 上下文根是 HelloWorld-1.0-SNAPSHOT,只是 WAR 文件的名称,因为您没有覆盖它。
  • 您的 REST 资源的路径在您的应用程序子类 (JAXActivator) 中配置为 rest。所以直到这一点一切都是正确的。
  • URI 中的下一部分是hat。但是这个路径没有映射到你的资源类中的任何方法;从而产生404 异常。因此,到您的资源类的有效映射是:

    http://localhost:8080/HelloWorld-1.0-SNAPSHOT/rest/json,或

    http://localhost:8080/HelloWorld-1.0-SNAPSHOT/rest/xml

您似乎还想向您的 REST 方法发送参数:

  • http://localhost:8080/HelloWorld-1.0-SNAPSHOT/rest/json/hat

  • http://localhost:8080/HelloWorld-1.0-SNAPSHOT/rest/xml/hat

取决于您是否需要 JSON 或 XML 响应。为此,您必须按如下方式修改 REST 方法:

@GET
@Path("/json/{p}")
@Produces({ "application/json" })
public String getHelloWorldJSON(@PathParam("p") String param) {
    return "{\"result\":\"" + helloService.createHelloMessage(param) + "\"}";
}

@GET
@Path("/xml/{p}")
@Produces({ "application/xml" })
public String getHelloWorldXML(@PathParam("p") String param) {
    return "<xml><result>" + helloService.createHelloMessage(param) + "</result></xml>";
}

这是 JAX-RS 2.0 规范中所述的@PathParam 的定义:

指定要从 URI 查询参数中提取 方法参数、类字段或 bean 属性的值。注释的值标识查询参数的名称。

【讨论】:

    【解决方案2】:

    您使用的网址与您的配置不匹配。

    根据您的配置,您可以请求 2 个可能的资源:

    1. http://your_server/rest/json
    2. http://your_server/rest/xml

    在产生的内容类型上基本不同。

    编辑:好的,我将发布必要的更改,以便您在exact查询的情况下完全获得您要求的输出使用:

    public class HelloWorld {
        @Inject
        HelloService helloService;
    
        @GET
        @Path("/{helloSuffix}")
        public String getHello(@PathParam("helloSuffix") String helloSuffix) {
            return helloService.createHelloMessage(helloSuffix);
        }
    }
    

    JAXActivator.java ...

    @ApplicationPath("HelloWorld-1.0-SNAPSHOT/rest")
    public class JAXActivator extends Application {
    }
    

    【讨论】:

    • 所以我应该使用 URL:http://localhost:8080/HelloWorld-1.0-SNAPSHOT/rest/json/hat?这将返回 Not Found
    • 不,试试 - your_server/rest/json?parameterName=hat 是您应该拥有的 URL 类型,但您的代码需要处理查询参数
    • @Rilcon42 您所说的与您的配置不符。您没有配置任何根据您的请求执行任何动态输出的资源(不可能有 Hello hat!)。看看你的 HelloWorld.java ;这两种资源都只是打印一个硬编码的“Hello World”。如果您想要描述的动态内容,我建议您查看 jaxrs 文档(路径注释和 PathParam)。
    • @Rilcon42 对我的答案添加了一个编辑,并进行了必要的更改以完成您的要求。
    【解决方案3】:

    你应该像这样改变你的端点:

    @GET
    @Path("/rest/{name}")
    @Consumes("application/json")
    @Produces("application/json")
    public String getHelloWorldJSON(@PathParam("name") String name) {
        return "{\"result\":\"" + helloService.createHelloMessage(name) + "\"}";
    }
    
    @GET
    @Path("/rest/{name}")
    @Consumes("application/xml")
    @Produces("application/xml")
    public String getHelloWorldJSON(@PathParam("name") String name) {
        return "<xml><result>" + helloService.createHelloMessage(name) + "</result></xml>";
    }
    

    @Consumes 是从 json 和 xml 输入更改的正确方法。 @Path("rest/{name}") 正在将此端点绑定到 /rest/something,@PathParam("name") 正在将变量路径值绑定到变量名

    如果您的服务器在http://localhost:8080/HelloWorld-1.0-SNAPSHOT 中正确部署,这将起作用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-26
      • 2021-08-08
      • 2012-09-23
      • 1970-01-01
      • 2018-10-24
      • 2016-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多