0

在我的 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");



}

}

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

4

1 回答 1

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,你应该注意他们很快就会提供这种类型的开箱即用接口。

这是一个简单的示例,但您仍应阅读用户指南

首先,创建一个服务(一个扩展 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) {
            ...
        }
    });
于 2013-05-27T17:58:47.643 回答