Java Reference
In-Depth Information
Redirecting to Another Service
Problem
You want to redirect clients from one RESTful service to another and keep attendant details
and metadata such as parameters intact.
Solution
Create a URI using javax.ws.rs.core.UriBuilder that maps the parameters and other data
you want to preserve. Then use Response.temporaryRedirect to return a redirect to the cli-
ent and pass it the URI you've built.
Discussion
The code in Example 8-25 redirects clients from one service to another. It maintains the query
string that was passed in the original request. You have one service, OldService , that re-
sponds on the URI the client actually requests. It redirects to the new service.
Example8-25.OldService.java redirects clients to the service at NewService
package com.soacookbook.rest.response;
import java.net.URI;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
/**
* Redirects the client from the requested URI to another,
* and maintains the query parameters.
*/
@Path("/oldversion")
public class OldService {
private static final String REDIR_PATH =
"http://localhost:8080/restexamples/resources/";
private static final String REDIR_SERVICE = "newversion";
Search WWH ::




Custom Search