'Postman returns null when using POST (Spring Boot)
** Update
Updated the input in Postman:
{
"firstName": "Thierry",
"lastName": "Blossom",
"dateOfBirth": "1981-01-20",
"email": "[email protected]",
"phoneNumber": "0612345678",
"kids": "2",
"otherPets": "1",
"streetName": "Dorpstraat",
"houseNumber": "60",
"postalCode": "1000AB",
"homeTown": "Amsterdam"
}
I now get as a result:
{
"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"
}
Why do I get null for the rest of the data?
Original post:
I've been changing code for 2 days now and trying to get it to work using examples on the internet, but just can't get it to work.
My project: I have a customer and an address, these need to be linked together. Via data.sql it worked and shows the correct info in Postman. However, when I want to add a customer myself via Postman, I get null for each field.
This is what I post in Postman:
{
"firstName": "Thierry",
"lastName": "Blossom",
"dateOfBirth": "1981-01-20",
"address": {
"streetName": "Dorpstraat",
"houseNumber": "60",
"postalCode": "1000AB",
"homeTown": "Amsterdam"
},
"email": "[email protected]",
"phoneNumber": "0612345678",
"kids": "2",
"otherPets": "1"
}
This is what Postman gives me back:
{
"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"
},
I personally think it's because of the controller or the service that it does not work. Maybe I forget something? The files I use are the following.
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', '[email protected]', '0612345678', '2', '1'),
('Simone', 'Lageboom', '1981-03-15', '[email protected]', '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);
The data.sql returns the following with method GET in Postman, so that does work.
[
{
"id": 1,
"firstName": "Danielle",
"lastName": "van den Akker",
"dateOfBirth": "1983-06-28",
"address": {
"id": 1,
"streetName": "Dorpstraat",
"houseNumber": "250",
"postalCode": "1000AB",
"homeTown": "Amsterdam"
},
"email": "[email protected]",
"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": "[email protected]",
"phoneNumber": "0687654321",
"kids": "0",
"otherPets": "3",
"fullName": "Simone Kerseboom"
}
]
Solution 1:[1]
{
"firstName": "Thierry",
"lastName": "Blossom",
"dateOfBirth": "1981-01-20",
"address": {
"streetName": "Dorpstraat",
"houseNumber": "60",
"postalCode": "1000AB",
"homeTown": "Amsterdam"
},
"email": "[email protected]",
"phoneNumber": "0612345678",
"kids": "2",
"otherPets": "1"
}
Does not match the RegisterCustomerRequest
, you don't have an address
property in our request class. I guess the body cannot be parsed to the expected object and ends up with null
values which are then persisted.
Solution 2:[2]
Please check that the @RequestBody is properly annotated with import org.springframework.web.bind.annotation.RequestBody;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | João Dias |
Solution 2 | binsy badarudeen |