Skip to content

Hibernate Validator error messages not showing on console #9174

Open

Description

Describe the bug
When using @Valid annotation, the error messages are not showing on quarkus console. Only on the client side. If the @Valid is not used, the error shows up on the console

Expected behavior
Any error that occurs to be printed on quarkus console

To Reproduce

@POST
    @Transactional
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @Operation(summary = "POST a product", description = "Add a new product to the inventory")
    @APIResponse(responseCode = "200", description = "Product registration successful")
    @APIResponse(responseCode = "400", description = "Invalid Product")
    @APIResponse(responseCode = "500", description = "Server unavailable")
    @APIResponse(description = "Product", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Product.class)))
    public Response add(Product product) {
        try {
            product.addUUID();
            productService.validateProduct(product);
            productRepository.persist(product);
            return Response.status(Status.CREATED).entity(product).build();
        } catch (ConstraintViolationException e) {
            e.printStackTrace();
            return Response.status(Status.BAD_REQUEST).entity(e.getConstraintViolations()).build();
        }
    }
package com.github.kgoedert.crm.product;

import java.time.LocalDateTime;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.FutureOrPresent;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.github.kgoedert.crm.StringEnumeration;
import com.github.kgoedert.crm.UUID;

import org.eclipse.microprofile.openapi.annotations.media.Schema;

import io.quarkus.hibernate.orm.panache.PanacheEntityBase;

@Entity
@Table(name = "products")
@Access(AccessType.FIELD)
@Schema(name = "Product", description = "Represents a product in the inventory")
public class Product extends PanacheEntityBase {
    @Id
    @GeneratedValue(generator = "productSeq", strategy = GenerationType.SEQUENCE)
    @SequenceGenerator(name = "productSeq", sequenceName = "product_id_seq", allocationSize = 1)
    @JsonIgnore
    private Long id;

    @Column(name = "name")
    @NotBlank(message = "Name may not be blank")
    @Schema(required = true, example = "Rice")
    private String name;

    @Column(name = "price")
    @DecimalMin(value = "0.1", message = "The price has to be at least 0.1")
    @Schema(required = true, example = "10.50")
    private double price;

    @Column(name = "category")
    @Enumerated(EnumType.STRING)
    @StringEnumeration(enumClass = Category.class)
    @Schema(required = true, example = "GRAIN")
    private Category category;

    @Column(name = "description")
    @Schema(required = false, example = "Imported rice from China")
    private String description;

    @Column(name = "amount")
    @Min(value = 1, message = "The product needs to have at least 1 item in stock")
    @Schema(required = true, example = "100")
    private int stockAmount;

    @Column(name = "date_created")
    @Transient
    @FutureOrPresent
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(required = false, readOnly = true, example = "Date and time product was registered in the server")
    private LocalDateTime createdAt;

    @Lob
    @Schema(required = false, example = "Base64 of an image")
    private String image;

    @UUID
    @Schema(required = true, readOnly = true, example = "123e4567-e89b-12d3-a456-4266141740000")
    private String uuid;

    public Product() {
        // for jpa
    }

    public Product(String uuid, String name, double price, String category, String description, int stockAmount,
            String image) {
        this.uuid = uuid;
        this.name = name;
        this.description = description;
        this.price = price;
        this.category = Category.valueOf(category);
        this.description = description;
        this.stockAmount = stockAmount;
    }

    public void addUUID() {
        this.uuid = java.util.UUID.randomUUID().toString();
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    public Category getCategory() {
        return category;
    }

    public int getStockAmount() {
        return stockAmount;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public String getUuid() {
        return uuid;
    }

    public String getDescription() {
        return description;
    }
}
@ApplicationScoped
public class ProductService {
    public void validateProduct(@Valid Product product) {

    }
}

The full code I am using is also available at https://github.com/kgoedert/hello-quarkus on the branch practice-3.

Environment (please complete the following information):

  • Output of uname -a or ver: Darwin MacBook-Pro.local 19.4.0 Darwin Kernel Version 19.4.0: Wed Mar 4 22:28:40 PST 2020; root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64
  • Output of java -version: openjdk version "11.0.2" 2019-01-15
    OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
    OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
  • GraalVM version (if different from Java):
  • Quarkus version or git rev: 1.4.2
  • Build tool (ie. output of mvnw --version or gradlew --version): Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
    Maven home: /Users/kelly/.m2/wrapper/dists/apache-maven-3.6.3-bin/1iopthnavndlasol9gbrbg6bf2/apache-maven-3.6.3
    Java version: 11.0.2, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home
    Default locale: en_BR, platform encoding: UTF-8
    OS name: "mac os x", version: "10.15.4", arch: "x86_64", family: "mac"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions