aime
aime

Reputation: 247

Jersey 2 - get base URI on Client

I need to inject UriInfo into the Client, but can't find solution how to do that. The main objective is to get base URI (localhost:port) when creating WebTarget. The fact is that application is used by different web-clients and explicit URI is needed. More over I'd like to test Client-Resource communication thus I need to get strict URI ni my test classes too. Application is multi-module Maven project. Resources and clients are located in different modules.
To be mush clear I describe project structure below (please do not pay attention to folders structure - it is depicted in a simple form):

--service-module
       \Resource.java
       \web.xml
--client-module
       \Client.java
--client-test-module
       \ClientResourceIntegrationTest.java

Client.java

public class Client {

    @Context
    UriInfo uriInfo;

    public Result getResult(String userName, Filter queryFilter) {
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.register(Filter.class);
        Client client = ClientBuilder.newClient(clientConfig);

        WebTarget webTarget = client
                //need to change on dynamically retrieved URI
                .target("http://localhost:1234/")
                .path("all/")
                .path(userName);

        Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
        Response response = invocationBuilder.post(Entity.entity(queryFilter, MediaType.APPLICATION_JSON));
        int responseStatus = response.getStatus();
        Result queryResult = response.readEntity(Result.class);
        response.close();
        return queryResult;
    }

}

Resource.java

@Path("/")
public class Resource {

    @Context
    UriInfo uriInfo;

    @POST
    @Path("all/{user}")
    @Produces(MediaType.APPLICATION_JSON)
    public Result getAll(@PathParam("user") String userName, Filter filter) {
        List<Map<String, Object>> attributes = new ArrayList<Map<String, Object>>();
        Map<String, Object> values = new HashMap<String, Object>();
        values.put("value", 1);
        attributes.add(values);

        Result result = new Result(attributes);
        return result;
    }

}

Jersey's ServletContainer is defined in web.xml that located in service-module. Scanned pakages are only resources located in this module (thus UriInfo is injected into Resource object). Instances of the remaining classes (which includes Clients) are created without IoC-container (obviously UriInfo is null). So, I don't know how to get URI in that case. If you have any suggestion where to read it or even how to do that - please leave a comment.

Upvotes: 0

Views: 1020

Answers (1)

Guenther
Guenther

Reputation: 2045

I'm not sure I understand your question. When you deploy a web application, you can define the context root in the EAR file. That context root won't change, if you install the same EAR on a different application server, unless the deployer configures a different context root.

The client could potentially run on a different machine so you need to find a way to configure the client with the information where to find the server. This is usually done by property files or config tables in a database. Simply setup config files for all suitable environments and feed them to the client.

Upvotes: 1

Related Questions