gpt4 book ai didi

java - 如何为 Broadleaf Commerce REST API 设置 JSON 格式 - 包装器成员为空?

转载 作者:行者123 更新时间:2023-12-01 08:58:35 27 4
gpt4 key购买 nike

我在目录端点上创建了一个新路径。它应该接受产品的 JSON 表示并将其添加到数据库中。

public ProductWrapper insertProduct(HttpServletRequest request, ProductWrapper wrapper)
{
return wrapper;
}


@RequestMapping(value="product",method=RequestMethod.POST)
public ProductWrapper addProduct(HttpServletRequest request, ProductWrapper wrapper){
return insertProduct(request, wrapper);
}

但是当我将 JSON 放入消息正文中时。它没有将其保存到我的包装中。我的 JSON 如下所示:

{
"id": 1,
"name": "Sudden Death Sauce",
"longDescription": "As my Chilipals know, I am never one to be satisfied. Hence, the creation of Sudden Death. When you need to go beyond... Sudden Death will deliver! ",
"retailPrice": {
"amount": "10.99",
"currency": "USD"
},
"primaryMedia": {
"id": 101,
"title": "Sudden Death Sauce Bottle",
"url": "/cmsstatic/img/sauces/Sudden-Death-Sauce-Bottle.jpg",
"altText": "primary"
},
"active": true,
"activeStartDate": "2017-01-25T16:32:36.993-0500",
"manufacturer": "Blair's",
"defaultCategoryId": 2002,
"productAttribute": [
{
"id": 1,
"productId": 1,
"attributeName": "heatRange",
"attributeValue": "4"
}
],
"media": [
{
"id": 102,
"title": "Sudden Death Sauce Close-up",
"url": "/cmsstatic/img/sauces/Sudden-Death-Sauce-Close.jpg",
"altText": "alt1"
},
{
"id": 101,
"title": "Sudden Death Sauce Bottle",
"url": "/cmsstatic/img/sauces/Sudden-Death-Sauce-Bottle.jpg",
"altText": "primary"
}
]
}

我错过了什么吗?我设置了一个断点,效果很好。包装器已实例化,但所有成员均为空。

最佳答案

您需要使用 @RequestBody 注释 wrapper 参数,并在 @RequestMapping 上添加 accepts :

@RequestMapping(value="product",method=RequestMethod.POST, accepts="application/json")
public ProductWrapper addProduct(HttpServletRequest request, @RequestBody ProductWrapper wrapper){
return insertProduct(request, wrapper);
}

当您发送请求时,请确保同时发送 Content-Type header ,例如 Content-Type=application/json ,以便 Spring 适本地序列化它.

关于java - 如何为 Broadleaf Commerce REST API 设置 JSON 格式 - 包装器成员为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41877624/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com