如何获取REST端点的URL?

时间:2017-07-28 22:10:44

标签: java rest wildfly-10

有人能解释为什么这个网址会返回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

3 个答案:

答案 0 :(得分:2)

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

  

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

  • 上下文根是HelloWorld-1.0-SNAPSHOT,只是您没有覆盖它的WAR文件的名称。
  • REST资源的路径在Application子类(rest)中配置为JAXActivator。所以在这一点上一切都是正确的。
  • 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属性的值。注释的值标识查询参数的名称。

答案 1 :(得分:0)

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

根据您的配置,您有2个可能的资源要求:

  1. http://your_server/rest/json
  2. http://your_server/rest/xml
  3. 基本上与生成的内容类型不同。

    修改:好的,我会发布必要的更改,以便您根据自己想要的完全查询提出完全您要求的输出使用方法:

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

答案 2 :(得分:0)

您应该像这样更改端点:

@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

中正确部署,这将有效