2016-09-26 18 views
1

我想問一下關於澤西島申請的基本問題。
是否可以在基本URL中添加和顯示HTML內容?如果可能的話,我該如何實現它?如何顯示Jersey Web Application的基本URL中的內容?

我使用Jersey 2.X Application。
在通常情況下,我擴展了ResourceConfig來實現並將ApplicationPath設置爲「/ api」。 此外,我在Test類中設置資源路徑爲「test」,並用「GET」請求定義testResp()。
我使用maven構建service.war並部署在本地tomcat上,所以我可以訪問http://localhost:8080/service/api/test在瀏覽器中得到結果。

但現在,我想顯示API基本URL一些HTML內容:http://localhost:8080/service/api/
例如,我會放一些推出這項服務,並 用戶可以訪問API基本URL閱讀。
如果可能,我該如何實現它?非常感謝!

以下是一些代碼示例。

澤西應用示例:

@ApplicationPath("/api") 
public class WebApplication extends ResourceConfig { 
    public WebApplication() { 
     /** 
     * register resource, load setting, ...etc 
     */ 
    } 
} 

資源示例:

@Path("test") 
public class Test { 

    @GET 
    public Response testResp() { 
     /** 
     * impliment the action of http://localhost:8080/service/api/test/ 
     */ 
    } 
} 

回答

0

創建@Path("/")@Path("")註解的資源類,然後創建一個資源方法來處理GET請求生成HTML:

@Path("/") 
public class WelcomeResource { 

    @GET 
    @Produces(MediaType.TEXT_HTML) 
    public Response produceHTML() { 
     String html = "<p>Hello, World!</p>"; 
     return Response.ok(html).build(); 
    } 
} 

HTML內容將在/api處提供。

+0

感謝您的回答。我會用這個方法來實現。 –

+0

@ Ringing.Wang我通常不喜歡被定義爲空字符串或斜槓的'@ Path'值,但在這種情況下它會起作用。 –