1

I was creating an application with Spring Boot and I have this problem. When I send JSON with Postman with the data...

enter image description here

...it returns this:

enter image description here

In H2 the values are inserted as null.

enter image description here

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 
7
  • Spring handles configuration of most in memory databases really well, so you shouldn't need any configuration for a H2 database. Commented Aug 13, 2022 at 1:12
  • How to make this code work? Commented Aug 13, 2022 at 1:14
  • What does the application log when you make the post request? Commented Aug 13, 2022 at 1:35
  • Check whether the customer object in request body does not contain null Commented Aug 13, 2022 at 2:24
  • 1
    I wouldn't use try catch in 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 an ExceptionHandler. Commented Aug 13, 2022 at 12:01

1 Answer 1

1

to fix the code add setter to getters ta so in 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; public Long getAge() { return age; } public void setAge(Long age) { this.age = age; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Good job. However, I would only use the @Setter notation on the fields that need it, not the whole class. So: name, age, location. That way you do not allow changing thing that should not need to be changed.
could you do an example?
Just remove the @Setter on Customer and put them on the members, like this: @Setter private Long age; You are also using both Lombok annotations and are providing you own implementation. The @Getter and @Setter annotations from Lombok generate these functions for you, to minimize clutter from boilerplate in your class. So either remove the annoations, or remove the get/set functions and add Lombok to your dependencies
I can't provide this in an answer, since it isn't really an answer to your question. You can take a look at this entity in one of my recent repos on github that utilizes Lombok annotations. You can find information on Lombok here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.