使用 POST 时 Postman returns null(Spring 引导)

Postman returns null when using POST (Spring Boot)

** 更新

更新了 Postman 中的输入:

{
    "firstName": "Thierry",
    "lastName": "Blossom",
    "dateOfBirth": "1981-01-20",    
    "email": "thierry@gmail.com",
    "phoneNumber": "0612345678",
    "kids": "2",
    "otherPets": "1",
    "streetName": "Dorpstraat",
    "houseNumber": "60",
    "postalCode": "1000AB",
    "homeTown": "Amsterdam"   
}

我现在得到的结果是:

{
        "id": 3,
        "firstName": null,
        "lastName": null,
        "dateOfBirth": null,
        "address": {
            "id": 3,
            "streetName": "Dorpstraat",
            "houseNumber": "60",
            "postalCode": "1000AB",
            "homeTown": "Amsterdam"
        },
        "email": null,
        "phoneNumber": null,
        "kids": null,
        "otherPets": null,
        "fullName": "null null"
}

为什么其余数据为空?


原文post:

我已经更改代码 2 天了,并尝试使用 Internet 上的示例使其正常工作,但就是无法正常工作。

我的项目:我有一个客户和一个地址,这些需要链接在一起。通过 data.sql 它可以工作并在 Postman 中显示正确的信息。但是,当我想通过 Postman 自己添加客户时,每个字段都为空。

这是我在 Postman 中 post:

{
    "firstName": "Thierry",
    "lastName": "Blossom",
    "dateOfBirth": "1981-01-20",
    "address": {
        "streetName": "Dorpstraat",
        "houseNumber": "60",
        "postalCode": "1000AB",
        "homeTown": "Amsterdam"
    },
    "email": "thierry@gmail.com",
    "phoneNumber": "0612345678",
    "kids": "2",
    "otherPets": "1"
}

这是 Postman 给我的回复:

 {
        "id": 3,
        "firstName": null,
        "lastName": null,
        "dateOfBirth": null,
        "address": {
            "id": 3,
            "streetName": null,
            "houseNumber": null,
            "postalCode": null,
            "homeTown": null
        },
        "email": null,
        "phoneNumber": null,
        "kids": null,
        "otherPets": null,
        "fullName": "null null"
    },

我个人认为是controller或者service的问题导致不行。也许我忘记了什么?我使用的文件如下。

Address.java

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sun.istack.NotNull;
import javax.persistence.*;

@Entity
@Table(name = "address")
public class Address {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @NotNull
    @Column(name = "street_name")
    private String streetName;

    @NotNull
    @Column(name = "house_number")
    private String houseNumber;

    @NotNull
    @Column(name = "postal_code")
    private String postalCode;

    @NotNull
    @Column(name = "home_town")
    private String homeTown;

    @JsonIgnore
    @OneToOne(fetch = FetchType.LAZY,
            cascade = CascadeType.ALL,
            orphanRemoval = true)
    @JoinColumn(name = "customer_id")
    private Customer customer;

    public Address() {
    }

    public Address(String streetName, String houseNumber, String postalCode, String homeTown) {
        this.streetName = streetName;
        this.houseNumber = houseNumber;
        this.postalCode = postalCode;
        this.homeTown = homeTown;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getStreetName() {
        return streetName;
    }

    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }

    public String getHouseNumber() {
        return houseNumber;
    }

    public void setHouseNumber(String houseNumber) {
        this.houseNumber = houseNumber;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public String getHomeTown() {
        return homeTown;
    }

    public void setHomeTown(String homeTown) {
        this.homeTown = homeTown;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
}

Customer.java

import com.sun.istack.NotNull;

import javax.persistence.*;
import java.time.LocalDate;

@Entity
@Table(name = "customers")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @NotNull
    @Column(name = "first_name")
    private String firstName;

    @NotNull
    @Column(name = "last_name")
    private String lastName;

    @NotNull
    @Column(name = "date_of_birth")
    private LocalDate dateOfBirth;

    @OneToOne(fetch=FetchType.LAZY,
            mappedBy="customer")
    private Address address;

    @NotNull
    @Column
    private String email;

    @NotNull
    @Column(name = "phone_number")
    private String phoneNumber;

    @NotNull
    @Column
    private String kids;

    @NotNull
    @Column(name = "other_pets")
    private String otherPets;

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public Customer(String firstName, String lastName, LocalDate dateOfBirth, String email, String phoneNumber, String kids, String otherPets) {
    }

    public Customer(long id, String firstName, String lastName, LocalDate dateOfBirth, String email, String phoneNumber, String kids, String otherPets) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.dateOfBirth = dateOfBirth;
        this.email = email;
        this.phoneNumber = phoneNumber;
        this.kids = kids;
        this.otherPets = otherPets;
    }

