Reputation: 1
I've been looking around for a bit and can't seem to figure out why this happens.
I made a post request in postman, it runs it, intelliJ's console doesn't display an error, yet when printing out the object, it shows only the name has been inserted as I wrote it, the rest are 0's.
EDIT: numbers now come in, but foreign key "holdid" only returns as a null value
EDIT 2: Made it work, code changed to show working code
My Controller:
import com.example.tourdebackend.domain.model.Cykelrytter;
import com.example.tourdebackend.domain.service.CykelholdService;
import com.example.tourdebackend.domain.service.CykelrytterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/tourdefrance")
public class CykelrytterController {
private final CykelrytterService cykelrytterService;
private final CykelholdService cykelholdService;
@Autowired
public CykelrytterController(CykelrytterService cykelrytterService,
CykelholdService cykelholdService) {
this.cykelrytterService = cykelrytterService;
this.cykelholdService = cykelholdService;
}
@PostMapping()
public ResponseEntity<Cykelrytter> createCykelrytter(
@RequestBody Cykelrytter cykelrytter) {
cykelrytterService.create(cykelrytter);
System.out.println(cykelrytter);
return new ResponseEntity<>(HttpStatus.OK);
}
}
My relevant service:
import com.example.tourdebackend.domain.model.Cykelrytter;
import com.example.tourdebackend.repository.CykelrytterRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class CykelrytterService {
private final CykelrytterRepository cykelrytterRepository;
@Autowired
public CykelrytterService(CykelrytterRepository cykelrytterRepository){
this.cykelrytterRepository = cykelrytterRepository;
}
public void create (Cykelrytter cykelrytter) {
cykelrytterRepository.save(cykelrytter);
}
public List<Cykelrytter> read() {
return cykelrytterRepository.findAll();
}
public Optional<Cykelrytter> readById(int id) { return cykelrytterRepository.findById(id); }
public Cykelrytter update(Cykelrytter cykelrytter){
return cykelrytterRepository.save(cykelrytter);
}
public void delete(int id) { cykelrytterRepository.deleteById(id); }
}
Models:
package com.example.tourdebackend.domain.model;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Data;
import javax.persistence.*;
@Entity
@Data
@Builder
public class Cykelrytter {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
@Column(name = "bjergpoint")
@JsonProperty("bjergpoint")
private int bjergPoint;
@Column(name = "spurtpoint")
@JsonProperty("spurtpoint")
private int spurtPoint;
@Column(name = "laptime")
@JsonProperty("laptime")
private double lapTime;
@JsonBackReference
@ManyToOne
@JoinColumn(name = "holdid", referencedColumnName = "id")
private Cykelhold cykelhold;
public Cykelrytter() {
}
public Cykelrytter(int id, String name, int bjergPoint, int spurtPoint, double lapTime, Cykelhold cykelhold) {
this.id = id;
this.name = name;
this.bjergPoint = bjergPoint;
this.spurtPoint = spurtPoint;
this.lapTime = lapTime;
this.cykelhold = cykelhold;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBjergPoint() {
return bjergPoint;
}
public void setBjergPoint(int bjergPoint) {
this.bjergPoint = bjergPoint;
}
public int getSpurtPoint() {
return spurtPoint;
}
public void setSpurtPoint(int spurtPoint) {
this.spurtPoint = spurtPoint;
}
public double getLapTime() {
return lapTime;
}
public void setLapTime(double lapTime) {
this.lapTime = lapTime;
}
public Cykelhold getCykelhold() {
return cykelhold;
}
public void setCykelhold(Cykelhold cykelhold) {
this.cykelhold = cykelhold;
}
@Override
public String toString() {
return "Cykelrytter{" +
"id=" + id +
", name='" + name + '\'' +
", bjergPoint=" + bjergPoint +
", spurtPoint=" + spurtPoint +
", lapTime=" + lapTime +
", cykelhold=" + cykelhold +
'}';
}
}
package com.example.tourdebackend.domain.model;
import javax.persistence.*;
import java.util.List;
@Entity
public class Cykelhold {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "holdname")
private String holdName;
@OneToMany
@JoinColumn(name = "id")
//@JsonBackReference
private List<Cykelrytter> cykelrytters;
public Cykelhold() {
}
public Cykelhold(int holdid, String holdName) {
this.id = holdid;
this.holdName = holdName;
}
public Cykelhold(int holdid, String holdName, List<Cykelrytter> cykelrytters) {
this.id = holdid;
this.holdName = holdName;
this.cykelrytters = cykelrytters;
}
public int getId() {
return id;
}
public void setId(int holdid) {
this.id = holdid;
}
public String getHoldName() {
return holdName;
}
public void setHoldName(String holdName) {
this.holdName = holdName;
}
public List<Cykelrytter> getCykelrytters() {
return cykelrytters;
}
public void setCykelrytters(List<Cykelrytter> cykelrytters) {
this.cykelrytters = cykelrytters;
}
@Override
public String toString() {
return "Cykelhold{" +
"id=" + id +
", holdName='" + holdName + '\'' +
", cykelrytters=" + cykelrytters +
'}';
}
}
My postman request:
{
"bjergpoint": 5,
"laptime": 52.02,
"cykelhold":{
"id": 2
},
"name": "kurt",
"spurtpoint": 5
}
Any help is appreciated, thank you!
Upvotes: 0
Views: 771
Reputation: 44525
For JSON deserialization, Jackson is case sensitive to find the Java fields for each of the fields in the JSON body. In your case, the Java field names are not the same as the JSON field names. You either have to rename them in one of the sides, or add the JsonProperty
annotation to tell Jackson, what the field is called in the JSON body.
Example:
@Column(name = "bjergpoint")
@JsonProperty("bjergpoint")
private int bjergPoint;
Upvotes: 2