2017-07-28 114 views
0

有人可以解釋爲什麼這個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> 

我的目錄結構:

my directory structure

+1

...爲什麼不呢?你沒有提到你正在部署到'HelloWorld-1.0-SNAPSHOT'上下文,並且沒有我能看到以'/ hat'結尾的路由。 – Makoto

+0

我的歉意,我通過一個WildFly服務器進行部署,並且'帽子'是發送到helloworld的文本 – Rilcon42

+0

不,它不是。這是路徑的一部分。它不是查詢參數,它不是POST有效負載的一部分。 – Makoto

回答

0

您使用的是不符合配置的URL。

鑑於你的配置,你有2個可能的資源來請求:

  1. http://your_server/rest/json
  2. http://your_server/rest/xml

這在生產的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 { 
} 
+0

所以我應該使用URL:'http:// localhost:8080/HelloWorld-1.0-SNAPSHOT/rest/json/hat'?這將返回'Not Found' – Rilcon42

+0

不,試試 - http:// your_server/rest/json?parameterName = hat是你應該擁有的URL的類型,但是你的代碼需要處理查詢參數 –

+0

@ Rilcon42你說什麼不符合你的配置。您沒有配置任何根據您的請求執行任何動態輸出的資源(不可能有一個Hello帽子!)。看看你的HelloWorld.java;這兩個資源只是打印一個硬編碼的「Hello World」。如果你想要描述那些動態的東西,我建議你看一下jaxrs文檔(Path註解和PathParam)。 –

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

2

正確部署讓我們試着匹配您的請求URI:

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

  • 上下文根是HelloWorld-1.0-SNAPSHOT,只是WAR文件的名稱,因爲您沒有覆蓋它。
  • 您的REST資源的路徑在應用程序子類中配置爲restJAXActivator)。所以在這點之前一切都是正確的。
  • 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查詢參數中提取。註釋的值標識查詢參數的名稱。