如何获取 REST 端点的 URL?

How to get URL of REST endpoint?

有人可以解释为什么这个 URL returns 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>

我的目录结构:

您使用的 url 与您的配置不匹配。

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

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

生成的内容类型基本不同。

编辑: 好的,我将 post 进行必要的更改,以便您完全 得到您要求的输出确切 您要使用的查询:

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 {
}

您应该像这样更改端点:

@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

中正确部署,这将有效

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

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

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

    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 的定义:

Specifies that the value of a method parameter, class field, or bean property is to be extracted from a URI query parameter. The value of the annotation identifies the name of a query parameter.