Home > database >  Java SpringBoot POST'ing to Postgresql results in NULL values
Java SpringBoot POST'ing to Postgresql results in NULL values

Time:07-21

I am trying to build a simple web application that consists of three connected PostgreSQL tables and REST API to manage them. However, when I try to add a new customer via POST, all the columns are inserted as NULL.

Customer.java

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@NonNull
@Table(name = "customers")
@Entity
public class Customer implements Serializable {
    private @Id @Column(name = "customerid")
    @GeneratedValue(strategy = GenerationType.IDENTITY) Long id;
    @Column(name = "customer_name")
    private String customerName;
    @Column(name = "customer_surname")
    private String customerSurname;
}

CustomerRepository.java

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}

ICustomerService.java

public interface ICustomerService {
    List<Customer> getAll();
    Customer getByID(Long id);
    Long add(Customer c);
    void remove(Long id);
}

CustomerService.java

@Service
public class CustomerService implements ICustomerService {
    private CustomerRepository customerRepository;
    @Autowired
    public CustomerService(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }
    @Override
    public List<Customer> getAll() {
        return this.customerRepository.findAll();
    }
    @Override
    public Customer getByID(Long id) {
        return this.customerRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(String.format("Customer %d not found.", id)));
    }
    @Override
    public Long add(Customer c) {
        return this.customerRepository.save(c).getId();
    }
    @Override
    public void remove(Long id) {
        this.customerRepository.deleteById(id);
    }
}

CustomerController.java

@RestController
@RequestMapping("/customers")
public class CustomerController {
    private final ICustomerService customerService;
    public CustomerController(ICustomerService customerService) {
        this.customerService = customerService;
    }
    @GetMapping
    public List<Customer> getAll() {
        return this.customerService.getAll();
    }
    @GetMapping("/{customerid}")
    public Customer getByID(@PathVariable("customerid") Long customerid) {
        return this.customerService.getByID(customerid);
    }
    @PostMapping
    public Long add(@RequestBody Customer c) {
        return this.customerService.add(c);
    }
    @DeleteMapping("/{customerid}")
    public void remove(@PathVariable("customerid") Long customerid) {
        this.customerService.remove(customerid);
    }
}

My example POST request:

curl http://localhost:8080/customers -X POST -d '{"customer_name": "John","customer_surname": "Smith"}' -H 'Content-Type: application/json'

The resulting entry in the customer table:

select * from customer where customerid = 1
 ------------------------------------------------
 |customerid | customer_name | customer_surname |
 -----------------------------------------------
 | 1         | NULL          | NULL             |
 ------------------------------------------------

CodePudding user response:

Your post request references name of the table instead of the variable in Customer class. You can either modify your request:

{"customerName": "John","customerSurname": "Smith"}

or modify your entity adding json property annotations as below:

@Column(name = "customer_name")
@JsonProperty("customer_name")
private String customerName;
@Column(name = "customer_surname")
@JsonProperty("customer_surname")
private String customerSurname;
  • Related