相关文章推荐
飘逸的水龙头  ·  Validations for Enum ...·  昨天    · 
飘逸的水龙头  ·  SQL 将 expression ...·  5 天前    · 
飘逸的水龙头  ·  CircleCI Developer ...·  10 月前    · 
飘逸的水龙头  ·  Nacos Docker 快速开始·  1 年前    · 
飘逸的水龙头  ·  DataGrid component ...·  1 年前    · 
豪情万千的眼镜  ·  智能指针reset()·  55 分钟前    · 
任性的筷子  ·  std::packaged_task::re ...·  56 分钟前    · 
爱喝酒的火车  ·  QueryMetricByPage - ...·  1小时前    · 
一身肌肉的大葱  ·  Solving the ...·  1小时前    · 
玩篮球的火锅  ·  日常点滴 - 一抹浅笑 ·  1小时前    · 
奔放的包子  ·  How to use OpenAPI in ...·  3 小时前    · 
刚毅的刺猬  ·  I want to dock my ...·  3 小时前    · 

Azure Container Apps is a fully managed serverless container service that enables you to build and deploy modern, cloud-native Java applications and microservices at scale. It offers a simplified developer experience while providing the flexibility and portability of containers.

Of course, Azure Container Apps has really solid support for our ecosystem, from a number of build options, managed Java components, native metrics, dynamic logger, and quite a bit more.

To learn more about Java features on Azure Container Apps, you can get started over on the documentation page .

And, you can also ask questions and leave feedback on the Azure Container Apps GitHub page .

Partner – Codium – NPI EA (site = Main Site)
announcement - icon

Get non-trivial analysis (and trivial, too!) suggested right inside your IDE or Git platform so you can code smart, create more value, and stay confident when you push.

Get CodiumAI for free and become part of a community of over 280,000 developers who are already experiencing improved and quicker coding.

Write code that works the way you meant it to:

CodiumAI. Meaningful Code Tests for Busy Devs

Partner – DBSchema – NPI EA (tag = Spring Data JPA)
announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema .

The way it does all of that is by using a design model , a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

Take a look at DBSchema

Partner – Aegik AB – NPI EA (cat=JPA)
announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only , so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server's performance, with most of the profiling work done separately - so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server , hit the record button, and you'll have results within minutes:

out the Profiler

eBook – Guide Junit – NPI EA (cat=Java)
announcement - icon

A quick guide to materially improve your tests with Junit 5:

>> The Junit 5 handbook
eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – RwS – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

REST With Spring (new)

Course – LS – NPI EA (cat=Spring)
announcement - icon

Get started with Spring and Spring Boot, through the reference Learn Spring course:

>> LEARN SPRING

Partner – Codium – NPI EA (cat = Testing)
announcement - icon

Get non-trivial analysis (and trivial, too!) suggested right inside your IDE or Git platform so you can code smart, create more value, and stay confident when you push.

Get CodiumAI for free and become part of a community of over 280,000 developers who are already experiencing improved and quicker coding.

Write code that works the way you meant it to:

CodiumAI. Meaningful Code Tests for Busy Devs

Partner – Bellsoft – NPI EA (cat = Spring)
announcement - icon

Looking for the ideal Linux distro for running modern Spring apps in the cloud?

Meet Alpaquita Linux : lightweight, secure, and powerful enough to handle heavy workloads.

This distro is specifically designed for running Java apps . It builds upon Alpine and features significant enhancements to excel in high-density container environments while meeting enterprise-grade security standards.

Specifically, the container image size is ~30% smaller than standard options, and it consumes up to 30% less RAM:

Alpaquita Containers now.

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth , to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project .

You can explore the course here:

>> Learn Spring Security

Partner – DBSchema – NPI EA (tag = SQL)
announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema .

The way it does all of that is by using a design model , a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

Take a look at DBSchema

Partner – Aegik AB – NPI EA (tag = SQL)
announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only , so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server's performance, with most of the profiling work done separately - so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server , hit the record button, and you'll have results within minutes:

out the Profiler

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot .

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

1. Introduction

In the tutorial Java Bean Validation Basics , we saw how we can apply javax validations using JSR 380 to various types. And in the tutorial Spring MVC Custom Validation , we saw how to create custom validations.

In this next tutorial, we’ll focus on building validations for enums using custom annotations.

2. Validating Enums

Unfortunately, most standard annotations can not be applied to enums .

