I am getting below given validation exception while sending http request from Postman to test Rest API.
2024-08-10T21:52:10.773+05:30 ERROR 24976 --- [validate-barcode] [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction] with root cause
jakarta.validation.ConstraintViolationException: Validation failed for classes [com.validate.validate_barcode.entity.Product] during persist time for groups [jakarta.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage=’must not be null’, propertyPath=createdDate, rootBeanClass=class com.validate.validate_barcode.entity.Product, messageTemplate='{jakarta.validation.constraints.NotNull.message}’}
]
at org.hibernate.boot.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:151) ~[hibernate-core-6.5.2.Final.jar:6.5.2.Final]
In my bean class, I have two instance variable createdDate and lastModifiedDate to capture record creation and updating date in the database.
My model class is –
@Entity
@Table(name = "product_master")
public class Product {
// Primary ID which increments automatically when new entry is added into the
// database
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
int id;
@Column(name = "product_code")
@NotNull
private String productCode;
@Column(name = "product_name")
@NotNull
private String productName;
@CreatedDate
@NotNull
@Column(name = "created_at", nullable = false, updatable = false)
private LocalDateTime createdDate;
@LastModifiedDate
@Column(name = "updated_at")
private LocalDateTime lastModifiedDate;
}
Controller Class is –
//Annotation
@RestController
@RequestMapping("/validateproduct")
public class ProductController {
// Annotation
@Autowired
private ProductService productSerive;
// Save operation
//@PostMapping("/saveProduct")
@RequestMapping(value = "/saveProduct", produces = "application/json")
public Product saveProduct(@RequestBody Product product) {
System.out.println("Post Mapping - saveProduct");
System.out.println(product.toString());
return productSerive.saveProduct(product);
}
}
In Postmen, I am sending data in body and have selected Body -> raw in Postmen. And request data is –
{
"productCode":"abc1234",
"productName":"ABCDEFG"
}
I explored some of the article to save created_at and updated_at, and I added @CreatedDate and @LastModifiedDate annotation.
pom.xml is –
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Is here anything wrong, I am doing?