I was creating an application with Spring Boot and I have this problem. When I send JSON with Postman with the data...
...it returns this:
In H2 the values are inserted as null.
Spring starter code.
Springboothdbh2Application.java
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Springboothdbh2Application { public static void main(String[] args) { SpringApplication.run(Springboothdbh2Application.class, args); } } Customer.java
package com.example.demo.entity; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UpdateTimestamp; import lombok.Getter; import lombok.Setter; import lombok.ToString; @Entity @Table(name="tbl_customer") @Setter @Getter @ToString public class Customer { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; private String name; private Long age; private String location; @CreationTimestamp @Column(name="created_at", nullable=false, updatable=false) private Date createdAt; @UpdateTimestamp @Column(name="updated_at") private Date updatedAt; } CustomerController.java
package com.example.demo.controller; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.entity.Customer; import com.example.demo.repository.ICustomerRepo; @RestController @RequestMapping("/api") public class CustomerController { @Autowired ICustomerRepo customerRepo; @GetMapping("/customers") public ResponseEntity<List<Customer>> getAllCustomers() { try { List<Customer> list = customerRepo.findAll(); if (list.isEmpty() || list.size() == 0) { return new ResponseEntity<>(HttpStatus.NO_CONTENT); } return new ResponseEntity<>(list, HttpStatus.OK); } catch (Exception e) { return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); } } @GetMapping("/customers/{id}") public ResponseEntity<Customer> getCustomer(@PathVariable Long id) { Optional<Customer> customer = customerRepo.findById(id); if (customer.isPresent()) { return new ResponseEntity<>(customer.get(), HttpStatus.OK); } return new ResponseEntity<>(HttpStatus.NOT_FOUND); } @PostMapping("/customers") public ResponseEntity<Customer> saveCustomer(@RequestBody Customer customer) { try { return new ResponseEntity<>(customerRepo.save(customer), HttpStatus.CREATED); } catch (Exception e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @PutMapping("/customers") public ResponseEntity<Customer> updateCustomer(@RequestBody Customer customer) { try { return new ResponseEntity<>(customerRepo.save(customer), HttpStatus.OK); } catch (Exception e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @DeleteMapping("/customers/{id}") public ResponseEntity<HttpStatus> deleteCustomer(@PathVariable Long id) { try { Optional<Customer> customer = customerRepo.findById(id); if (customer.isPresent()) { customerRepo.delete(customer.get()); } return new ResponseEntity<>(HttpStatus.NO_CONTENT); } catch (Exception e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } } ICustumerRepo.java
package com.example.demo.repository; import org.springframework.data.jpa.repository.JpaRepository; import com.example.demo.entity.Customer; public interface ICustomerRepo extends JpaRepository<Customer, Long> { } This is my Spring Boot application properties file:
spring.h2.console.enabled=true spring.datasource.url=jdbc:h2:mem:crm spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect spring.jpa.hibernate.ddl-auto=update 



try catchin every endpoint (it creates clutter), rather throw a custom exception and annotate the exception with something like this:@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Actor Not Found")or use anExceptionHandler.