How to “ignore”: jakarta.validation.UnexpectedTypeException: No validator could be found for constraint ‘jakarta.validation.constraints.Pattern’

I read that the usage of @jakarta.validation.constraints.Pattern should be used on return types of Charset. The issue that I am facing is that in my case, I am using generated java files from some gradle tasks that puts the Patterns on all getter methods, no matter the return type. At time of generation it is difficult to determine whether the return type is String or not, so the Pattern annotations are set on all the getter methods.

Now because we are working with a lot of dynamic data, we want to validate these inputs based on a flag validate.

When validate is set to true, I try to trigger validator.validate(view) which then throws jakarta.validation.UnexpectedTypeException:

ExampleService.kt:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import jakarta.validation.Validator
import com.example.generated.DEXPDF
// more imports
@Service
class ExampleService(
// ...
private val validator: Validator
)
// ... some code
private fun assertRequestValid(view: DEXPDF) {
val violations = validator.validate(view) // throws UnexpectedTypeException
if (violations.isNotEmpty()) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, violations.joinToString(", "))
}
}
// ... some more code
</code>
<code>import jakarta.validation.Validator import com.example.generated.DEXPDF // more imports @Service class ExampleService( // ... private val validator: Validator ) // ... some code private fun assertRequestValid(view: DEXPDF) { val violations = validator.validate(view) // throws UnexpectedTypeException if (violations.isNotEmpty()) { throw ResponseStatusException(HttpStatus.BAD_REQUEST, violations.joinToString(", ")) } } // ... some more code </code>
import jakarta.validation.Validator
import com.example.generated.DEXPDF
// more imports

@Service
class ExampleService(
    // ...
    private val validator: Validator
)

// ... some code 

private fun assertRequestValid(view: DEXPDF) {
        val violations = validator.validate(view) // throws UnexpectedTypeException
        if (violations.isNotEmpty()) {
            throw ResponseStatusException(HttpStatus.BAD_REQUEST, violations.joinToString(", "))
        }
    }

// ... some more code

The generated java looks something like that, this is just a small chunk of the file:

DEXPDF.java:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// some code...
@Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
@Size(min = 1, max = 7)
@JsonProperty("messageVersion")
@Pattern(regexp = "F\.[1-9][0-9]?\.[1-9]?[0-9]")
public String getMessageVersion() {
return messageVersion;
}
/**
* Sets the value of the messageVersion property.
*
* @param value
* allowed object is
* {@link String }
*
*/
@Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
public void setMessageVersion(String value) {
this.messageVersion = value;
}
/**
* Gets the value of the messageSender property.
*
* @return
* possible object is
* {@link DEXPDF.MessageSender }
*
*/
@Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
@Size(min = 1, max = 17)
@JsonProperty("messageSender")
@Pattern(regexp = "[A-Z]{2}[!-~]{1,15}")
public DEXPDF.MessageSender getMessageSender() {
return messageSender;
}
/**
* Sets the value of the messageSender property.
*
* @param value
* allowed object is
* {@link DEXPDF.MessageSender }
*
*/
@Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
public void setMessageSender(DEXPDF.MessageSender value) {
this.messageSender = value;
}
/**
* Gets the value of the messageRecipient property.
*
* @return
* possible object is
* {@link DEXPDF.MessageRecipient }
*
*/
@Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
@JsonProperty("messageRecipient")
@Pattern(regexp = "DE0[01][0-9]{4}")
public DEXPDF.MessageRecipient getMessageRecipient() { // throws exception due to unexpected type
return messageRecipient;
}
// more code...
</code>
<code>// some code... @Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00") @Size(min = 1, max = 7) @JsonProperty("messageVersion") @Pattern(regexp = "F\.[1-9][0-9]?\.[1-9]?[0-9]") public String getMessageVersion() { return messageVersion; } /** * Sets the value of the messageVersion property. * * @param value * allowed object is * {@link String } * */ @Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00") public void setMessageVersion(String value) { this.messageVersion = value; } /** * Gets the value of the messageSender property. * * @return * possible object is * {@link DEXPDF.MessageSender } * */ @Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00") @Size(min = 1, max = 17) @JsonProperty("messageSender") @Pattern(regexp = "[A-Z]{2}[!-~]{1,15}") public DEXPDF.MessageSender getMessageSender() { return messageSender; } /** * Sets the value of the messageSender property. * * @param value * allowed object is * {@link DEXPDF.MessageSender } * */ @Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00") public void setMessageSender(DEXPDF.MessageSender value) { this.messageSender = value; } /** * Gets the value of the messageRecipient property. * * @return * possible object is * {@link DEXPDF.MessageRecipient } * */ @Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00") @JsonProperty("messageRecipient") @Pattern(regexp = "DE0[01][0-9]{4}") public DEXPDF.MessageRecipient getMessageRecipient() { // throws exception due to unexpected type return messageRecipient; } // more code... </code>
// some code...

@Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
    @Size(min = 1, max = 7)
    @JsonProperty("messageVersion")
    @Pattern(regexp = "F\.[1-9][0-9]?\.[1-9]?[0-9]")
    public String getMessageVersion() {
        return messageVersion;
    }

    /**
     * Sets the value of the messageVersion property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    @Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
    public void setMessageVersion(String value) {
        this.messageVersion = value;
    }

    /**
     * Gets the value of the messageSender property.
     * 
     * @return
     *     possible object is
     *     {@link DEXPDF.MessageSender }
     *     
     */
    @Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
    @Size(min = 1, max = 17)
    @JsonProperty("messageSender")
    @Pattern(regexp = "[A-Z]{2}[!-~]{1,15}")
    public DEXPDF.MessageSender getMessageSender() {
        return messageSender;
    }

    /**
     * Sets the value of the messageSender property.
     * 
     * @param value
     *     allowed object is
     *     {@link DEXPDF.MessageSender }
     *     
     */
    @Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
    public void setMessageSender(DEXPDF.MessageSender value) {
        this.messageSender = value;
    }

    /**
     * Gets the value of the messageRecipient property.
     * 
     * @return
     *     possible object is
     *     {@link DEXPDF.MessageRecipient }
     *     
     */
    @Generated(value = "com.sun.tools.xjc.Driver", comments = "JAXB RI v4.0.0", date = "2024-06-11T15:18:18+02:00")
    @JsonProperty("messageRecipient")
    @Pattern(regexp = "DE0[01][0-9]{4}")
    public DEXPDF.MessageRecipient getMessageRecipient() { // throws exception due to unexpected type
        return messageRecipient;
    }

// more code...

How can I tell the validator to just ignore the unexpected types instead of throwing an exception? If this is not configurable, is there an alternative?

I tried to surround it with a try catch which of course did not help at all. I also tried to create a custom factory by implementing the jakarta.validation.Validator but at that point I did not know how to continue, there wasn’t a “straightforward” way to configure that.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật