如何获取REST端点的URL?
有人可以解释为什么这个URL返回404?如何获取REST端点的URL?
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 [email protected]
*
*/
@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>";
}
}
JAXActiva
tor.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个可能的资源来请求:
这在生产的contentType基本上是不同的。
编辑:好吧,我将让你有发布必要的修改正是你问输出给出的确切查询你想使用:
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' – Rilcon42
不,试试 - http:// your_server/rest/json?parameterName = hat是你应该拥有的URL的类型,但是你的代码需要处理查询参数 –
@ Rilcon42你说什么不符合你的配置。您没有配置任何根据您的请求执行任何动态输出的资源(不可能有一个Hello帽子!)。看看你的HelloWorld.java;这两个资源只是打印一个硬编码的“Hello World”。如果你想要描述那些动态的东西,我建议你看一下jaxrs文档(Path注解和PathParam)。 –
你应该改变你的端点像这样:
@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:
- 上下文根是
HelloWorld-1.0-SNAPSHOT
,只是WAR文件的名称,因为您没有覆盖它。 - 您的REST资源的路径在应用程序子类中配置为
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>";
}
这里是@PathParam
的定义在JAX-RS陈述2.0规范:
指定的值一个方法参数,类字段或bean属性将从URI查询参数中提取。注释的值标识查询参数的名称。
...为什么不呢?你没有提到你正在部署到'HelloWorld-1.0-SNAPSHOT'上下文,并且没有我能看到以'/ hat'结尾的路由。 – Makoto
我的歉意,我通过一个WildFly服务器进行部署,并且'帽子'是发送到helloworld的文本 – Rilcon42
不,它不是。这是路径的一部分。它不是查询参数,它不是POST有效负载的一部分。 – Makoto