2

I'm defining a RESTful WebService in Java.

It takes as input:

  • an username (for instance, jdoe);
  • an URL (for instance: https://blablablabla.io/sample?boh=mah).

By using GET method of the HTTP protocol, it should produce a JSON file.

Which is the best way to pass these params?

In this particular case, is there a best practice to follow in order to properly pass an URL as param?

vdenotaris
  • 11,848
  • 23
  • 69
  • 121
  • FYI: Have a look at Dropwizard, you have a simple REST Framework including Jetty and other useful stuff. Easy to use and you have a good description how to start -> http://dropwizard.io/getting-started.html – user3280180 Oct 16 '14 at 14:32
  • I'm yet using RESTEasy on JBoss. – vdenotaris Oct 16 '14 at 14:39

3 Answers3

3

You must first encode it, you can use

URLEncoder.encode("url");
Rocky Pulley
  • 20,107
  • 18
  • 63
  • 98
1

If you want to define an http GET method, then the only way to pass parameters to it is through the URI query string (ie. ?x=y&...).

This is because GET calls can not take in a message body.

If you want to pass in more complicated information, you will need to use POST, PUT, or some other method. Though, if you are actually just getting information (semantically), then you shouldn't use anything but GET.

Luke
  • 7,719
  • 3
  • 43
  • 74
  • Thus, should I pass the URL as queryparam or pathparam? – vdenotaris Oct 16 '14 at 15:16
  • 1
    @vdenotaris "I'd recommend putting any required parameters in the path, and any optional parameters should almost certainly be query string parameters." See: [this answer](http://stackoverflow.com/a/11569077/2479481) – Luke Oct 16 '14 at 15:18
-3

Also you can use Path parameters

This is example

@Path("/users")
public class UserResorce {

      @GET
      @Path("/{username}")
      @Produces(MediaType.APPLICATION_JSON)
      public String getUser(@PathParam("username") String username)){

      }
}


The url is http://domain_name/your_application_path/users/username