Rohan Sahgal wrote:
> Hi,
> I was just seeing the JAX-RS api and its spec.
>
> For @HttpMethod the api reads "A Java method annotated with a runtime
> annotation that is itself annotated with this annotation will be used to
> handle HTTP requests of the indicated HTTP method".
> Does this mean that say I have an annotation MyAnnotation, I can now
> @MyAnnotation instead of @GET (or any other http method).
Sort of. A better example: a developer can specify say WebDAV methods
like @PROPPATCH.
> Can someone
> give a small example of the syntax, I cannot find it used in any of the
> examples.
This is the code for @GET:
package javax.ws.rs;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Indicates that the annotated method responds to HTTP GET requests
* @see HttpMethod
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod(HttpMethod.GET)
public @interface GET {
}
For @PROPPATCH this would be:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("PROPPATCH")
public @interface PROPPATCH {
}
> Is the purpose of this annotation to somehow extend the functionality of
> @GET?
>
No, the purpose is to enable open ended support for additional HTTP methods.
> @Encoded is to disable decoding for parameters. Does this mean that say
> I have a method that echo's back a string that is sent in the URL, if
> now I annotate that method with @Encoded it will no longer decode it,
> even if I pass it in the URL?
>
> @GET
> @ProduceMime("text/plain")
> @Path("echo")
> @Encoded
> public String echo(@DefaultValue("no string sent")
> @QueryParam("string")String echoString){
> return echoString;
> }
>
> Then will this method always return "no string sent".
>
No. The "no string sent" will only be sent if there us no query
parameter "string" in the URI.
@Encoded refers to the percent encoding in the URI. The URI syntax
disallows direct encoding of some characters, and instead they need to
be percent encoded, which is why you sometimes see %20 in URIs.
In your example if the URI contains the query parameter string whose
value contains percent encoded characters then the Java parameter
"echoString" would also contain those characters.
> Sorry I am unsure of the annotations to use in my application and wanted
> to understand all of them before deciding on the design, I could not
> find these two used in any of the samples.
>
That is OK.
If using Jersey, i recommend you experiment, see the end of the email
for a simple example.
Paul.
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.net.httpserver.HttpServer;
import java.net.URI;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.Encoded;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.UriBuilder;
public class Main {
@Path("/")
public static class R {
@Encoded
@GET
public String get(
@DefaultValue("no string sent")
@QueryParam("string") String echoString) {
return echoString;
}
}
public static void client() throws Exception {
Client c = Client.create();
WebResource r = c.resource("
http://localhost:9999/");
assertEquals("no string sent", r.get(String.class));
URI u = UriBuilder.fromPath("").queryParam("string", "hash
#").build();
assertEquals("hash+%23", r.uri(u).get(String.class));
}
public static void assertEquals(Object o1, Object o2) {
if (!o1.equals(o2)) {
System.out.println(o1 + " != " + o2);
throw new RuntimeException();
}
}
public static void main(String[] args) throws Exception {
HttpServer s = HttpServerFactory.create("
http://localhost:9999/");
s.start();
try {
client();
} finally {
s.stop(0);
}
}
}
--
| ? + ? = To question
----------------\
Paul Sandoz
x38109
+33-4-76188109