Bạn có thể tạo dịch vụ RESTful proxy và sử dụng dịch vụ này làm điểm nhập cho tất cả các dịch vụ RESTful khác của bạn. Proxy này có thể nhận các yêu cầu, thực hiện bất kỳ việc xử lý trước nào, gọi dịch vụ RESTful cần thiết, xử lý đáp ứng và sau đó trả lại một cái gì đó cho người gọi.
Tôi đã thiết lập như thế này trong một dự án mà tôi đang thực hiện. Proxy thực hiện các chức năng như xác thực, ủy quyền và ghi nhật ký kiểm tra. Tôi có thể đi sâu vào chi tiết nếu bạn thích.
Chỉnh sửa:
Đây là ý tưởng về cách bạn có thể muốn triển khai proxy hỗ trợ các yêu cầu GET;
@Path("/proxy")
public class Proxy
{
private Logger log = Logger.getLogger(Proxy.class);
@Context private UriInfo uriInfo;
@GET
@Path("/{webService}/{method}")
public Response doProxy(@Context HttpServletRequest req,
@PathParam("webService") String webService,
@PathParam("method") String method)
{
log.debug("log request details");
//implement this method to work out the URL of your end service
String url = constructURL(req, uriInfo, webService, method);
//Do any actions here before calling the end service
Client client = Client.create();
WebResource resource = client.resource(url);
try
{
ClientResponse response = resource.get(ClientResponse.class);
int status = response.getStatus();
String responseData = response.getEntity(String.class);
log.debug("log response details");
//Do any actions here after getting the response from the end service,
//but before you send the response back to the caller.
return Response.status(status).entity(responseData).build();
}
catch (Throwable t)
{
//Global exception handler here
//remember to return a Response of some kind.
}
}
Câu trả lời được chấp nhận không đề cập đến bạn mong muốn có các hành động yêu cầu trước và sau. Bạn đã bao giờ đến một giải pháp cho điều đó chưa? –
Tùy chỉnh [Bộ lọc Servlet] (http://download.oracle.com/javaee/5/api/javax/servlet/Filter.html) là cách để thực hiện. – yegor256