1

Not sure if i'm going crazy or something, but I have a very simple POST mapping set up to receive a JSON response with data types of String and boolean. For some reason the method is changing the name of my fields and it's also failing to parse the value of the 2 boolean variables and some String variables.

Notes:

  1. I tested using boolean and string/number data types in another entity/controller/repo (Visitor class) within the same project and it works.
  2. The data that gets committed to the DB is already erroneous (hence the return response is erroneous) so I believe the initial parsing of the data from the ResponseEntity is the issue.
  3. I tried using both BIT and BOOLEAN in my table schema but both results are the same.

Thanks in advance for any feedback!

Entity

    package com.vmsac.vmsacserver.model;

    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;

    import javax.persistence.*;

    @NoArgsConstructor
    @AllArgsConstructor
    @Entity
    @Data
    @Table(name="scheduledvisit")
    public class ScheduledVisit {

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    @Column(name="scheduledvisitid")
    private Long scheduledVisitId;
    @Column(name="visitorid")
    private Long visitorId;
    @Column(name="startdateofvisit")
    private String startDateofVisit;
    @Column(name="enddateofvisit")
    private String endDateofVisit;
    @Column(name="qrcodeid")
    private Long qrCodeId;
    @Column(name="isvalid")
    private boolean isValid;
    @Column(name="isonetimeuse")
    private boolean isOneTimeUse;
    @Column(name="raisedby")
    private Long raisedBy;
    }

Controller

package com.vmsac.vmsacserver.controller;
import com.vmsac.vmsacserver.model.ScheduledVisit;
import com.vmsac.vmsacserver.repository.ScheduledVisitRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.net.URI;
import java.net.URISyntaxException;

@RestController
@RequestMapping("/api")
public class ScheduledVisitController{

    @Autowired
    private ScheduledVisitRepository scheduledVisitRepository;

    @GetMapping(path = "/scheduled-visits")
    List<ScheduledVisit> getScheduledVisits(){
        return scheduledVisitRepository.findAll();
    }

    @PostMapping(path = "/register-scheduled-visit", consumes = "application/json")
    ResponseEntity<ScheduledVisit> createScheduledVisit(@Valid @RequestBody ScheduledVisit scheduledVisit) throws URISyntaxException{
        ScheduledVisit registeredVisit = scheduledVisitRepository.save(scheduledVisit);
        return ResponseEntity.created(new URI("/api/register-scheduled-visit" + registeredVisit.getScheduledVisitId())).body(registeredVisit);

    }

}

Repository

package com.vmsac.vmsacserver.repository;

import com.vmsac.vmsacserver.model.ScheduledVisit;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ScheduledVisitRepository extends JpaRepository<ScheduledVisit, Long> {

    List<ScheduledVisit> findByScheduledVisitId(String scheduledVisitId);
}

JSON Object (using Postman)

{
  "visitorId" : 456879,
  "startDateOfVisit" : "10Jan2021",
  "endDateOfVisit" : "12-Jan-2021",
  "qrCodeId" : 12325,
  "isValid" : true,
  "isOneTimeUse" : false,
  "raisedBy" : 123456
}

Response

{
    "scheduledVisitId": 3,
    "visitorId": 456879,
    "startDateofVisit": null, //this string returns null, should have a value
    "endDateofVisit": null, //this string returns null, should have a value
    "qrCodeId": 12325,
    "raisedBy": 123456,
    "valid": false, //field name should be isValid, value should be true
    "oneTimeUse": false //field name should be isOneTimeUse
}

DB Schema

CREATE TABLE ScheduledVisit (
  scheduledVisitId BIGINT NOT NULL AUTO_INCREMENT,
  visitorId BIGINT,
  startDateOfVisit VARCHAR(128),
  endDateOfVisit VARCHAR(128),
  qrCodeId BIGINT,
  isValid BIT,
  isOneTimeUse BIT,
  raisedBy BIGINT,
  PRIMARY KEY (scheduledVisitId)
);
2
  • Trivial recommendation, for your entities to be more readable, you can a blank line after each statement that means a blank like after each ;. This will be much clear in reading as the fields and the annotations which are related can be read as a group. Commented Jun 21, 2021 at 11:00
  • I can also recommend using a dto and not expose your entity directly. Then you can be flexible with which fields you want to show etc. If you use a dto another useful annotation is @Jacksonized in combination with @Builder. You will need thought something like MapStruct to map your dto to entity. But take that only as a recommendation that has nothing to do with your actual problem. Commented Jun 21, 2021 at 11:07

1 Answer 1

1

Problem is that you have in JSON: startDateOfVisit and endDateOfVisit with big letter O, and in your ScheduledVisit with small letter o. Therefore getters and setters were created with Lombok also with small letter o and Jackson doesn't find getters/setters for these properties, therefore they are nulls. So please check that for all your json-properties you have suitable getters and setters.

With booleans is a little more complicated and I think for Lombok the easiest way is not to use prefix, so in ScheduledVisit I would rename field isValid to valid and isOneTimeUse to oneTimeUse.

Otherwise you can rewrite Lombok getter/setter for your boolean field, e.g. like this:

    private boolean isValid;

    @JsonProperty("isValid")
    public boolean isValid() {
        return isValid;
    }
    
    public void setValid(boolean isValid) {
        this.isValid = isValid;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! This solved my issue. Also, appreciate all the suggestions for improvement above.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.