Jonathan S. Fisher
Jonathan S. Fisher

Reputation: 8807

In Java EE 6, how do I get the URL of a JAX-RS endpoint programmatically?

TomEE 1.7.2, Java EE 6, Java 8

I have this JAX-RS Application:

@ApplicationPath("/api")
public class CallStatsCatcherApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        return new HashSet<Class<?>>(Arrays.asList(RestEndpoint.class));
    }
}

@ApplicationScoped
@Path("/rest")
public class RestEndpoint {
    @GET
    public String echo(@QueryParam("foo") String foo) {
        return foo;
    }
}

During startup, TomEE prints:

INFO:               GET http://localhost:8080/test-application/api/rest/      ->      String echo(String)

How can I get that URL programmatically during startup? I'd like to create a framework that advertises this URL locally on the network.

Upvotes: 3

Views: 1615

Answers (2)

Silvio Lucas
Silvio Lucas

Reputation: 2059

You can use the UriInfo class in conjunction with UriBuilder class to mount or create URLs using the current path.

There is similar question in stackoverflow: getting the base url...

Upvotes: 0

ostry
ostry

Reputation: 117

To get server adress part you can try use something like that: Java EE: how to get the URL of my application?

But if you need IP address in startup bean, you can try use:

InetAddress ip = InetAddress.getLocalHost();
String ipAddress = ip.getHostAddress();`

Another option is to use:

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer()`

But it may be associated with the server platform and you will need to search the property.

Upvotes: 1

Related Questions