Spring 引导:实体和数据库之间的映射不起作用。(在响应正文中获取 {})
Spring Boot : mapping between entity and database not working.(Getting {} in response body)
我正在尝试创建一个网络服务,但是当我尝试从数据库 (MariaDB) 获取记录时,响应显示为 {} 并且没有显示实际的 fields.The 代码如下。
(隐藏导入)
Customer.java
@AllArgsConstructor
@Data
@Entity
@Table(name ="customer")
public class Customer {
@Id
@Column(name="customer_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
Long customerId;
@Column(name = "acc_holder_name")
String accHolderName;
@Column(name = "clear_balance")
Long clearBalance;
@Column(name = "over_draft")
Boolean overDraft;
public Customer() {}
public Customer(Long id)
{
this.customerId = id;
this.accHolderName = "default";
this.clearBalance = (long) 0;
this.overDraft = false;
}
public String toString()
{
return this.customerId + this.accHolderName;
}
public Customer(String accHolderName, long clearBalance, boolean overDraft)
{
this.accHolderName = accHolderName;
this.clearBalance = clearBalance;
this.overDraft = overDraft;
}
}
CustomerService.java
@Service
public class CustomerService {
private CustomerRepository customerRepo;
@Autowired
public CustomerService(CustomerRepository cr)
{
this.customerRepo = cr;
}
public List<Customer> getCustomers(){
List<Customer> ls = customerRepo.findAll();
return ls;
}
}
CustomerController.java
@RestController
@RequestMapping("customer")
public class CustomerController {
private CustomerService custService;
@Autowired
public CustomerController(CustomerService cs)
{
this.custService = cs;
}
@GetMapping
@ResponseBody
public List<Customer> getCustomers() {
return custService.getCustomers();
}
}
application.properties
spring.datasource.url=jdbc:mariadb://localhost:3306/sample-db
spring.datasource.username=hidden
spring.datasource.password=hidden
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto = update
CustomerRepostitory.java
@Repository
public interface CustomerRepository extends JpaRepository<Customer,Long> {
}
我的table如下
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| customer_id | int(11) | NO | PRI | NULL | auto_increment |
| acc_holder_name | varchar(40) | YES | | NULL | |
| clear_balance | int(11) | YES | | NULL | |
| over_draft | tinyint(1) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+----------------+
我的 table 插入了 6 条记录。我发送以下请求
curl --location --request GET 'localhost:8080/customer/' \
--data-raw ''
并得到以下响应
[
{},
{},
{},
{},
{},
{}
]
似乎无法将 JSON 序列化到输出,因此您可以将 @JsonProperty("variable")
添加到 Customer
对象中的所有变量中,如下所示:
public class Customer {
@Id
@Column(name="customer_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
@JsonProperty("customerId")
Long customerId;
@Column(name = "acc_holder_name")
@JsonProperty("accHolderName")
String accHolderName;
@Column(name = "clear_balance")
@JsonProperty("clearBalance")
Long clearBalance;
@Column(name = "over_draft")
@JsonProperty("overDraft")
Boolean overDraft;
// ...
}
不要在 Rest 中公开 JPA 实体 API。这是一种不好的做法。公开您的实体会在您的 API 和持久性模型之间建立强耦合。
因此,使用 DTO(数据传输对象)来 return 您的数据。您可以使用模型映射器将实体映射到 DTO,如下所示。
@Data
public class CustomerDTO {
Long customerId;
String accHolderName;
Long clearBalance;
Boolean overDraft;
}
使用波纹管对你的 pom.xml 的依赖来获取模型映射器。
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.4.5</version>
</dependency>
然后将实体映射到 DTO,如下所示。
public List<CustomerDTO> getCustomers(){
List<CustomerDTO> ls = customerRepo.findAll()
.stream()
.map(customer->
new ModelMapper().map(page, SimpleBaseDTO.class))
.collect(Collectors.toList());
return ls;
}
我正在尝试创建一个网络服务,但是当我尝试从数据库 (MariaDB) 获取记录时,响应显示为 {} 并且没有显示实际的 fields.The 代码如下。
(隐藏导入)
Customer.java
@AllArgsConstructor
@Data
@Entity
@Table(name ="customer")
public class Customer {
@Id
@Column(name="customer_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
Long customerId;
@Column(name = "acc_holder_name")
String accHolderName;
@Column(name = "clear_balance")
Long clearBalance;
@Column(name = "over_draft")
Boolean overDraft;
public Customer() {}
public Customer(Long id)
{
this.customerId = id;
this.accHolderName = "default";
this.clearBalance = (long) 0;
this.overDraft = false;
}
public String toString()
{
return this.customerId + this.accHolderName;
}
public Customer(String accHolderName, long clearBalance, boolean overDraft)
{
this.accHolderName = accHolderName;
this.clearBalance = clearBalance;
this.overDraft = overDraft;
}
}
CustomerService.java
@Service
public class CustomerService {
private CustomerRepository customerRepo;
@Autowired
public CustomerService(CustomerRepository cr)
{
this.customerRepo = cr;
}
public List<Customer> getCustomers(){
List<Customer> ls = customerRepo.findAll();
return ls;
}
}
CustomerController.java
@RestController
@RequestMapping("customer")
public class CustomerController {
private CustomerService custService;
@Autowired
public CustomerController(CustomerService cs)
{
this.custService = cs;
}
@GetMapping
@ResponseBody
public List<Customer> getCustomers() {
return custService.getCustomers();
}
}
application.properties
spring.datasource.url=jdbc:mariadb://localhost:3306/sample-db
spring.datasource.username=hidden
spring.datasource.password=hidden
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto = update
CustomerRepostitory.java
@Repository
public interface CustomerRepository extends JpaRepository<Customer,Long> {
}
我的table如下
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| customer_id | int(11) | NO | PRI | NULL | auto_increment |
| acc_holder_name | varchar(40) | YES | | NULL | |
| clear_balance | int(11) | YES | | NULL | |
| over_draft | tinyint(1) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+----------------+
我的 table 插入了 6 条记录。我发送以下请求
curl --location --request GET 'localhost:8080/customer/' \
--data-raw ''
并得到以下响应
[
{},
{},
{},
{},
{},
{}
]
似乎无法将 JSON 序列化到输出,因此您可以将 @JsonProperty("variable")
添加到 Customer
对象中的所有变量中,如下所示:
public class Customer {
@Id
@Column(name="customer_id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
@JsonProperty("customerId")
Long customerId;
@Column(name = "acc_holder_name")
@JsonProperty("accHolderName")
String accHolderName;
@Column(name = "clear_balance")
@JsonProperty("clearBalance")
Long clearBalance;
@Column(name = "over_draft")
@JsonProperty("overDraft")
Boolean overDraft;
// ...
}
不要在 Rest 中公开 JPA 实体 API。这是一种不好的做法。公开您的实体会在您的 API 和持久性模型之间建立强耦合。 因此,使用 DTO(数据传输对象)来 return 您的数据。您可以使用模型映射器将实体映射到 DTO,如下所示。
@Data
public class CustomerDTO {
Long customerId;
String accHolderName;
Long clearBalance;
Boolean overDraft;
}
使用波纹管对你的 pom.xml 的依赖来获取模型映射器。
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.4.5</version>
</dependency>
然后将实体映射到 DTO,如下所示。
public List<CustomerDTO> getCustomers(){
List<CustomerDTO> ls = customerRepo.findAll()
.stream()
.map(customer->
new ModelMapper().map(page, SimpleBaseDTO.class))
.collect(Collectors.toList());
return ls;
}