Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the base url of my server with JAX-RS

Tags:

url

jax-rs

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.

like image 636
tribal Avatar asked Oct 04 '11 18:10

tribal


People also ask

How do I find the URL of a base URL?

There's nothing in the Android URI class that gives you the base URL directly- As gnuf suggests, you'd have to construct it as the protocol + getHost(). The string parsing way might be easier and let you avoid stuffing everything in a try/catch block.

What is JAX-RS used for?

JAX-RS is a Java programming language API designed to make it easy to develop applications that use the REST architecture. The JAX-RS API uses Java programming language annotations to simplify the development of RESTful web services.

What is resource in JAX-RS?

Root resource classes are "plain old Java objects" (POJOs) that are either annotated with @Path or have at least one method annotated with @Path or a request method designator, such as @GET , @PUT , @POST , or @DELETE .


1 Answers

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.

like image 108
Nicolas Zozol Avatar answered Sep 20 '22 06:09

Nicolas Zozol