18

How do I get the base url of my server with JAX-RS? Basically I want ""http://localhost:8080/.." when the program is on localhost and "http://www.theSite.com/..." when the program is on a live server. I am using Jersey Framework.

tribal
  • 653
  • 2
  • 13
  • 26

2 Answers2

40

Yes, you may use myUri = uri.getBaseUri();

Here how you get the Uri object :

@Path("myresource")
public class MyResource{

  @Context
  UriInfo uri;

  @GET
  public String myresponse(){
    URI myUri = uri.getBaseUri();
    return ...
  }
}

You will have plenty of informations with UriInfo. Check here the javadoc.

Christoph Böhme
  • 3,766
  • 1
  • 19
  • 29
Nicolas Zozol
  • 6,910
  • 3
  • 50
  • 74
7

Use getBaseUri() of @Context UriInfo.

Tarlog
  • 10,024
  • 2
  • 43
  • 67