java - 如何获取 REST 端点的 URL?

标签 java rest wildfly-10

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

我的目录结构:

my directory structure

最佳答案

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

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

  • 上下文根是 HelloWorld-1.0-SNAPSHOT,只是 WAR 文件的名称,因为您尚未覆盖它。
  • REST 资源的路径在您的应用程序子类 (JAXActivator) 中配置为 rest。所以到目前为止一切都是正确的。
  • URI 中的下一部分是帽子。但是这个路径没有映射到你的资源类中的任何方法;从而产生 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 的定义:

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.

关于java - 如何获取 REST 端点的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45383083/

相关文章:

java - DAO 的设计策略应该如何进行更新操作?

javascript - 如何为从移动和 JavaScript Web 应用程序访问的 rest API 实现基于 token 的 OAuth 2.0 身份验证

rest - 在 REST 服务上使用空 PUT

rest - 通过代理连接到本地主机重定向

maven - 将依赖项添加到 pom.xml 后出现 Tika-Parsers 错误

java - Jersey 未将响应映射到 JAVA 对象

jsf - 无法将 EJB 注入(inject) JSF 阶段监听器

java - 无限跨度 : locking in remote transactional cache

java - 如何分析Java应用程序在x64操作系统上引起的系统卡住?

java - 尝试在构造函数中传递数组