elegos
elegos
JCHJava Community | Help. Code. Learn.
Created by elegos on 11/18/2024 in #java-help
VSCode and RedHat Java: wrong JDK for gradle tasks
Dear community, I've installed OpenJDK 21. I have configured JAVA_HOME and JDK_HOME accordingly, setup the JDK Runtime under Project Settings (JavaSE-21, which correctly points to the Eclipse Adoptium's jdk 21 folder). The build.gradle file is configured to run with Java version 21 (see following config). I have no clue why, when I start the Gradle tasks via the Gradle integration tab, it uses the java.exe included in the redhat's extension (java v. 17). Running gradlew from the shell, it works correctly (i.e. .\gradlew.bat init) Thanks for any help 🙂
plugins {
id 'fabric-loom' version '1.8-SNAPSHOT'
id 'maven-publish'
}

# ...

tasks.withType(JavaCompile).configureEach {
it.options.release = 21
}

java {
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
// if it is present.
// If you remove this line, sources will not be generated.
withSourcesJar()

sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
plugins {
id 'fabric-loom' version '1.8-SNAPSHOT'
id 'maven-publish'
}

# ...

tasks.withType(JavaCompile).configureEach {
it.options.release = 21
}

java {
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
// if it is present.
// If you remove this line, sources will not be generated.
withSourcesJar()

sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
Gradle's output:
> Configure project :
Fabric Loom: 1.8.12
<-------------> 0% WAITING
> IDLE
> IDLE
> IDLE
> IDLE

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'my-project-name'.
> Failed to setup Minecraft, java.lang.IllegalStateException: Minecraft 1.21.3 requires Java 21 but Gradle is using 17

* Try:
> Run with --stacktrace option to get the stack trace.
Could not execute build using connection to Gradle distribution 'https://services.gradle.org/distributions/gradle-8.10.2-bin.zip'.
> Configure project :
Fabric Loom: 1.8.12
<-------------> 0% WAITING
> IDLE
> IDLE
> IDLE
> IDLE

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'my-project-name'.
> Failed to setup Minecraft, java.lang.IllegalStateException: Minecraft 1.21.3 requires Java 21 but Gradle is using 17

* Try:
> Run with --stacktrace option to get the stack trace.
Could not execute build using connection to Gradle distribution 'https://services.gradle.org/distributions/gradle-8.10.2-bin.zip'.
11 replies
JCHJava Community | Help. Code. Learn.
Created by elegos on 4/30/2023 in #java-help
Spring boot: unable to instantiate ConstraintValidator: Unsatisfied dependency
I've got the following DTO with its own custom constraint annotation and relative validator:
// ...

@Data
public class MyResource {
private String foo;
private Integer bar;

@Constraint(validatedBy = MyValidator.class)
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyResConstraint {
public String message() default "";
public Class<?>[] groups() default {};
public Class<? extends Payload>[] payload() default {};
}

public class MyValidator implements ConstraintValidator<MyResConstraint, Object> {
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
System.out.println(value);
System.out.println(context);

return false;
}

}
}
// ...

@Data
public class MyResource {
private String foo;
private Integer bar;

@Constraint(validatedBy = MyValidator.class)
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyResConstraint {
public String message() default "";
public Class<?>[] groups() default {};
public Class<? extends Payload>[] payload() default {};
}

public class MyValidator implements ConstraintValidator<MyResConstraint, Object> {
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
System.out.println(value);
System.out.println(context);

return false;
}

}
}
I've applied to a @Validated RestController class method, as follows:
@PostMapping(path = "/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> whatever(
@RequestBody @ModelAttribute @MyResConstraint
MyResource res
) {
@PostMapping(path = "/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> whatever(
@RequestBody @ModelAttribute @MyResConstraint
MyResource res
) {
When I try to call the POST mapping, I get the following error:
[Request processing failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.demo.MyResource$MyValidator': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.MyResource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}]
[Request processing failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.demo.MyResource$MyValidator': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.MyResource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}]
What am I doing wrong? Thanks!
4 replies
JCHJava Community | Help. Code. Learn.
Created by elegos on 4/30/2023 in #java-help
Spring Boot MVC: validate a multipart/form-data RequestBody
Hello! I have the need to fill a form with some meta data and a file. The meta data required fields depends on the media "type" that you're selecting from a drop-down. I've thus created a custom validator, associated to a custom annotation and put all on the PostMapping action annotating the parameter with @Valid @MyCustomAnnotation (see below). The thing is that the validator is not being called for a reason beyond my knowledge. May you please help me understand what I'm doing wrong? Thank you very much 🙂 The DTO + validator:
@Data
public class CreateMediaRequestResource {
private MediaType mediaType;
// TODO: various attributes depending on mediaType
private MultipartFile media;

@Documented
@Constraint(validatedBy = CreateMediaRequestValidator.class)
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface CreateMediaRequestValidation {
public String message() default "";
public Class<?>[] groups() default {};
public Class<? extends Payload>[] payload() default {};
}

@Component
public class CreateMediaRequestValidator implements ConstraintValidator<CreateMediaRequestValidation, CreateMediaRequestResource> {
@Override
public boolean isValid(CreateMediaRequestResource value, ConstraintValidatorContext context) {
System.out.println(value);
System.out.println(context);

return false;
}}
}
@Data
public class CreateMediaRequestResource {
private MediaType mediaType;
// TODO: various attributes depending on mediaType
private MultipartFile media;

@Documented
@Constraint(validatedBy = CreateMediaRequestValidator.class)
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface CreateMediaRequestValidation {
public String message() default "";
public Class<?>[] groups() default {};
public Class<? extends Payload>[] payload() default {};
}

@Component
public class CreateMediaRequestValidator implements ConstraintValidator<CreateMediaRequestValidation, CreateMediaRequestResource> {
@Override
public boolean isValid(CreateMediaRequestResource value, ConstraintValidatorContext context) {
System.out.println(value);
System.out.println(context);

return false;
}}
}
The controller's method:
// ...
public ResponseEntity<MediaMetadataResource> createMedia(
@RequestBody @ModelAttribute
@Valid @CreateMediaRequestValidation
CreateMediaRequestResource data
) {
// this won't fail (and the validator won't print)
// ...
public ResponseEntity<MediaMetadataResource> createMedia(
@RequestBody @ModelAttribute
@Valid @CreateMediaRequestValidation
CreateMediaRequestResource data
) {
// this won't fail (and the validator won't print)
I've shortened the code as much as possible to stay in the chars limit, please ask me anything which might be related. Thank you very much!
9 replies