将参数传递给Rest Client

时间:2013-05-27 15:45:42

标签: rest gwt jersey

在我的gwt应用程序中,我实现了这个方法:

@Service("carService")
@Path("/cars")
@Scope("request")
public class CarServiceImpl implements CarService {

     @Autowired
     private CarDAO carDAO;

     @Override
     @GET @Path("{type}/{start}/{end}")
     @Produces({MediaType.APPLICATION_XML})
     public List<Car> findByType(@PathParam("type") CarType type, 
                     @PathParam("start") Date start, 
                     @PathParam("end") Date end) {
     return carDAO.findByType(type, start, end);
     }

其中findByType是carDAO中的一种方法,要求数据库(在点对点架构中)可供租用的汽车。 现在我必须实现其休息客户端,我有:

打包it.unibo.ronf.server.rest;

import java.util.Date;
import java.util.List;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;

import it.unibo.ronf.shared.entities.Car;
import it.unibo.ronf.shared.entities.CarType;

public class ClientRestCars {

public List<Car> findAvailableCar(CarType type, Date start, Date end) {

    Client client = Client.create();

    WebResource webResource = client
            .resource("http://localhost:8080/RONF/rest/cars");



}

}

我不知道如何继续,如何将参数传递给网址并获取汽车列表作为结果。 可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

如果您没有框架来发送请求,请查看restygwt。它允许以这种方式创建此类请求:

@PUT
@Path("{type}/{start}/{end}")
public void updateOrder(@PathParam("type") CarType carType,
                        @PathParam("start") Date start,
                        @PathParam("end") Date end,
                        MethodCallback<OrderConfirmation> callback);

注意:如果您使用的是gwtp,您应该注意他们很快会提供这种开箱即用的界面。

以下是一个简单示例,但您仍应阅读user guide

首先,创建一个服务(扩展RestService的接口):

@Path("/test/method")
public interface CarService extends RestService {

  /*
   * With @PathParam, you can specify the name of the attribute in the request.
   * Without PathParam, the name of the attribute takes the name of the argument.
   * The parameter type T of MethodCallBack<T> is the type you expect in the success method of the callback (see above)
   */
  @GET
  public void get(CarType carType,@PathParam("start")Date myStartDateWithASillyName, Date end,MethodCallback<List<Car>> callback);

  /*
   * Another method just to show that you are not limited to one...
   * Here, the request have car parameter and is intended to save a car.
   * As REST norms expect in this case, the resulting car is returned from server (with potentially the id updated after insert in database)
   */
  @POST
  public void post(Car car,MethodCallback<Car> callback);
}

现在,您可以在代码中的任何位置调用您的服务:

CarService service = GWT.create(CarService.class);
    service.get(carType,dateFrom,dateTo,new MethodCallback<List<Car>>() {

        /*
         *This method is called if a problems occurs (server not reachable, 404,403 etc)
         */
        public void onFailure(Method method, Throwable exception) {
            Window.alert("Error x: " + exception);
        }

        /*
         * In case of success, this method is called and you obtain 
         * the expected List<Car> as the response parameter
         */
        public void onSuccess(Method method, List<Car> response) {
            ...
        }
    });