通过url javaplay设置参数

时间:2017-03-12 20:13:32

标签: java web-services url get playframework-2.0

因此,作为我正在做的一些工作的一部分,我获得了一个文件,其中包含正在Swift应用程序中使用的WebServices。我对WebServices一无所知,只通过语法理解了解Java。我需要使用swift应用程序中的参数调用其中一个获取。我首先要弄清楚的是我如何使用与其相关联的URL中的参数来调用其中一个web服务。例如,我想调用方法

http://localhost:9000/ListVehicleByPlateNumber

我希望通过URL指定参数,例如

http://localhost:9000/ListVehicleByPlateNumber?para="123"

但是这并没有为参数赋值,而且我没有得到结果。如果我硬编码,那么函数中使用的字符串是=" 123"它给了我我正在寻找的结果。我只需要知道如何通过url传递这个参数,语法方面。

路由文件

GET     /ListVehicleByPlateNumber controllers.NewVehicle.listVehicleByPlateNumber(para: String ?="")

控制器

public Result listVehicleByPlateNumber(String para){
    NewVehicleModel v = new NewVehicleModel();
    List<NewVehicleModel> vehiclesC = v.searchByPlateVehicle(para);
    ObjectNode wrapper = Json.newObject();
    ObjectNode msg = Json.newObject();

    if(vehiclesC != null) {
        msg.set("VehicleList", toJson(vehiclesC));
        wrapper.set("success", msg);
        return ok(wrapper);
    }else{
        msg.put("error", "There are no vehicles with the plate number");
        wrapper.set("error", msg);
        return badRequest(wrapper);
    }
}

它被称为

public List<NewVehicleModel> searchByPlateVehicle(String plateNumber){

    Transaction t = Ebean.beginTransaction();
    List<NewVehicleModel> vehicles = new ArrayList<>();
    try {
        String sql = "SELECT V.idNewVehicle, V.VehicleType,V.PlateNumber,V.VehicleJurisdiction,V.State,V.Vin,V.Year, " +
                "V.Make,V.modelos,V.RegistrationNumber,V.InsuranceCompany,V.PurchaseDate,V.ExpirationDate,V.idPersonaFK " +
                "FROM NewVehicle V " +
                "WHERE V.PlateNumber = :plateNumber";

        RawSql rawSql = RawSqlBuilder.parse(sql)
                .columnMapping("V.idNewVehicle", "idNewVehicle")
                .columnMapping("V.State", "state")
                .columnMapping("V.VehicleType", "vehicleType")
                .columnMapping("V.PlateNumber", "plateNumber")
                .columnMapping("V.VehicleJurisdiction", "vehicleJurisdiction")
                .columnMapping("V.Vin", "vin")
                .columnMapping("V.Year", "year")
                .columnMapping("V.Make", "make")
                .columnMapping("V.modelos", "modelos")
                .columnMapping("V.RegistrationNumber", "registrationNumber")
                .columnMapping("V.InsuranceCompany", "insuranceCompany")
                .columnMapping("V.PurchaseDate", "purchaseDate")
                .columnMapping("V.ExpirationDate", "expirationDate")
                .columnMapping("V.idPersonaFK", "idPersonaFK")
                .create();

        Query<NewVehicleModel> query = Ebean.find(NewVehicleModel.class);
        query.setRawSql(rawSql)
                .setParameter("plateNumber", plateNumber);
        vehicles = query.findList();
        t.commit();


    }
catch (Exception e){
    System.out.println(e.getMessage());

}finally {
        t.end();
    }

    return vehicles;

}

1 个答案:

答案 0 :(得分:0)

找到我自己的答案。我最终从Integer转换为String,以及它在路径中的外观

GET     /ListVehicleByPlateNumber/:para       controllers.NewVehicle.listVehicleByPlateNumber(para: Integer )

控制器

public Result listVehicleByPlateNumber(int para){
    String p = String.valueOf(para);

值格式为123的URI格式。

http://localhost:9000/ListVehicleByPlateNumber/123