Skip to content

Instantly share code, notes, and snippets.

@AdamBien
Created August 12, 2022 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamBien/49efb5c357b19ad2bdde82ea026db803 to your computer and use it in GitHub Desktop.
Save AdamBien/49efb5c357b19ad2bdde82ea026db803 to your computer and use it in GitHub Desktop.
102ndAirhacksQ&A.md

Ask questions and see you at September, 5, 8.PM. CET: youtube.com/c/bienadam

Also checkout recent episode:

101 airhacks.tv

Please keep the questions Jakarta EE-stic. Means: as short and as concise as only possible. Feel free to ask several, shorter questions. Upcoming airhacks.tv events are also going to be announced at meetup.com/airhacks

@littlewing88
Copy link

Hi Adam,
I've written some custom javax.validation annotations to automatically produce http 400 Bad Request responses in my jax-rs applications. For example:

@Constraint(validatedBy = DatePatternValidator.class)
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER, ElementType.FIELD })
public @interface Date {
	String message() default "{com.mycompany.validation.Date.message}";
	Class<?>[] groups() default {};
	Class<?>[] payload() default {};
}

public class DatePatternValidator implements ConstraintValidator<Date, String> {

	@Override
	public void initialize(Date constraintAnnotation) {
	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		if (value == null)
			return true;

		try {
			new SimpleDateFormat("yyyy-MM-dd").parse(value);
			return true;
		} catch (ParseException e) {
			// not a valid date
		}
		return false;
	}
}

Now I'm trying to pack these annotations in a jar and make it available as a dependency, but I can't get my head around the ValidationProvider with Configuration bootstrap mechanism.

Here's my effort: I've included within the jar a file named javax.validation.spi.ValidationProvider in META-INF/services/ which provides the fully qualified name of an implementation that I made of ValidationProvider, also included in the jar:

public class DatePatternValidationProvider implements ValidationProvider<DatePatternValidationConfiguration> {

	public DatePatternValidationProvider() {
		System.out.println("DatePatternValidationProvider constructor");
	}

	@Override
	public DatePatternValidationConfiguration createSpecializedConfiguration(BootstrapState state) {
		return DatePatternValidationConfiguration.class.cast(createGenericConfiguration(state));
	}

	@Override
	public Configuration<?> createGenericConfiguration(BootstrapState state) {
		return Validation.byDefaultProvider().configure();
	}

	@Override
	public ValidatorFactory buildValidatorFactory(ConfigurationState configurationState) {
		return Validation.buildDefaultValidatorFactory();
	}
}

public class DatePatternValidationConfiguration implements Configuration<DatePatternValidationConfiguration> {
	// not sure what should go here in the implementation
}

Using the jar as a dependency I can see the ValidationProvider that gets constructed - through the System.out - but then the validation is not triggered and no exception is raised either.

Can you help?
Thank you so much, love all your Q&A content and your blog!

@abdelmuniem
Copy link

abdelmuniem commented Aug 30, 2022

Hi Adam,

I am developing an app using quarkus when I insert java.timeYearMonth using jpa and hibernate, I use to receive an error deserialize JSON value into type: class java.time.YearMonth.

@Entity(name = "Book")
@Table(name = "book")
public class Book {

    @Id
    @GeneratedValue
    private Long id;

    @NaturalId
    private String isbn;

    private String title;

    @Column(name = "published_on", columnDefinition = "date")
    @Convert(converter = YearMonthDateAttributeConverter.class)
    private YearMonth publishedOn;

    // Getters and setters omitted for brevity
}

I want to insert only year and month, local date is working fine.

Your help is much appreciated as usual.

All the best,
Abdelmuniem

@paloliska
Copy link

Hi Adam,
I am trying quarkus-smallrye-fault-tolerance. I have service annotated with @transactional and @Retry with some parameters. I would like to have custom annotation @RetryableTransaction as following:

@Transactional
@Retry
public @interface RetryableTransaction {
}

However with this annotation on service method retry mechanism does not work. Do you know why? Is it part of microprofile specification?

@paloliska
Copy link

Hi Adam, one more question:
Did you ever used http 2 server push?
Is there any easy way to use it without any low level code?

@Endilicam
Copy link

Hi Adam,

in the case of a GET call in a REST service with a parameter (query param) that contains multiple ids, if many ids are sent it can give a 414 error (Request-URI Too Large), searching the internet I have found 3 solutions, but none of them convinced me:

  1. Limit the number of ids sent.
  2. Make a POST and put the parameter in the BODY and return the information in this call.
  3. Make a POST, with an identifier of the call, put the parameter in the BODY and then make another GET call with the identifier where it returns the information.

What do you think, any other solution?

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment