1

So basically i checked other questions and the most answers are to use @RequestBody in my Post method on backend but it still throws 500 error with all my received json data is null

Any other suggestions what can be the problem?

here's my Rest:

@PostMapping({ "/createScreen" })
    public ResponseEntity<String> createScreen(@RequestBody AdminWrapper adminWrapper) {
        System.out.println(adminWrapper);
        adminDao.saveAll(adminWrapper.getAdminList());

        return new ResponseEntity<String>(HttpStatus.OK);
    }

and this is the json i want to send back:

{
    "module": "testModule",
        "networkId": 1,
            "adminInfos":
    [{
        "id": 1,
        "key": "Test",
        "value": "TestValue",
        "defValue": "TestDef",
        "type": "checkbox",
        "isActive": true
    }]
}

but in the log i see this:

adminWrapper [module=null, networkId = null, adminInfos = null ]

Request from frontend:

function submit() {

let adminWrapper = {
        module: 'testModule',
        networkId: 1,
        adminInfos: []
    }

$('#content  > .form-group').each(function () {

        let value = $(this).find('input').val();
        if ($(this).find('input').attr('type') === 'checkbox')

            value = $(this).find('input').prop('checked')

        let adminInfo = {

            id: $(this).find('input').attr('data-id'),
            key: $(this).find('input').attr('data-key'),
            value: value
        }
        adminWrapper.adminInfos.push(adminInfo);
    })

$.ajax({
        type: "POST",
        url: 'http://localhost:8080/.../createScreen',
        contentType: "application/json",
        data: JSON.stringify({
            adminWrapper
        })
    })
}

Kerk
  • 233
  • 2
  • 16
  • Have you set the content type header to application/json in your POST request? – Jordan Mackie Dec 12 '19 at 15:15
  • https://stackoverflow.com/a/49010773/4364101 you can try this to see exactly what String body is arriving at the controller too if it helps with debugging – Jordan Mackie Dec 12 '19 at 15:16
  • Also, do you have a 1. no args constructor for AdminWrapper 2. getters+setters for all fields? You'll need to have the same for any other class fields like your NetworkId – Jordan Mackie Dec 12 '19 at 15:19
  • 1
    yes i got my content type right, got constructor and getters setters aswell, checked if i typed something wrong but everything looks good – Kerk Dec 12 '19 at 15:22
  • Good stuff Kerk, just for a sanity check can you add how you're making the request too? :) If you're sending from Chrome, you can use the dev tools, view the network tab, and see what you're sending there too – Jordan Mackie Dec 12 '19 at 15:25
  • 1
    i edited my question and added the request – Kerk Dec 12 '19 at 15:35
  • 1
    `JSON.stringify({adminWrapper})` is wrapping your object in an extra layer I think, try remove those curly brackets. You can confirm this by checking the network tab in chrome too. Another tool you can use for a sanity check is Postman where you can fill in the raw JSON you would be expecting to send – Jordan Mackie Dec 12 '19 at 15:44
  • 1
    removed the brackets still same and in postman i get status 200 so my request works but the values are somehow null.. – Kerk Dec 12 '19 at 15:52
  • just to make sure I understand: when you make the request through postman, do you receive the values you expect filled in in Spring? And in a separate test, can you see what the request body is when the request is being sent from your browser? – Jordan Mackie Dec 12 '19 at 15:58
  • 1
    in browser in the requestbody i see what i want so everything is sent fine from frontend but in backend all the sent values are null like if i would send empty json to backend, and if i send the with postman it works i see the values in my database – Kerk Dec 12 '19 at 16:06
  • what exactly is being sent by the frontend though, just to be sure. If it works with postman that would really suggest its the client doing something wrong, so I'd focus there. – Jordan Mackie Dec 12 '19 at 16:13
  • 1
    yes good point i checked again what is sent from frontend and i realize that one of the values were missing from the json that was sent and now it works finally, thanks for all the tips appreciate it! – Kerk Dec 13 '19 at 09:15

2 Answers2

1

So the problem is in my

        let adminInfo = {
            id: $(this).find('input').attr('data-id'),
            key: $(this).find('input').attr('data-key'),
            value: value
        }

i forgot to add these:

        "defValue": "TestDef",
        "type": "checkbox",
        "isActive": true
Kerk
  • 233
  • 2
  • 16
0

I think your AdminWrapper.java is not matching the structure with request josn which is coming from front end, try to match each and every field from request json.

  • you can refer below example

https://www.baeldung.com/spring-request-response-body

Saurabh
  • 11
  • 4