    public Customer() {

    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public LocalDate getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(LocalDate dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getKids() {
        return kids;
    }

    public void setKids(String kids) {
        this.kids = kids;
    }

    public String getOtherPets() {
        return otherPets;
    }

    public void setOtherPets(String otherPets) {
        this.otherPets = otherPets;
    }

    public String getFullName() {
        return this.getFirstName() + " " + this.getLastName();
    }
}

RegisterCustomerRequest.java

import com.sun.istack.NotNull;

import java.time.LocalDate;

public class RegisterCustomerRequest {

    //Customer
    @NotNull
    private String firstName;
    @NotNull
    private String lastName;
    @NotNull
    private LocalDate dateOfBirth;
    @NotNull
    private String email;
    @NotNull
    private String phoneNumber;
    @NotNull
    private String kids;
    @NotNull
    private String otherPets;

    //Address
    @NotNull
    private String streetName;
    @NotNull
    private String houseNumber;
    @NotNull
    private String postalCode;
    @NotNull
    private String homeTown;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public LocalDate getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(LocalDate dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getKids() {
        return kids;
    }

    public void setKids(String kids) {
        this.kids = kids;
    }

    public String getOtherPets() {
        return otherPets;
    }

    public void setOtherPets(String otherPets) {
        this.otherPets = otherPets;
    }

    public String getStreetName() {
        return streetName;
    }

    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }

    public String getHouseNumber() {
        return houseNumber;
    }

    public void setHouseNumber(String houseNumber) {
        this.houseNumber = houseNumber;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public String getHomeTown() {
        return homeTown;
    }

    public void setHomeTown(String homeTown) {
        this.homeTown = homeTown;
    }
}

CustomerBuilder.java

import nl.danielle.cattery.payload.RegisterCustomerRequest;

import java.time.LocalDate;

public class CustomerBuilder {

    //Customer
    private String firstName;
    private String lastName;
    private LocalDate dateOfBirth;
    private String email;
    private String phoneNumber;
    private String kids;
    private String otherPets;

    //Address
    private String streetName;
    private String houseNumber;
    private String postalCode;
    private String homeTown;

    public CustomerBuilder(RegisterCustomerRequest registerCustomerRequest){
        this.firstName = registerCustomerRequest.getFirstName();
        this.lastName = registerCustomerRequest.getLastName();
        this.dateOfBirth = registerCustomerRequest.getDateOfBirth();
        this.email = registerCustomerRequest.getEmail();
        this.phoneNumber = registerCustomerRequest.getPhoneNumber();
        this.kids = registerCustomerRequest.getKids();
        this.otherPets = registerCustomerRequest.getOtherPets();
        this.streetName = registerCustomerRequest.getStreetName();
        this.houseNumber = registerCustomerRequest.getHouseNumber();
        this.postalCode = registerCustomerRequest.getPostalCode();
        this.homeTown = registerCustomerRequest.getHomeTown();
    }

    public Customer buildCustomer(){
        return new Customer(firstName, lastName, dateOfBirth, email, phoneNumber, kids, otherPets);
    }

    public Address buildAddress(){
        return new Address(streetName, houseNumber, postalCode, homeTown);
    }
}

CustomerController.java

import nl.danielle.cattery.model.Customer;
import nl.danielle.cattery.payload.RegisterCustomerRequest;
import nl.danielle.cattery.service.CustomerServiceImpl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.net.URI;

@RestController
@RequestMapping(value = "/customers")
public class CustomerController {

    final
    CustomerServiceImpl customerService;

    public CustomerController(CustomerServiceImpl customerService) {
        this.customerService = customerService;
    }

    @GetMapping(value = "")
    public ResponseEntity<Object> getCustomers() {
        return ResponseEntity.ok().body(customerService.getCustomers());
    }

    @GetMapping(value = "/{id}")
    public ResponseEntity<Object> getCustomer(@PathVariable("id") long id) {
        return ResponseEntity.ok().body(customerService.getCustomerById(id));
    }

    @PostMapping(value = "/add")
    public ResponseEntity<Object> createCustomer(@RequestBody RegisterCustomerRequest registerCustomerRequest) {
        long newId = customerService.createCustomer(registerCustomerRequest);

        URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
                .buildAndExpand(newId).toUri();

        return ResponseEntity.created(location).build();
    }

    @PutMapping(value = "/{id}")
    public ResponseEntity<Object> updateCustomer(@PathVariable("id") long id, @RequestBody Customer customer) {
        customerService.updateCustomer(id, customer);
        return ResponseEntity.noContent().build();
    }

    @DeleteMapping(value = "/{id}")
    public ResponseEntity<Object> deleteCustomer(@PathVariable("id") long id) {
        customerService.deleteCustomer(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping(value = "/lastname/{lastname}")
    public ResponseEntity<Object> getCustomerLastName(@PathVariable("lastname") String lastName) {
        return ResponseEntity.ok().body(customerService.getCustomerByLastName(lastName));
    }
}

CustomerServiceImpl.java

import nl.danielle.cattery.exceptions.DatabaseErrorException;
import nl.danielle.cattery.exceptions.RecordNotFoundException;
import nl.danielle.cattery.model.Address;
import nl.danielle.cattery.model.Customer;
import nl.danielle.cattery.model.CustomerBuilder;
import nl.danielle.cattery.payload.RegisterCustomerRequest;
import nl.danielle.cattery.repository.AddressRepository;
import nl.danielle.cattery.repository.CustomerRepository;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Service
public class CustomerServiceImpl implements CustomerService{

    final
    CustomerRepository customerRepository;

    final
    AddressRepository addressRepository;

    public CustomerServiceImpl(CustomerRepository customerRepository, AddressRepository addressRepository) {
        this.customerRepository = customerRepository;
        this.addressRepository = addressRepository;
    }

    @Override
    public Collection<Customer> getCustomers() {
        return customerRepository.findAll();
    }

    @Override
    public Customer getCustomerById(long id) {
        if (!customerRepository.existsById(id)) {
            throw new RecordNotFoundException();
        }
        return customerRepository.findById(id).orElse(null);
    }

    @Override
    public long createCustomer(RegisterCustomerRequest registerCustomerRequest) {

        Customer customer = new CustomerBuilder(registerCustomerRequest).buildCustomer();
        Address address = new CustomerBuilder(registerCustomerRequest).buildAddress();

        Address savedAddress = addressRepository.save(address);
        customer.setAddress(savedAddress);
        address.setCustomer(customer);

        return customerRepository.save(customer).getId();
    }

    @Override
    public void updateCustomer(long id, Customer customer) {
        if (customerRepository.existsById(id)) {
            try {
                Customer existingCustomer = customerRepository.findById(id).orElse(null);
                existingCustomer.setFirstName(customer.getFirstName());
                existingCustomer.setLastName(customer.getLastName());
                existingCustomer.setDateOfBirth(customer.getDateOfBirth());
                existingCustomer.setEmail(customer.getEmail());
                existingCustomer.setPhoneNumber(customer.getPhoneNumber());
                existingCustomer.setKids(customer.getKids());
                existingCustomer.setOtherPets(customer.getOtherPets());
                customerRepository.save(existingCustomer);
            } catch (Exception e) {
                throw new DatabaseErrorException();
            }
        } else {
            throw new RecordNotFoundException();
        }
    }

    @Override
    public void deleteCustomer(long id) {
        customerRepository.deleteById(id);
    }

    @Override
    public Customer getCustomerByLastName(String lastName) {
        return customerRepository.findByLastNameIgnoreCase(lastName);
    }
}

data.sql

INSERT INTO customers (first_name, last_name, date_of_birth, email, phone_number, kids, other_pets)
VALUES
    ('Danielle', 'van den Akker', '1983-06-28', 'intoyou@gmail.com', '0612345678', '2', '1'),
    ('Simone', 'Lageboom', '1981-03-15', 'simone@gmail.com', '0687654321', '0', '3');

INSERT into address (street_name, house_number, postal_code, home_town, customer_id)
VALUES
    ('Dorpstraat', '250', '1000AB', 'Amsterdam', 1),
    ('Laan', '10', '1000AB', 'Utrecht', 2);

data.sql returns 以下使用 Postman 中的 GET 方法,因此确实有效。

[
    {
        "id": 1,
        "firstName": "Danielle",
        "lastName": "van den Akker",
        "dateOfBirth": "1983-06-28",
        "address": {
            "id": 1,
            "streetName": "Dorpstraat",
            "houseNumber": "250",
            "postalCode": "1000AB",
            "homeTown": "Amsterdam"
        },
        "email": "intoyou@gmail.com",
        "phoneNumber": "0612345678",
        "kids": "2",
        "otherPets": "1",
        "fullName": "Danielle van den Akker"
    },
    {
        "id": 2,
        "firstName": "Simone",
        "lastName": "Kerseboom",
        "dateOfBirth": "1981-03-15",
        "address": {
            "id": 2,
            "streetName": "Laan",
            "houseNumber": "10",
            "postalCode": "1000AB",
            "homeTown": "Utrecht"
        },
        "email": "simone@gmail.com",
        "phoneNumber": "0687654321",
        "kids": "0",
        "otherPets": "3",
        "fullName": "Simone Kerseboom"
    }
]
{
    "firstName": "Thierry",
    "lastName": "Blossom",
    "dateOfBirth": "1981-01-20",
    "address": {
        "streetName": "Dorpstraat",
        "houseNumber": "60",
        "postalCode": "1000AB",
        "homeTown": "Amsterdam"
    },
    "email": "thierry@gmail.com",
    "phoneNumber": "0612345678",
    "kids": "2",
    "otherPets": "1"
}

RegisterCustomerRequest 不匹配,您在我们的请求 class 中没有 address 属性。我猜 body 无法解析为预期的对象并以 null 值结束,然后将其保留。

请检查@RequestBody 是否正确注释了 import org.springframework.web.bind.annotation.RequestBody;