您的位置:首頁技術文章
文章詳情頁

JAX-RS,GlassFish,Eclipse。簡單的Web服務不起作用

瀏覽:88日期:2024-05-03 11:14:35
如何解決JAX-RS,GlassFish,Eclipse。簡單的Web服務不起作用?

您的代碼似乎還可以,因此問題很可能是您尚未定義服務的基本URL。您需要告訴JAX-RS(Jersey是GlassFish的實現),它必須將哪個URL模式作為端點(基本URL)進行攔截。

實現此目的的一種方法是使用應用程序類,該類可以添加到項目中的任何程序包中(您也可以在web.xml文件中定義必要的內容,我不會介紹)。以下是代碼:

import javax.ws.rs.ApplicationPath;import javax.ws.rs.core.Application;@ApplicationPath('your-base-url')public class ApplicationConfig extends Application {}

您可以使用@ApplicationPath批注定義基本URL。然后,您可以在以下位置訪問服務http://127.0.0.1:8080/hello-rest/your-base-url/hello

解決方法

我試圖在我的機器上運行一個簡單的“ Hello World” RESTful Web服務。我使用Eclipse Kepler和GlassFish4.0。我能夠部署該服務,并且可以在GlassFish的管理頁面上看到該服務,但是當我嘗試訪問該服務時,出現以下錯誤:“ HTTP狀態404-找不到”。

以下是簡單服務的代碼:

import javax.ws.rs.Consumes;import javax.ws.rs.GET;import javax.ws.rs.PUT;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.Context;import javax.ws.rs.core.UriInfo;@Path('hello')public class HelloRest { @SuppressWarnings('unused') @Context private UriInfo context; /** * Default constructor. */ public HelloRest() {// TODO Auto-generated constructor stub } /** * Retrieves representation of an instance of HelloRest * @return an instance of String */ @GET @Produces('application/xml') public String getXml() {// TODO return proper representation objectreturn '<greeting>Hello REST</greeting>'; } /** * PUT method for updating or creating an instance of HelloRest * @param content representation for the resource * @return an HTTP response with content of the updated or created resource. */ @PUT @Consumes('application/xml') public void putXml(String content) { }}

為了訪問該服務,我嘗試以下URL:,http://127.0.0.1:8080/hello-rest/hello其中hello-restEclipse項目的名稱和GlassFish管理頁面建議的根路徑。

標簽: web
相關文章:
国产综合久久一区二区三区