简体   繁体   中英

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. 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"):

{
    "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 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

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:

@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:

@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
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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