1

这是我的 API 的响应:

"The number of parameters to execute should be consistent with the expected number of parameters = [2] but the actual number is [0]."

我使用 pgClient、Vert.x 3.9.3 并毫无问题地使用各种 REST API,但是....在这个查询中(可能是错误的)

private static final String SELECT_CBA = "select art.leyenda, $1::numeric(10,3) as cantidad, uni.abreviatura, \r"
+ "round(((art.precio_costo * (art.utilidad_fraccionado/100)) + art.precio_costo) * $2::numeric(10,3),2) as totpagar \r"
+ "FROM public.articulos art join public.unidades uni on uni.idunidad = art.idunidad \r"
+ "WHERE (substring(art.codigobarra,1,2) = \'$3\' and substring(art.codigobarra,3,6) = \'$4\')";

一些解释
$1 和 $2 是相同的参数;$2 和 $3 必须是带引号的参数。这是我的休息垂直:

poolClient = PgPool.pool(vertx, options, poolOptions);      
bizArticulo = new BizArticulo(poolClient);
Router router = Router.router(vertx);
router.route("/api/articulos*").handler(BodyHandler.create());
router.get("/api/articulos").handler(bizArticulo::getAll);
router.get("/api/articulos/:cantcomp1/:cantcomp2/:tipoprod/:prodpadre").handler(bizArticulo::getOneReadingBarcode);

其中 :cantcomp1 -> $1, :cantcomp2 -> $2, :tipoprod -> $3 和 $4 -> :prodpadre

最后,这是我的“生意”

public void getOneReadingBarcode(RoutingContext routingContext) {       
        HttpServerResponse response = routingContext.response();
        pgClient
        .preparedQuery(SELECT_CBA)      
        .execute(ar -> {
            if (ar.succeeded()) {
                RowSet<Row> rows = ar.result();
                List<Articulo> articulos = new ArrayList<>();
                rows.forEach(row -> {
                    articulos.add(fromBarCode(row));
                });
                response.putHeader("content-type", "application/json; charset=utf-8")
                .setStatusCode(200)
                .end(Json.encodePrettily(articulos));
            } else {
                System.out.println("Failure: " + ar.cause().getMessage());
                response.putHeader("content-type", "application/json; charset=utf-8")
                .end(Json.encodePrettily(ar.cause().getMessage()));
            }
        });
    }

在邮递员中,我写道:

192.168.0.15:8092/api/articulos/0.750/0.750/20/021162 ;_ _ _ _ _ _ _ 我假设参数匹配,但它返回上面提到的错误。怎么了?任何帮助将不胜感激埃内斯托

4

1 回答 1

1

好的,我回答自己... 首先:对我的查询(SELECT_CB)进行小修复,我将参数 $1, $2... 放在括号中..

private static final String SELECT_CBA = "select art.leyenda, ($1::numeric(10,3)) as cantidad, uni.abreviatura, \r"
+ "round(((art.precio_costo * (art.utilidad_fraccionado/100)) + art.precio_costo) * ($2::numeric(10,3)),2) as totpagar \r"
+ "FROM public.articulos art join public.unidades uni on uni.idunidad = art.idunidad \r"
+ "WHERE (substring(art.codigobarra,1,2) = ($3) and substring(art.codigobarra,3,6) = ($4))";

第二:从上下文中获取参数(参见上一个问题中的router.context )

Double cantComprada1 = Double.parseDouble(routingContext.request().getParam("cantcomp1"));
Double cantComprada2 = Double.parseDouble(routingContext.request().getParam("cantcomp2"));
String tipoProducto = routingContext.request().getParam("tipoprod");
String productoPadre = routingContext.request().getParam("prodpadre");

第三将参数作为参数

pgClient
.preparedQuery(SELECT_CBA)
.execute(Tuple.of(cantComprada1, cantComprada2, tipoProducto, productoPadre), ar -> {
......
......
}

所有工作都很好

于 2020-09-30T08:49:31.513 回答