Having trouble mapping my JSON to a Spring Boot @RequestBody POJO

I'm struggling with an issue where I have a hard-coded JSON object that is supposed to map to my POJO in Spring Controller, but I keep getting null values. I have double-checked my getters and setters, and everything seems to be correct. Can anyone help me figure out what I am doing wrong?

Controller

@PostMapping("/dashboard")
public Dashboard getDashboard(@RequestBody PaginationRequest paginationRequest) {
      return topcatService.getDashboard(paginationRequest);
}

JSON

 var paginationRequest = { grouping  : e.target.value ,total : "1", currentPage : "1", pageSize : "5"};

POJO

 public class PaginationRequest {
        private String grouping;
        private String total;
        private String  currentPage;
        private String pageSize;

       //setter/getter
    }

Answer №1

To ensure proper data serialization, consider making your properties public and using the @JsonProperty annotation for each property (e.g. @JsonProperty("grouping")). It's possible that your getters and setters do not adhere to standard naming conventions.

Answer №2

To ensure your data is properly received by your controller, it is essential to first create a valid JSON structure and test it.

You can test this by sending an example JSON like the following:

var paginationRequest = '{"grouping":"anyValue","total":"1","currentPage":"1","pageSize": "5"}';

This demonstrates sending only a String in JSON format.

If you have an Object, you may need to convert it to a JSON String:

var somejson =  JSON.stringify(someobject);

Answer №3

It appears that I have identified the issue.

Ironically, it turns out to be a simple mistake on my part.

Upon closer inspection, I discovered that the @RequiredBody annotation was mistakenly imported from the swagger dependency instead of spring. The signatures are identical, making it difficult to pinpoint the error initially.

Answer №4

Here are a few tweaks I can recommend,

@RequestMapping(value = "/dashboard", method = RequestMethod.POST, 
                consumes = "application/json", produces = "application/json")
public Dashboard displayDashboardInfo(@RequestBody PaginationRequest paginationRequest) {

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What changes can I make to the script in order to display every element from a MySQL query using PHP, MySQL, JavaScript,

Enhanced Multiple Dropdown Selection Using Ajax In the following scenario, we have a webpage featuring a multiple dropdown selection that interacts with a MySQL database. By choosing options in the dropdowns labeled site, menu, and category (categ), a que ...

Receive text messages before they reach the primary messaging app and modify their content

Can you intercept and modify an SMS message before it reaches the primary messaging app? ...

Check out our website's event countdown timer featuring a server-sided event tracking system

Currently, I am in the process of developing a website and have the requirement to incorporate a timer on one of the pages. The goal is for the timer to count down from a specified time, such as DD::hh:mm 02:12:34, until it reaches zero. Once the countdown ...

difficulty arises when attempting to invoke a viewmodel from another viewmodel inside a ko.computed function

Is it possible to have two view model functions in my JavaScript where one references the other? I am encountering an error with this setup. Here are my view models: var userViewModel = function (data) { var _self = this; _self.ID = ko.obs ...

Front-end procedural logic for increasing identification values

$scope.items.push({ "itemId": $scope.tabId + 1, "itemName" : itemName, }); Whenever I try to push the item, I always console.log($scope.itemId) but it remains the same without increasing. One way to handle this issue could be utilizing $http after each ...

JQuery ID Media Queries: Enhancing responsive design with targeted

Is it possible to integrate a media query into the selection of an ID using JQuery? For example: $('#idname') $('@media (max-width: 767px) { #idname }') In essence, can you target the #idname with that specified media query in JQuery ...

Error Occurred: Android Realm get Not Executed within a Transaction

Issue Resolved The problem was fixed by replacing realm.commitTransaction(); with realm.copyToRealm(item); Challenge When trying to retrieve data from a dialog through a listener using an interface and attempting to save the selected data as a class mod ...

Utilizing JavaScript in AJAX Responses

Can I include JavaScript in an AJAX response and run it, or should I only use JSON or plain HTML for a more elegant solution? I'm trying to figure out the best way to handle AJAX requests that involve inserting HTML or running JavaScript based on user ...

Navigating collisions in the ECS architecture: Best practices

I'm currently developing a game using typescript and the ECS design pattern. One of the challenges I'm facing is handling collisions between different entities within the game world. I have an entity called Player which comprises several componen ...

Webpack fails to handle CSS background images

I'm having trouble with my Webpack configuration as it's not processing CSS images set in the background property: background: url('./hero.jpg') no-repeat right; This is resulting in an error message that reads: ERROR in ./src/app/comp ...

Having difficulty converting a local variable into a global variable in JavaScript and Express

I am facing challenges trying to convert a local variable into a global variable while working with Express and Javascript. Below is my JavaScript code snippet: // Setting up Express and EJS const express = require("express"); const JSON = requi ...

Generate a JSON line for each value in the ARRAY

Hello everyone, I'm currently working on implementing handlebars templating and to do so I need to generate a JSON from array values {"path":"Avions", "fileName":"AvionsEdit.vue"},{"path":"Avions", "fileName":"AvionsShow.vue"}etc... While I can cre ...

What is the best way to extract information from a JSON array stored in a

I have JSON data stored in a file. { "from_excel":[ { "solution":"Fisrt", "num":"1" }, { "solution":"Second", "num":"2" }, { "solution":"third", "num":"3" }, { "solution": ...

Enhance the jQueryUI progress bar by dynamically updating it with inner HTML data

I am working on implementing a jQueryUI progress bar and facing some challenges. Here is the basic code for the progress bar: <script> $(function() { $("#progressbar").progressbar({ value: 50, }); }); </script& ...

Pressing a button to alter the text color

While experimenting, I attempted to alter the text color of my HTML using JavaScript. After a few attempts, I came to the realization that I cannot change the color if I am already changing it in CSS. Does this mean CSS is executed at the end? Also, how ca ...

What is the best way to run a function after 10 seconds of inactivity using jquery?

Can anyone help me figure out how to run a function every 10 seconds when the system is idle? Here's an example: setInterval(function () { test(); },10000); //for every 10 Sec I really need assistance in getting this function to ...

Facing an issue with Selenium WebDriver in Java where I am unable to accept an alert prompt

While using selenium IDE to record, I attempted to click the "OK" button in a pop-up and expected to be able to do so by using driver.findElement(By.linkText("OK")).click(); However, this method did not work as anticipated. Similarly, the following code ...

The process of registering with JWT tokens and the challenge that arises when a token expires

I had the idea to implement a registration process that requires users to provide their username, email (which must not already exist in the database), password, and confirm password. The project is built using NextJS with pages router and Typescript. impo ...

Angular Firebase Count of Items in List

My current dilemma revolves around obtaining a count of items within a firebase list. Firebase details: foods randompushid title: apple, comboQuery: apple23523526(userid) Angular Code snippet: this.foods= this.firebaseDB.list(& ...

Tips for running a JavaScript function from a controller in a Rails application

I am looking for a way to upload an image and display it without refreshing the page. One method I am familiar with involves using a hidden iframe and setting the form target to it. Then, I would return a piece of JavaScript from the controller that call ...