在这种情况下,我试图获取车辆数据。当获取数据但映射的数据不能从中取出时。
这是我的控制器类。
package com.ashen.cdb.cdb_api.controller;
import com.ashen.cdb.cdb_api.model.Vehicle;
import com.ashen.cdb.cdb_api.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@CrossOrigin(maxAge = 3600)
@RestController
public class VehicleController {
@Autowired
private VehicleService vehicleService;
@PostMapping(path = "/vehicle")
Vehicle addVehicle(@RequestBody Vehicle vehicle){
return vehicleService.addVehicle(vehicle);
}
@CrossOrigin(origins = "http://localhost:8080")
@GetMapping(path = "/vehicle")
Iterable<Vehicle> getAllVehicles(){
return vehicleService.getAllVehicle();
}
@GetMapping(path = "/vehicle/{vehicle_id}")
Vehicle getVehicle(@PathVariable int vehicle_id){
return vehicleService.getVehicle(vehicle_id);
}
}
车辆模型类。
package com.ashen.cdb.cdb_api.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.CreationTimestamp;
import javax.persistence.*;
import java.util.Date;
import java.util.Set;
@Entity
@Table(name = "vehicle")
public class Vehicle {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "vehicle_id")
private int vehicle_id;
@Column(name = "vehicle_no")
private String vehicle_no;
@Column(name = "vehicle_brand")
private String vehicle_brand;
@Column(name = "vehicle_model")
private String vehicle_model;
@Column(name = "vehicle_man_year")
private String vehicle_man_year;
@Column(name = "vehicle_reg_year")
private String vehicle_reg_year;
@Column(name = "vehicle_inventory")
private String vehicle_inventory;
@Column(name = "legal_state")
private int legal_state;
//sys_user foreign key from user table
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id",unique = true)
@JsonIgnore
private User user;
@OneToMany(mappedBy = "vehicle", cascade = CascadeType.ALL)
private Set<BidHistory> bidHistory;
@Column(name = "created_at")
@CreationTimestamp
private Date created_at;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int getVehicle_id() {
return vehicle_id;
}
public void setVehicle_id(int vehicle_id) {
this.vehicle_id = vehicle_id;
}
public String getVehicle_no() {
return vehicle_no;
}
public void setVehicle_no(String vehicle_no) {
this.vehicle_no = vehicle_no;
}
public String getVehicle_brand() {
return vehicle_brand;
}
public void setVehicle_brand(String vehicle_brand) {
this.vehicle_brand = vehicle_brand;
}
public String getVehicle_model() {
return vehicle_model;
}
public void setVehicle_model(String vehicle_model) {
this.vehicle_model = vehicle_model;
}
public String getVehicle_man_year() {
return vehicle_man_year;
}
public void setVehicle_man_year(String vehicle_man_year) {
this.vehicle_man_year = vehicle_man_year;
}
public String getVehicle_reg_year() {
return vehicle_reg_year;
}
public void setVehicle_reg_year(String vehicle_reg_year) {
this.vehicle_reg_year = vehicle_reg_year;
}
public String getVehicle_inventory() {
return vehicle_inventory;
}
public void setVehicle_inventory(String vehicle_inventory) {
this.vehicle_inventory = vehicle_inventory;
}
public int getLegal_state() {
return legal_state;
}
public void setLegal_state(int legal_state) {
this.legal_state = legal_state;
}
public Date getCreated_at() {
return created_at;
}
public void setCreated_at(Date created_at) {
this.created_at = created_at;
}
public Set<BidHistory> getBidHistory() {
return bidHistory;
}
public void setBidHistory(Set<BidHistory> bidHistory) {
this.bidHistory = bidHistory;
}
}
车辆存储库层。
package com.ashen.cdb.cdb_api.repository;
import com.ashen.cdb.cdb_api.model.Vehicle;
import org.springframework.data.repository.CrudRepository;
public interface VehicleRepository extends CrudRepository<Vehicle, Integer> {
}
VehcileService类别
package com.ashen.cdb.cdb_api.service;
import com.ashen.cdb.cdb_api.model.Vehicle;
public interface VehicleService {
Iterable<Vehicle> getAllVehicle();
Vehicle addVehicle(Vehicle vehicle);
Vehicle getVehicle(Integer vehicle_id);
}
VehicleServiceImple类
package com.ashen.cdb.cdb_api.service;
import com.ashen.cdb.cdb_api.model.Vehicle;
import com.ashen.cdb.cdb_api.repository.VehicleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class VehicleServiceimpl implements VehicleService{
@Autowired
private VehicleRepository vehicleRepository;
@Override
public Vehicle addVehicle(Vehicle vehicle){
return vehicleRepository.save(vehicle);
}
@Override
public Vehicle getVehicle(Integer vehicle_id){
return null;
}
@Override
public Iterable<Vehicle> getAllVehicle(){
return vehicleRepository.findAll();
}
}
这是输出。
[
{
"vehicle_id": 1,
"vehicle_no": "CDS 1259",
"vehicle_brand": "Toyota",
"vehicle_model": "Xtrail",
"vehicle_man_year": "2018",
"vehicle_reg_year": "2019",
"vehicle_inventory": null,
"legal_state": 1,
"bidHistory": [],
"created_at": "2020-03-22T07:32:51.000+0000"
}
]
此处的bidHistory []数据应从bidHistory类获取。但是这组是空的。