JAX-RS,GlassFish,Eclipse。簡單的Web服務(wù)不起作用
您的代碼似乎還可以,因此問題很可能是您尚未定義服務(wù)的基本URL。您需要告訴JAX-RS(Jersey是GlassFish的實(shí)現(xiàn)),它必須將哪個URL模式作為端點(diǎn)(基本URL)進(jìn)行攔截。
實(shí)現(xiàn)此目的的一種方法是使用應(yīng)用程序類,該類可以添加到項(xiàng)目中的任何程序包中(您也可以在web.xml文件中定義必要的內(nèi)容,我不會介紹)。以下是代碼:
import javax.ws.rs.ApplicationPath;import javax.ws.rs.core.Application;@ApplicationPath('your-base-url')public class ApplicationConfig extends Application {}
您可以使用@ApplicationPath批注定義基本URL。然后,您可以在以下位置訪問服務(wù)http://127.0.0.1:8080/hello-rest/your-base-url/hello
解決方法我試圖在我的機(jī)器上運(yùn)行一個簡單的“ Hello World” RESTful Web服務(wù)。我使用Eclipse Kepler和GlassFish4.0。我能夠部署該服務(wù),并且可以在GlassFish的管理頁面上看到該服務(wù),但是當(dāng)我嘗試訪問該服務(wù)時,出現(xiàn)以下錯誤:“ HTTP狀態(tài)404-找不到”。
以下是簡單服務(wù)的代碼:
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) { }}
為了訪問該服務(wù),我嘗試以下URL:,http://127.0.0.1:8080/hello-rest/hello其中hello-restEclipse項(xiàng)目的名稱和GlassFish管理頁面建議的根路徑。
相關(guān)文章:
