2013-11-29 54 views
0

我想在GlassFish的EAR中部署一些模塊並從java客戶端調用服務。如何從Java客戶端調用我的REST服務?

這些類有一個EJB模塊(「ejbees.jar」)。

@Stateless 
@Path("hello") 
public class HelloREST{ 

    @GET 
    @Produces({MediaType.TEXT_PLAIN}) 
    public String sayHello(){ 
     return "Hello REST"; 
    } 
} 

@ApplicationPath("example/*") 
public class ApplicationConfig extends Application{ 

    @Override 
    public Set<Class<?>> getClasses() { 
     Set<Class<?>> c = new HashSet(); 
     c.add(HelloREST.class); 
     return c; 
} 

EJB模塊和一些其他JAR被包裝稱爲「simpleapp.ear」並部署到GlassFish的EAR內部。 GlasFish列出了應用程序「simpleapp」,其中包含一個模塊「ejbees.jar」和一個組件「HelloREST」。

這是我到目前爲止稱爲HelloREST的代碼。

public void callHelloRest(String uri){ 
    WebTarget wr = ClientBuilder.newClient.target(uri); 
    String hellomessage = wr.path("hello").request(MediaType.TEXT_PLAIN).get(String.class);//Here comes HTTP 404 not found error. 
} 

中的URI我想:

http://localhost:8080/example 
http://localhost:8080/simpleapp/example 
http://localhost:8080/simpleapp/ejbees/example 
http://localhost:8080/ejbees/simpleapp/example 

但我總是得到一個HTTP 404未找到錯誤。如何正確的URI看起來像?或者在EAR內部的EJB中包裝REST服務是否是錯誤的?有沒有辦法向GlassFish詢問REST服務的URI?

在此先感謝。

回答

0

我ApplicationConfig類看起來是這樣的:

@ApplicationPath("rest") 
public class ApplicationConfig extends Application { 
} 

REST服務看起來是這樣的:

@Stateless 
@Path("/hello") 
public class HelloRest {   
    @GET 
    @Path("/getHello") 
    public String getHello(@QueryParam(value = "name") String name) throws Exception 
     return "Hello, " + name; 
    } 
} 

現在,您可以訪問它想:

http://localhost:8080/rest/hello/getHello?name=RAM 

沒有測試這個例子,但這很像我在我的應用程序中使用它。