有人可以解釋爲什麼這個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>
我的目錄結構:
...爲什麼不呢?你沒有提到你正在部署到'HelloWorld-1.0-SNAPSHOT'上下文,並且沒有我能看到以'/ hat'結尾的路由。 – Makoto
我的歉意,我通過一個WildFly服務器進行部署,並且'帽子'是發送到helloworld的文本 – Rilcon42
不,它不是。這是路徑的一部分。它不是查詢參數,它不是POST有效負載的一部分。 – Makoto