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:
- I tested using boolean and string/number data types in another entity/controller/repo (Visitor class) within the same project and it works.
- 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.
- 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)
);
;. This will be much clear in reading as the fields and the annotations which are related can be read as a group.@Jacksonizedin 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.