从Java中的POST请求中提取数据

时间:2017-06-28 11:03:14

标签: java curl post

当我对API端点执行CURL请求时,我发现在解析请求时,我正在发送的JSON被置于请求中>代表>身体,而不是我在request.body()

中的期望

这是意料之外的,因此最终会得到以下代码,在数据库中创建一个空值的记录。

我是否需要修改curl请求?或者以其他方式提取数据?

CURL请求:

curl -H "Content-Type: application/json" -X POST -d '{"name":"christian"}' http://127.0.0.1:4567/suppliers

POST代码

    post("/suppliers",
            (request, response) -> {

                response.type("application/json");
                Supplier supplier = new Gson().fromJson(request.body(),
                        Supplier.class);

                SupplierCrud sc = new SupplierCrud();
                sc.createSupplier(supplier);

                return new Gson().toJson(new StandardSupplierResponse(
                        StatusResponse.SUCCESS,new Gson().toJsonTree(supplier)));
            });

The location the data is stored

1 个答案:

答案 0 :(得分:0)

哑。我发现我使用大写变量创建了Supplier.java对象。

@Expose private String Type; @Expose private int Id; @Expose private String Name;

我将此修改为全部小写。一切随后都有效......

@Expose private String type; @Expose private int id; @Expose private String name;