I have a microservice, in controller my required values come from service layer but it doesn't show in postman.
This is controller-
@RestController
@RequestMapping("/rental")
public class RentalController {
@Autowired
private RentalService rentalService;
@GetMapping("/{id}")
public ResponseEntity<Rental> getRental(@PathVariable("id") Long id){
Rental rl = rentalService.findRentalById(id);
return ResponseEntity.ok(rl);
}
@GetMapping("/viewAll")
public List<Rental> getAllRental(){
List<Rental> ls = rentalService.viewAllRental();
return ls;
}
}
This is service-
@Service
public class RentalService {
@Autowired
private RentalRepository rentalRepository;
@Autowired
private RentalTypeRepository rentalTypeRepository;
@Autowired
private SizeTypeRepository sizeTypeRepository;
@Autowired
private SleepingArrangementRepository sleepingArrangementRepository;
public Rental save(Rental rt) {
return rentalRepository.save(rt);
}
public Rental findRentalById(Long id) {
return rentalRepository.findAllByRentalId(id);
}
public List<Rental> viewAllRental(){
List<Rental> lsRental= new ArrayList<Rental();
lsRental = rentalRepository.findAll();
return lsRental;
}
public List<RentalType> viewAllRentalType(){
List<RentalType> lsRental= new ArrayList<RentalType();
lsRental = rentalTypeRepository.findAll();
return lsRental;
}
public List<SizeType> viewAllSizeType(){
List<SizeType> lsSizeType= new ArrayList<SizeType();
lsSizeType = sizeTypeRepository.findAll();
return lsSizeType;
}
}
This is repository
@Repository
public interface RentalRepository extends JpaRepository<Rental, Long> {
public Rental findAllByRentalId(Long id);
}
This is Rental
entity.
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Table(name="rental",schema="bnb_pms")
public class Rental {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="RENTAL_ID")
private long rentalId;
@Column(name="RENTAL_NAME")
private String rentalName;
@Column(name="INTERNAL_NAME")
private String internalName;
@Column(name="DES")
private String des;
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST })
@JoinColumn(name="TYPE_ID")
private RentalType rentalType;
@Column(name="RENTAL_SIZE")
private int rentalSize;
@Column(name="GUEST_ACCOMODATE")
private int guestAccomodate;
@Column(name="TOTAL_UNITS")
private int totalUnits;
}
This is RentalType
entity.
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name="rental_type",schema="bnb_pms")
public class RentalType {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="TYPE_ID")
private long typeId;
@Column(name="TYPE_NAME")
private String typeName;
}
I can see value of rental type in controller while debigging as shown in figure, debugger image But in postman rental type object is not shown.
Postman response is as following,
{
"rentalId": 453,
"rentalName": "Ehsan ulhaq Darrr1659",
"internalName": "Khadim Hussain Dar",
"des": "[email protected]",
"rentalType": {},
"rentalSize": 2,
"guestAccomodate": 4,
"totalUnits": 3
}
This issue has to do with your java objects serialization (via Jackson) to JSON, not JPA or the mappings. If you look at what is serialized:
"rentalType": {}
You can tell that there is an object being serialized, it just doesn't have any attributes or data. This is because Jackson will use property access by default.
If you check your entity definitions, the one that has data, Rental has:
@Getter @Setter @AllArgsConstructor @NoArgsConstructor
While the RentalType class is just using:
@NoArgsConstructor @AllArgsConstructor
If you add in the getters and setters, Jackson will be able to see the properties and include them in the object JSON.