1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
<chapter id="_PathParam">
<title>@PathParam</title>
<note>
<para>
RESTEasy <link linkend="_NewParam">supports <code>@PathParam</code> annotations with no parameter name.</link>.
</para>
</note>
<para>
@PathParam is a parameter annotation which allows you to map variable URI path fragments into your method call.
</para>
<para>
<programlisting>
@Path("/library")
public class Library {
@GET
@Path("/book/{isbn}")
public String getBook(@PathParam("isbn") String id) {
// search my database and get a string representation and return it
}
}
</programlisting>
</para>
<para>
What this allows you to do is embed variable identification within the URIs of your resources. In the above example, an isbn URI parameter is used
to pass information about the book we want to access. The parameter type you inject into can be any primitive type, a String, or any Java object that has
a constructor that takes a String parameter, or a static valueOf method that takes a String as a parameter. For example, lets say we wanted isbn to be a real object. We could do:
</para>
<para>
<programlisting>
@GET
@Path("/book/{isbn}")
public String getBook(@PathParam("isbn") ISBN id) {...}
public class ISBN {
public ISBN(String str) {...}
}
</programlisting>
</para>
<para>
Or instead of a public String constructors, have a valueOf method:
</para>
<para>
<programlisting>
public class ISBN {
public static ISBN valueOf(String isbn) {...}
}
</programlisting>
</para>
<para>
</para>
<para>
</para>
<sect1 id="Advanced__PathParam_and_Regular_Expressions">
<title>Advanced @PathParam and Regular Expressions</title>
<para>
There are a few more complicated uses of @PathParams not discussed in the previous section.
</para>
<para>
</para>
<para>
</para>
<para>
You are allowed to specify one or more path params embedded in one URI segment. Here are some examples:
</para>
<para>
<programlisting>
1. @Path("/aaa{param}bbb")
2. @Path("/{name}-{zip}")
3. @Path("/foo{name}-{zip}bar")
</programlisting>
</para>
<para>
So, a URI of "/aaa111bbb" would match #1. "/bill-02115" would match #2. "foobill-02115bar" would match #3.
</para>
<para>
</para>
<para>
We discussed before how you can use regular expression patterns within @Path values.
</para>
<para>
<programlisting>
@GET
@Path("/aaa{param:b+}/{many:.*}/stuff")
public String getIt(@PathParam("param") String bs, @PathParam("many") String many) {...}
</programlisting>
</para>
<para>
For the following requests, lets see what the values of the "param" and "many" @PathParams would be:
</para>
<para>
<table frame="topbot">
<tgroup cols="3" rowsep="1" colsep="1">
<thead>
<row>
<entry>
Request</entry>
<entry>
param</entry>
<entry>
many</entry>
</row>
</thead>
<tbody>
<row>
<entry>
GET /aaabb/some/stuff</entry>
<entry>
bb</entry>
<entry>
some</entry>
</row>
<row>
<entry>
GET /aaab/a/lot/of/stuff</entry>
<entry>
b</entry>
<entry>
a/lot/of</entry>
</row>
</tbody>
</tgroup>
</table>
</para>
<para>
</para>
</sect1>
<sect1 id="_PathParam_and_PathSegment">
<title>@PathParam and PathSegment</title>
<para>
The specification has a very simple abstraction for examining a fragment of the URI path being invoked on javax.ws.rs.core.PathSegment:
</para>
<para>
<programlisting>
public interface PathSegment {
/**
* Get the path segment.
* <p>
* @return the path segment
*/
String getPath();
/**
* Get a map of the matrix parameters associated with the path segment
* @return the map of matrix parameters
*/
MultivaluedMap<String, String> getMatrixParameters();
}
</programlisting>
</para>
<para>
You can have RESTEasy inject a PathSegment instead of a value with your @PathParam.
</para>
<para>
<programlisting>
@GET
@Path("/book/{id}")
public String getBook(@PathParam("id") PathSegment id) {...}
</programlisting>
</para>
<para>
This is very useful if you have a bunch of @PathParams that use matrix parameters. The idea of matrix parameters is
that they are an arbitrary set of name-value pairs embedded in a uri path segment. The PathSegment object gives you
access to these parameters. See also MatrixParam.
</para>
<para>
A matrix parameter example is:
</para>
<para>
GET http://host.com/library/book;name=EJB 3.0;author=Bill Burke
</para>
<para>
The basic idea of matrix parameters is that it represents resources that are addressable by their attributes as well as their raw id.
</para>
<para>
</para>
<para>
</para>
<para>
</para>
<para>
</para>
<para>
</para>
<para>
</para>
<para>
</para>
<para>
</para>
<para>
</para>
<para>
</para>
<para>
</para>
</sect1>
</chapter>
|