java - 创建银行 API 并让客户拥有地址。我该怎么做?
问题描述
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String street_number;
private String street_name;
private String city;
private String state;
private String zip;
public Address() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStreet_number() {
return street_number;
}
public void setStreet_number(String street_number) {
this.street_number = street_number;
}
public String getStreet_name() {
return street_name;
}
public void setStreet_name(String street_name) {
this.street_name = street_name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}
package com.bankingapplicationmain.bankingapplicationmain.models;
import javax.persistence.*;
import java.util.Set;
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String first_Name;
private String last_Name;
@OneToMany
private Set<Address> addresses;
public Customer() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirst_Name() {
return first_Name;
}
public void setFirst_Name(String first_Name) {
this.first_Name = first_Name;
}
public String getLast_Name() {
return last_Name;
}
public void setLast_Name(String last_Name) {
this.last_Name = last_Name;
}
}
到目前为止,我尝试使用“多对一”、“多对多”,但我想我仍然不确定这些是如何工作的!我的创建、删除、方法工作正常,但是当我尝试获取所有客户时,他们都丢失了地址。
我发布的内容:
{
"id": 1,
"first_name": "Leon",
"last_name": "Hunter",
"address": {
"street_number": "902",
"street_name": "Walker Road",
"city": "Clearfield",
"state": "Pennsylvania",
"zip": "16830"
}
}
VS我得到的:
{
"id": 1,
"first_name": "Leon",
"last_name": "Hunter"
非常感谢任何帮助!
编辑:
import com.bankingapplicationmain.bankingapplicationmain.models.Customer;
import com.bankingapplicationmain.bankingapplicationmain.services.CustomerService;
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;
@RestController
@RequestMapping("/customer")
public class CustomerController {
@Autowired
CustomerService customerService;
//get all customers
@GetMapping
public ResponseEntity<List<Customer>> getAllCustomers(){
return customerService.getAllCustomers();
}
//get customer by id
@GetMapping("/{id}")
public ResponseEntity<Customer> getCustomerById(@PathVariable Long id){
return customerService.getCustomerById(id);
}
//create customer
@PostMapping
public ResponseEntity<?> createCustomer(@Valid @RequestBody Customer customer) {
return customerService.createCustomer(customer);
}
//edit customer
@PutMapping("/{id}")
public ResponseEntity<?> updateCustomer(@PathVariable Long id, @Valid @RequestBody Customer customer){
return customerService.updateCustomer(customer, id);
}
}
package com.bankingapplicationmain.bankingapplicationmain.services;
import com.bankingapplicationmain.bankingapplicationmain.exceptions.CustomerNotFoundException;
import com.bankingapplicationmain.bankingapplicationmain.models.Customer;
import com.bankingapplicationmain.bankingapplicationmain.repositories.CustomerRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.net.URI;
import java.util.List;
import java.util.Optional;
@Service
public class CustomerService {
private CustomerRepository customerRepository;
private static final Logger logger = LoggerFactory.getLogger(CustomerService.class);
@Autowired
public CustomerService(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
protected void verifyCustomer(Long customerId) throws CustomerNotFoundException {
Optional<Customer> customer = customerRepository.findById(customerId);
if(customer.isEmpty()) {
throw new CustomerNotFoundException("Customer with id " + customerId + " not found");
}
}
// Get all customers
public ResponseEntity<List<Customer>> getAllCustomers() {
logger.info("Customer(s) found.");
return new ResponseEntity<>(customerRepository.findAll(), HttpStatus.OK);
}
//get customer by id
public ResponseEntity<Customer> getCustomerById(Long customerId) {
if (customerRepository.findById(customerId).isPresent()) {
logger.info("Customer found.");
customerRepository.findById(customerId);
}
throw new CustomerNotFoundException("Customer with id " + customerId + " not found");
}
public ResponseEntity<?> createCustomer(Customer customer){
logger.info("Customer created.");
customerRepository.save(customer);
HttpHeaders responseHeaders = new HttpHeaders();
URI newCustomerUri = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(customer.getId())
.toUri();
responseHeaders.setLocation(newCustomerUri);
return new ResponseEntity<>(null, responseHeaders, HttpStatus.CREATED);
}
public ResponseEntity<?> updateCustomer(Customer customer, Long customerId) {
verifyCustomer(customerId);
logger.info("Customer info updated.");
customerRepository.save(customer);
return new ResponseEntity<>(HttpStatus.OK);
}
}
根据其他人的要求,我也添加了我的服务和控制器类。也许我在这里也做错了什么。
解决方案
我建议地址的一对一关系,这样各个客户的所有数据都将分开。
例如,如果您的妈妈、爸爸和孩子都链接到一个地址并且孩子搬走了,您可能会更改他们的地址并更改父母的地址,这是不受欢迎的行为。
As for the Address not showing, what code are you using to retrieve the data? I suspect because it isn't a primitive value(string) you may need to add a method to combine all the address data into a single string
推荐阅读
- python-3.x - Bitwise_and 函数返回错误断言失败且掩码相同
- php - PHP魔术常量 - 自动传递给函数?
- javascript - Stripe:如何在给定信用卡信息的情况下创建客户对象
- node.js - 如何解决 AWS Lambda 中的 Runtime.HandlerNotFound 错误
- laravel - 在 Laravel 中为关系分配 id
- android - 单一原因导致主线程上的网络或来自错误线程异常的查看根
- swift - 如何在 Xcode 中向 Google Maps API 添加叠加层
- css - 在 CSS 中选择最内部的等效规则
- java - 如何根据比较连续的列表元素将 Java 流减少为布尔值
- javascript - gif动画只有在包含slick.js幻灯片后才能启动吗?