renad sahal
renad sahal

Reputation: 187

DTO insert null values into database in springboot

I'm just trying to walk through DTO concept in spring boot app So I create a customer mode, Customer DTO, Customer Service and Controller

But when i test the API through postman, it's insert null values for all attrivbutes, but i don't now why...

and this is the service :

@Override
@Transactional
public Customer addCustomer(Customer customer) {
    Customer newCustomer = cutomerRepository.save(customer);
    return newCustomer;
}

and at last this is the controller :

public ResponseEntity<Customer> addCustomer(CustomerDTO customer) {
    try {
        System.out.println(customer.getEmail()+"000000000000000000000000");
        Customer newCustomer = customerService
                .addCustomer(new Customer(customer.getSerialNumber(), customer.getFirstName(),customer.getLastName(),customer.getEmail(),customer.getMobileNumber()));
        System.out.println(customer.getEmail()+ "************************");
        return new ResponseEntity<>(newCustomer, HttpStatus.CREATED);
    } catch (Exception e) {
        return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

the output from 000 and *** are null, and this is how i insert values

Any help or Idea ?

Upvotes: 0

Views: 1243

Answers (1)

Antoniossss
Antoniossss

Reputation: 32535

You need @RequestBody on controller's handler method arg

public ResponseEntity<Customer> addCustomer(@RequestBody CustomerDTO customer) {

Upvotes: 1

Related Questions