如何为 Broadleaf Commerce REST API 格式化 JSON - 包装器是成员是否为空?
How do I format JSON for Broadleaf Commerce REST API - Wrapper is members are null?
我在目录端点上创建了一个新路径。假定接受产品的 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 将其适当地序列化。
我在目录端点上创建了一个新路径。假定接受产品的 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 将其适当地序列化。