我有问题。我一直在做一个简单的REST应用程序项目,现在我需要从http客户端发送JSON以创建实体。当我发送一个属性时可以,但是当我写另一个属性时,它将第二个属性存储到同一字符串中。 看起来像这样(实际实体在列表“ toDos”中):
{
"id": 1,
"name": "block2",
"archive": null,
"toDos": [
{
"id": 2,
"text": "{\n \"text\" : \"my third todo\",\n \"scaryness\" : 1,\n \"hardness\" : 1\n}",
"scaryness": null,
"hardness": null,
"ready": false,
"createdOn": "2018-11-10T19:19:23.32207"
}
]
}
我的POST请求:
POST http://localhost:8080/todos/create-todo/1
Content-Type: application/json
{
"text" : "my third todo",
"scaryness" : 1,
"hardness" : 1
}
我在github上的项目:https://github.com/FedosovMax/to-do-app
请帮助我找出如何将恐惧和硬度传送到正确的地方。如果需要其他信息,请给我写信。
答案 0 :(得分:0)
这是我在RestController中的错误,下一个是我的代码:
@PostMapping(value = "/create-todo/{blockId}")
@ResponseStatus(HttpStatus.CREATED)
public void createToDo(@PathVariable long blockId, @RequestBody String text, Scaryness scaryness, Hardness hardness){
toDoService.saveToDo(text, scaryness, hardness, blockId);
}
因此将其另存为一个字符串。现在我的RestController看起来像:
@PostMapping(value = "/create-todo/{blockId}")
@ResponseStatus(HttpStatus.CREATED)
public void createToDo(@PathVariable long blockId, @RequestBody ToDo toDo){
toDoService.saveToDo(toDo, blockId);
}
它正常工作。
{
"id": 1,
"name": "block2",
"archive": null,
"toDos": [
{
"id": 2,
"text": "my third todo",
"scaryness": 1,
"hardness": 2,
"ready": false,
"createdOn": null
}