For example, when applying the @Pattern annotation to an enum we receive an error like this one with Hibernate Validator:

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 
 'javax.validation.constraints.Pattern' validating type 'com.baeldung.javaxval.enums.demo.CustomerType'. 
 Check configuration for 'customerTypeMatchesPattern'

Actually, the only standard annotations which can be applied to enum’s are @NotNull and @Null.

3. Validating the Pattern of an Enum

Let’s start by defining an annotation to validate the pattern of an enum:

@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = EnumNamePatternValidator.class)
public @interface EnumNamePattern {
    String regexp();
    String message() default "must match \"{regexp}\"";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};

Now we can simply add this new annotation using a regular expression to our CustomerType enum:

@EnumNamePattern(regexp = "NEW|DEFAULT")
private CustomerType customerType;

As we can see, the annotation does not actually contain the validation logic. Therefore, we need to provide a ConstraintValidator:

public class EnumNamePatternValidator implements ConstraintValidator<EnumNamePattern, Enum<?>> {
    private Pattern pattern;
    @Override
    public void initialize(EnumNamePattern annotation) {
        try {
            pattern = Pattern.compile(annotation.regexp());
        } catch (PatternSyntaxException e) {
            throw new IllegalArgumentException("Given regex is invalid", e);
    @Override
    public boolean isValid(Enum<?> value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        Matcher m = pattern.matcher(value.name());
        return m.matches();

In this example, the implementation is very similar to the standard @Pattern validator. However, this time, we match the name of the enum.

4. Validating a Subset of an Enum

Matching an enum with a regular expression is not type-safe. Instead, it makes more sense to compare with the actual values of an enum.

However, because of the limitations of annotations, such an annotation cannot be made generic. This is because arguments for annotations can only be concrete values of a specific enum, not instances of the enum parent class.

Let’s see how to create a specific subset validation annotation for our CustomerType enum:

@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = CustomerTypeSubSetValidator.class)
public @interface CustomerTypeSubset {
    CustomerType[] anyOf();
    String message() default "must be any of {anyOf}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};

This annotation can then be applied to enums of the type CustomerType:

@CustomerTypeSubset(anyOf = {CustomerType.NEW, CustomerType.OLD})
private CustomerType customerType;

Next, we need to define the CustomerTypeSubSetValidator to check whether the list of given enum values contains the current one:

public class CustomerTypeSubSetValidator implements ConstraintValidator<CustomerTypeSubset, CustomerType> {
    private CustomerType[] subset;
    @Override
    public void initialize(CustomerTypeSubset constraint) {
        this.subset = constraint.anyOf();
    @Override
    public boolean isValid(CustomerType value, ConstraintValidatorContext context) {
        return value == null || Arrays.asList(subset).contains(value);

While the annotation has to be specific for a certain enum, we can of course share code between different validators.

5. Validating That a String Matches a Value of an Enum

Instead of validating an enum to match a String, we could also do the opposite. For this, we can create an annotation that checks if the String is valid for a specific enum.

@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = ValueOfEnumValidator.class)
public @interface ValueOfEnum {
    Class<? extends Enum<?>> enumClass();
    String message() default "must be any of enum {enumClass}";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};

This annotation can be added to a String field and we can pass any enum class.

@ValueOfEnum(enumClass = CustomerType.class)
private String customerTypeString;

Let’s define the ValueOfEnumValidator to check whether the String (or any CharSequence) is contained in the enum:

public class ValueOfEnumValidator implements ConstraintValidator<ValueOfEnum, CharSequence> {
    private List<String> acceptedValues;
    @Override
    public void initialize(ValueOfEnum annotation) {
        acceptedValues = Stream.of(annotation.enumClass().getEnumConstants())
                .map(Enum::name)
                .collect(Collectors.toList());
    @Override
    public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        return acceptedValues.contains(value.toString());

This validation can especially be useful when working with JSON objects. Because the following exception appears, when mapping an incorrect value from a JSON object to an enum:

Cannot deserialize value of type CustomerType from String value 'UNDEFINED': value not one
 of declared Enum instance names: [...]

We can, of course, handle this exception. However, this does not allow us to report all violations at once.

Instead of mapping the value to an enum, we can map it to a String. We’d then use our validator to check whether it matches any of the enum values.

6. Bringing It All Together

We can now validate beans using any of our new validations. Most importantly, all of our validations accept null values. Consequently, we can also combine it with the annotation @NotNull:

public class Customer {
    @ValueOfEnum(enumClass = CustomerType.class)
    private String customerTypeString;
    @NotNull
    @CustomerTypeSubset(anyOf = {CustomerType.NEW, CustomerType.OLD})
    private CustomerType customerTypeOfSubset;
    @EnumNamePattern(regexp = "NEW|DEFAULT")
    private CustomerType customerTypeMatchesPattern;
    // constructor, getters etc.

In the next section, we’ll see how we can test our new annotations.

7. Testing Our Javax Validations for Enums

In order to test our validators, we’ll set up a validator, which supports our newly defined annotations. We’ll the Customer bean for all our tests.

First, we want to make sure that a valid Customer instance does not cause any violations:

@Test 
public void whenAllAcceptable_thenShouldNotGiveConstraintViolations() { 
    Customer customer = new Customer(); 
    customer.setCustomerTypeOfSubset(CustomerType.NEW); 
    Set violations = validator.validate(customer); 
    assertThat(violations).isEmpty(); 

Second, we want our new annotations to support and accept null values. We only expect a single violation. This should be reported on customerTypeOfSubset by the @NotNull annotation:

@Test
public void whenAllNull_thenOnlyNotNullShouldGiveConstraintViolations() {
    Customer customer = new Customer();
    Set<ConstraintViolation> violations = validator.validate(customer);
    assertThat(violations.size()).isEqualTo(1);
    assertThat(violations)
      .anyMatch(havingPropertyPath("customerTypeOfSubset")
      .and(havingMessage("must not be null")));

Finally, we validate our validators to report violations, when the input is not valid:

@Test
public void whenAllInvalid_thenViolationsShouldBeReported() {
    Customer customer = new Customer();
    customer.setCustomerTypeString("invalid");
    customer.setCustomerTypeOfSubset(CustomerType.DEFAULT);
    customer.setCustomerTypeMatchesPattern(CustomerType.OLD);
    Set<ConstraintViolation> violations = validator.validate(customer);
    assertThat(violations.size()).isEqualTo(3);
    assertThat(violations)
      .anyMatch(havingPropertyPath("customerTypeString")
      .and(havingMessage("must be any of enum class com.baeldung.javaxval.enums.demo.CustomerType")));
    assertThat(violations)
      .anyMatch(havingPropertyPath("customerTypeOfSubset")
      .and(havingMessage("must be any of [NEW, OLD]")));
    assertThat(violations)
      .anyMatch(havingPropertyPath("customerTypeMatchesPattern")
      .and(havingMessage("must match \"NEW|DEFAULT\"")));

8. Conclusion

In this tutorial, we covered three options to validate enums using custom annotations and validators.

First, we learned how to validate the name of an enum using a regular expression.

Second, we discussed a validation for a subset of values of a specific enum. We also explained why we cannot build a generic annotation to do this.

Finally, we also looked at how to build a validator for strings. In order to check whether a String conforms to a particular value of a given enum.

As always, the full source code of the article is available over on Github.

Partner – Bellsoft – NPI EA (cat = Spring)
announcement - icon

Looking for the ideal Linux distro for running modern Spring apps in the cloud?

Meet Alpaquita Linux: lightweight, secure, and powerful enough to handle heavy workloads.

This distro is specifically designed for running Java apps. It builds upon Alpine and features significant enhancements to excel in high-density container environments while meeting enterprise-grade security standards.

Specifically, the container image size is ~30% smaller than standard options, and it consumes up to 30% less RAM:

Alpaquita Containers now.

Partner – Bellsoft – NPI EA (cat = Spring)
announcement - icon

Just published a new writeup on how to run a standard Java/Boot application as a Docker container, using the Liberica JDK on top of Alpaquita Linux:

Spring Boot Application on Liberica Runtime Container.

Partner – Aegik AB – NPI EA (tag = SQL)
announcement - icon

Slow MySQL query performance is all too common. Of course it is.

The Jet Profiler was built entirely for MySQL, so it's fine-tuned for it and does advanced everything with relaly minimal impact and no server changes.

out the Profiler

Partner – Codium – NPI EA (cat = Testing)
announcement - icon

Explore the secure, reliable, and high-performance Test Execution Cloud built for scale. Right in your IDE:

CodiumAI. Meaningful Code Tests for Busy Devs

Basically, write code that works the way you meant it to.

Course – LS (cat=Java)
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

res – REST with Spring (eBook) (everywhere)
Download the Guide
eBook – Video Security – NPI EA (cat=Spring Security)
access to the video
eBook – Persistence – NPI EA (cat=Persistence)
 
推荐文章