简体   繁体   中英

Rest Service URI mapping to Java Inner Classes

I have to model the rest service operations where I am trying to create a URI to hit Java Inner classes. Let me know if this can be achieved using Rest Web Service? I am using rest easy for my services.

Edit: (using code provided by answer from peeskillet)

javax.ws.rs.ProcessingException: Unable to invoke request
at  org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:287)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:407)

Caused by: org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8081 refused
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:190)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:643)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:283)
... 26 more
Caused by: java.net.ConnectException: Connection refused

Please advise if a resource class can be made as Inner class to other class

Yes it is possible, but we cannot simply annotate the inner class path @Path and expect the call to get there. We need to use "Sub-Resource Locators". Basically, it can be a method only annotated with @Path , that will return an instance of the inner resource class. SOmething like

@Path("/messages")
public class MessageResource {

    @GET
    @Path("/{id}")
    public Response getMessage(@PathParam("id") int id) {
        return Response.ok("messages, id: " + id).build();
    }

    @Path("/{id}/comments")
    public CommentsResource getComments() {
        return new CommentsResource();
    }

    public class CommentsResource {

        @GET
        @Path("/{id}")
        public Response getComment(@PathParam("id") int id) {
            return Response.ok("Hello Sub-Resource Locators").build();
        }
    }
}

We can test it

@Test
public void testResteasy() {
    WebTarget target = client.target(TestPortProvider.generateURL(BASE_URI))
            .path("messages").path("1234").path("comments").path("5678");
    Response response = target.request().get();
    System.out.println("Status:" + response.getStatus());
    System.out.println("Response: " + response.readEntity(String.class));
    response.close();
}

And we'll get the response Hello Sub-Resource Locators


Further Reading


EDIT:

Base on the error you've provided, the TestPortProvider will use port 8081, and start off your URI with http://localhost:8081 . What you pass to gererateURL will be appended. Normally I use this with embedded server tests. But if you are running the actual server, it will most likely be on port 8080. I would suggest just passing the String base URL instead, something like:

WebTarget target = client.target("http://localhost:8080/MyApp/rest")
        .path("messages").path("1234").path("comments").path("5678");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM