简体   繁体   English

使用Json不能保存多个字符串

[英]Can't save more than one string using Json

I have a problem. 我有个问题。 I've been doing a simple REST app project and now I need to send JSON from http client for creating an entity. 我一直在做一个简单的REST应用程序项目,现在我需要从http客户端发送JSON以创建实体。 When I sent one property it's ok, but when I write another one it stores the second property to the same string. 当我发送一个属性时可以,但是当我写另一个属性时,它将第二个属性存储到同一字符串中。 It looks like this(the actual entity is in the list "toDos"): 看起来像这样(实际实体在列表“ 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"
        }
    ]
}

My POST request: 我的POST请求:

POST http://localhost:8080/todos/create-todo/1
Content-Type: application/json

{
  "text" : "my third todo",
  "scaryness" : 1,
  "hardness" : 1
}

My project on github: https://github.com/FedosovMax/to-do-app 我在github上的项目: https : //github.com/FedosovMax/to-do-app

Help me please to find out how do send scaryness and hardness to the right places. 请帮助我找出如何将恐惧和硬度传送到正确的地方。 If additional information needed, write me please. 如果需要其他信息,请给我写信。

It's my mistake in the RestController, my code was next: 这是我在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);
}

So it saved as one String. 因此将其另存为一个字符串。 Now my RestController looks like: 现在我的RestController看起来像:

@PostMapping(value = "/create-todo/{blockId}")
@ResponseStatus(HttpStatus.CREATED)
public void createToDo(@PathVariable long blockId, @RequestBody ToDo toDo){
    toDoService.saveToDo(toDo, blockId);
}

and it works right. 它运作正常。

 {
    "id": 1,
    "name": "block2",
    "archive": null,
    "toDos": [
        {
            "id": 2,
            "text": "my third todo",
            "scaryness": 1,
            "hardness": 2,
            "ready": false,
            "createdOn": null
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM