0

I have spring boot application. I am trying to save a simple Hospital details JSON request but when I check the on the server side the JSON is not mapped to VO object. It always comes as null.

Code: pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.company.tracker</groupId>
    <artifactId>hospitals</artifactId>
    <version>1.0</version>
    <name>HealthCareApp</name>
    <description>Spring boot for hopitals</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- OAuth2 Client -->
        <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> 
            </dependency> -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- JWT library -->
        <!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.22.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.4.22.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>1.3.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.3.0.Final</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass> com.company.tracker.hostipals.HealthCareAppApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

HospitalDetailsVO.java

    package com.company.tracker.hospitals.vo;

import java.io.Serializable;

import lombok.Data;

@Data
public class HospitalDetailsVO  extends AuditVO implements Serializable{

    
    /**
     * 
     */
    private static final long serialVersionUID = -759883774566026493L;


    private Long hospitalId;
    
    
    private String registrationId;
    

    private String hospitalName;
    
    
    private String address1;
    
    
    private String address2;
    
    
    private String address3;
    
    
    private String streetName;
    
    
    private String landmark;
    
    
    private String city;
    
    
    private String district;
    
    
    //private ZipCodeMasterData zipcode;
    
}

AuditVO.java

    package com.company.tracker.hospitals.vo;

import java.io.Serializable;
import java.time.LocalDate;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Data
public class AuditVO implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1781884768898596479L;

    private String createdBy;

    private String creationDate;

    private String lastModifiedBy;

    private String lastModifiedDate;

    private String isDeleted;
}

Controller:

    package com.company.tracker.hostipals.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.company.tracker.hospitals.entity.HospitalDetails;
import com.company.tracker.hospitals.vo.HospitalDetailsVO;
import com.company.tracker.hostipals.service.HospitalInformationService;

@RestController
@RequestMapping("/hospital")
public class HospitalDetailsController {

    @Autowired
    HospitalInformationService hospitalInformation;

    @PostMapping(value = "/registration", consumes = "application/json", produces = "application/json")
    public String hospitalRegistration(@RequestBody HospitalDetailsVO hospitalRegistration) {
        return hospitalInformation.createHospital(hospitalRegistration);

    }

    
}

Request JSON:

"hospitalId":null,
"registrationId":1233,
"hospitalName":"Bharathi mithulan hospital",
"address1":"O.Nanjagoundanpalayam",
"address2":"Odathurai",
"address3":"Erode",
"streetName":"Bhavani TK",
"landmark":"Srinivasa Rice mill opposite",
"city":"Erode",
"district":"Tamilnadu",
"createdBy":"bharathi",
"creationDate":"5-11-2020",
"lastModifiedBy":"bahrathi",
"lastModifiedDate":"5-11-2020",
"isDeleted":"N"
}

When pass the above JSON the value received on controller is coming as null for all the object valuesa highlighted below.

enter image description here

Can I know where I am making msitake in mapping the request JSON to VO object.

2
  • What is inside the class AuditVO ? . Perhaps some of the base class attributes expected is not proper ? class HospitalDetailsVO extends AuditVO
    – jfk
    Nov 6, 2020 at 8:32
  • @jafarmlp added the AuditVO class as well
    – bharathi
    Nov 6, 2020 at 8:33

2 Answers 2

0

I would recommend you to examine the raw data first to see what is reached at the backend.

@PostMapping(value = "/registration", consumes = "application/json", produces = "application/json")
    public String hospitalRegistration(@RequestBody String data) {
       //print the data
    }
0

I have fixed the issue. The lombok getter and setter not get generated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.