【发布时间】: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