Java Reference
In-Depth Information
3. Use the queryParam method to create a new instance of the builder that extracts the query
parameter called “user” and stores its value in the {value} part of the template. This will
handle the equals sign appropriately.
4. When you're done with the builder, call build, and it will put together all of these parts
into a URI that can be passed to the response redirector.
NOTE
See section 3.7 of the JAX-RS specification for the rules on request preprocessing and URI templates.
Also, if you need to have anything unusual in the query string or other data with your request,
the UriBuilder class generally behaves in the way you would hope. For example, UriBuild-
er.fromPath("{p}").build("foo#bar") will result in the encoding of the # sign so that the
result is foo%23bar .
Redirecting can be useful when you are migrating clients to newer versions of your service.
Checking for the data that they may have provided and then mapping default values if any-
thing is missing can be helpful in this area. Example 8-26 shows a service that accepts the
redirect returned from the old service in Example 8-25 and returns the result to the client.
Example8-26.NewService.java
package com.soacookbook.rest.response;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
@Path("/newversion")
public class NewService {
@GET
public String doGet(@QueryParam("user") String user) {
System.out.println("In New doGet.");
return "Hello from New Version, " + user;
}
}
Now open a browser and point it to http://localhost:8080/restexamples/resources/oldver-
sion?user=Indiana Jones .
Your browser's address bar will change, and you'll see this: http://localhost:8080/res-
texamples/resources/newversion?user=Indiana+Jones . And of course, the message you expect
is displayed, with the proper formatting: Hello from New Version, Indiana Jones .
Search WWH ::




Custom Search