replaced the use of RestServerConfig with ServerConfig.
Added an example query QueryOrdersByName and its use in a client.
This commit is contained in:
parent
a12286698b
commit
4d873d5dd7
|
@ -22,6 +22,7 @@ import com.google.gson.rest.definition.RestCallSpec;
|
|||
import com.google.gson.rest.definition.RestRequest;
|
||||
import com.google.gson.rest.definition.RestResource;
|
||||
import com.google.gson.rest.definition.RestResponse;
|
||||
import com.google.gson.webservice.client.ServerConfig;
|
||||
import com.google.gson.webservice.definition.WebServiceSystemException;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -37,15 +38,15 @@ import java.util.logging.Logger;
|
|||
* @author inder
|
||||
*/
|
||||
public class RestClientStub {
|
||||
private final RestServerConfig config;
|
||||
private final ServerConfig config;
|
||||
private final Logger logger;
|
||||
private final Level logLevel;
|
||||
|
||||
public RestClientStub(RestServerConfig serverConfig) {
|
||||
public RestClientStub(ServerConfig serverConfig) {
|
||||
this(serverConfig, null);
|
||||
}
|
||||
|
||||
public RestClientStub(RestServerConfig serverConfig, Level logLevel) {
|
||||
public RestClientStub(ServerConfig serverConfig, Level logLevel) {
|
||||
this.config = serverConfig;
|
||||
this.logger = logLevel == null ? null : Logger.getLogger(RestClientStub.class.getName());
|
||||
this.logLevel = logLevel;
|
||||
|
|
|
@ -18,15 +18,17 @@ import com.google.gson.Gson;
|
|||
import com.google.gson.example.model.Cart;
|
||||
import com.google.gson.example.model.LineItem;
|
||||
import com.google.gson.example.model.Order;
|
||||
import com.google.gson.example.model.QueryOrdersByItemName;
|
||||
import com.google.gson.rest.client.ResourceDepotClient;
|
||||
import com.google.gson.rest.client.RestClientStub;
|
||||
import com.google.gson.rest.client.RestServerConfig;
|
||||
import com.google.gson.rest.definition.ValueBasedId;
|
||||
import com.google.gson.rest.query.client.ResourceQueryClient;
|
||||
import com.google.gson.webservice.client.ServerConfig;
|
||||
import com.google.gson.webservice.client.WebServiceClient;
|
||||
import com.google.gson.webservice.definition.CallPath;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* A sample client for the rest resource for {@link Order}
|
||||
|
@ -36,11 +38,17 @@ import java.util.logging.Level;
|
|||
public class OrderClient {
|
||||
public static final CallPath CALL_PATH = new CallPath("/rest/order");
|
||||
private final ResourceDepotClient<ValueBasedId<Order>, Order> restClient;
|
||||
private final ResourceQueryClient<
|
||||
ValueBasedId<Order>, Order, QueryOrdersByItemName> queryClient;
|
||||
public OrderClient() {
|
||||
RestServerConfig serverConfig = new RestServerConfig("http://localhost");
|
||||
RestClientStub stub = new RestClientStub(serverConfig, Level.INFO);
|
||||
ServerConfig serverConfig = new ServerConfig("http://localhost");
|
||||
Gson gson = new Gson();
|
||||
|
||||
restClient = new ResourceDepotClient<ValueBasedId<Order>, Order>(
|
||||
stub, CALL_PATH, Order.class, new Gson());
|
||||
new RestClientStub(serverConfig), CALL_PATH, Order.class, new Gson());
|
||||
ServerConfig wsServerConfig = new ServerConfig("http://localhost");
|
||||
queryClient = new ResourceQueryClient<ValueBasedId<Order>, Order, QueryOrdersByItemName>(
|
||||
new WebServiceClient(wsServerConfig), CALL_PATH, gson);
|
||||
}
|
||||
|
||||
public Order placeOrder(Cart cart) {
|
||||
|
@ -48,11 +56,19 @@ public class OrderClient {
|
|||
return restClient.post(order);
|
||||
}
|
||||
|
||||
private List<Order> query(String itemName) {
|
||||
return queryClient.query(new QueryOrdersByItemName(itemName));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
OrderClient client = new OrderClient();
|
||||
List<LineItem> lineItems = new ArrayList<LineItem>();
|
||||
lineItems.add(new LineItem("item1", 2, 1000000L, "USD"));
|
||||
String itemName = "item1";
|
||||
lineItems.add(new LineItem(itemName, 2, 1000000L, "USD"));
|
||||
Cart cart = new Cart(lineItems, "first last", "4111-1111-1111-1111");
|
||||
client.placeOrder(cart);
|
||||
Order order = client.placeOrder(cart);
|
||||
System.out.println("Placed order: " + order);
|
||||
List<Order> queriedOrder = client.query(itemName);
|
||||
System.out.println("Queried order by item name ( " + itemName + "): " + order);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2008-2010 Google Inc.
|
||||
* Copyright (C) 2010 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -13,26 +13,25 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.google.gson.rest.client;
|
||||
package com.google.gson.example.model;
|
||||
|
||||
/**
|
||||
* Configuration needed to access a Gson REST service.
|
||||
* A query for orders by item name
|
||||
*
|
||||
* @author inder
|
||||
* @author Inderjeet Singh
|
||||
*/
|
||||
public final class RestServerConfig {
|
||||
private final String serviceBaseUrl;
|
||||
public class QueryOrdersByItemName {
|
||||
private final String itemName;
|
||||
|
||||
public RestServerConfig(String serviceBaseUrl) {
|
||||
this.serviceBaseUrl = serviceBaseUrl;
|
||||
public QueryOrdersByItemName() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public String getServiceBaseUrl() {
|
||||
return serviceBaseUrl;
|
||||
public QueryOrdersByItemName(String itemName) {
|
||||
this.itemName = itemName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return serviceBaseUrl;
|
||||
public String getItemName() {
|
||||
return itemName;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user