我正在尝试使用 java 中的 micornaut 微服务进行一些粗略的操作。但是遇到这个错误,请如果有人可以帮助我。
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.625 s <<< FAILURE! - in micronautDataAcess.MicronautDataAcessTest
[ERROR] micronautDataAcess.MicronautDataAcessTest Time elapsed: 0.623 s <<< ERROR!
java.lang.RuntimeException: Failed to load a service: Unexpected error loading bean definition [micronautDataAcess.$Repository.StudentRepository$Interce
pted$Definition]: failed to access class micronautDataAcess.Repository.StudentRepository$Intercepted from class micronautDataAcess.$Repository.StudentRe
pository$Intercepted$Definition$Reference (micronautDataAcess.Repository.StudentRepository$Intercepted and micronautDataAcess.$Repository.StudentReposit
ory$Intercepted$Definition$Reference are in unnamed module of loader 'app')
Caused by: io.micronaut.context.exceptions.BeanContextException: Unexpected error loading bean definition [micronautDataAcess.$Repository.StudentReposit
ory$Intercepted$Definition]: failed to access class micronautDataAcess.Repository.StudentRepository$Intercepted from class micronautDataAcess.$Repositor
y.StudentRepository$Intercepted$Definition$Reference (micronautDataAcess.Repository.StudentRepository$Intercepted and micronautDataAcess.$Repository.Stu
dentRepository$Intercepted$Definition$Reference are in unnamed module of loader 'app')
Caused by: java.lang.IllegalAccessError: failed to access class micronautDataAcess.Repository.StudentRepository$Intercepted from class micronautDataAces
s.$Repository.StudentRepository$Intercepted$Definition$Reference (micronautDataAcess.Repository.StudentRepository$Intercepted and micronautDataAcess.$Re
pository.StudentRepository$Intercepted$Definition$Reference are in unnamed module of loader 'app')
POM 文件 -
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>micronautDataAcess</groupId>
<artifactId>micronautDataAcess</artifactId>
<version>0.1</version>
<packaging>${packaging}</packaging>
<parent>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-parent</artifactId>
<version>3.1.3</version>
</parent>
<properties>
<packaging>jar</packaging>
<jdk.version>1.8</jdk.version>
<!-- If you are building with JDK 9 or higher, you can uncomment the lines below to set the release version -->
<!-- <release.version>8</release.version> -->
<micronaut.version>3.1.3</micronaut.version>
<exec.mainClass>micronautDataAcess.Application</exec.mainClass>
<micronaut.runtime>netty</micronaut.runtime>
</properties>
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-server-netty</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-runtime</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-jdbc</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.sql</groupId>
<artifactId>micronaut-jdbc-hikari</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-hibernate-jpa</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.micronaut.build</groupId>
<artifactId>micronaut-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- Uncomment to enable incremental compilation -->
<!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->
<annotationProcessorPaths combine.children="append">
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-validation</artifactId>
<version>${micronaut.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amicronaut.processing.group=micronautDataAcess</arg>
<arg>-Amicronaut.processing.module=micronautDataAcess</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
<pluginRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
Controller File -
package micronautDataAcess.Controller;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.*;
import jakarta.inject.Inject;
import micronautDataAcess.Model.StudentModel;
import micronautDataAcess.Services.StudentServiceInterface;
import java.util.Optional;
@Controller("/crud")
public class StudentController {
@Inject
StudentServiceInterface studentServiceInterface;
/*Endpoint for the post method */
@Post(value = "/add")
@Consumes(value = "application/json")
public HttpResponse save(@Body StudentModel crudDemoModel){
StudentModel savedCrudDemoModel = studentServiceInterface.save(crudDemoModel);
return HttpResponse.created(savedCrudDemoModel);
}
/* Endpoint for the finding */
@Get(value = "/find")
@Produces(value = "application/json")
public HttpResponse findById(@NonNull Integer sid){
Optional<StudentModel> foundModelObject = studentServiceInterface.findById(sid);
if(foundModelObject.isPresent()){
return HttpResponse.ok(foundModelObject);
}
return HttpResponse.badRequest("\'Error\':\'Id provied not found in the DB'" );
}
}
Model -
package micronautDataAcess.Model;
import javax.persistence.*;
@Entity
@Table(name = "dummystudent")
public class StudentModel {
/*All the column data*/
/* Student id */
@Id
Integer sid;
/* Student name */
@Column(name = "name", nullable = false, unique = false)
String name;
/* Getter method for sid */
public Integer getSid() {
return sid;
}
/* Setter method for the student id */
public void setSid(int sid) {
this.sid = sid;
}
/* Getter method for student name */
public String getName() {
return name;
}
/* Setter method for student name */
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "StudentModel {" +
"sid" + sid +
"name" + name + "}";
}
}
Repository -
package micronautDataAcess.Repository;
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;
import micronautDataAcess.Model.StudentModel;
import java.util.Optional;
@Repository
/* Curd repository -- will accept the 2 parameters
1. object, char, string etc.
2. Will accept the Integer -- that is no primitive data type.
Here we are passing studentModel - object and Integer
*/
public interface StudentRepository extends CrudRepository<StudentModel, Integer> {
/* Save method will return the student object on save*/
StudentModel save(StudentModel studentModel);
/* Optional - if the entity is not found then it will return null otherwise it will return student
entity containing all the student column data.
*/
Optional<StudentModel> findById(Integer sid);
/* Excepted the void method for the delete operation -- */
void deleteById(Integer sid);
}
服务 - 服务接口
package micronautDataAcess.Services;
import micronautDataAcess.Model.StudentModel;
import java.util.Optional;
public interface StudentServiceInterface {
StudentModel save(StudentModel studentModel);
Optional<StudentModel> findById(Integer sid);
void deleteById(Integer sid);
}
服务实施
package micronautDataAcess.Services;
import io.micronaut.context.annotation.Bean;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import micronautDataAcess.Model.StudentModel;
import micronautDataAcess.Repository.StudentRepository;
import java.util.Optional;
/*Initialization of Bean and Indicating the Singleton instance */
@Singleton
@Bean
public class StudentServiceImpl implements StudentServiceInterface {
/* Injecting the Student repository here in the Service Implementation */
@Inject
StudentRepository studentRepository;
/*Here we are overriding the StudentModel object that save the method.*/
@Override
public StudentModel save(StudentModel studentModel) {
return studentRepository.save(studentModel);
}
/*Here we are overriding the StudentModel -- findbyid method.*/
@Override
public Optional<StudentModel> findById(Integer sid) {
return studentRepository.findById(sid);
}
/*Here we are overriding the StudentModel -- deleteById.*/
@Override
public void deleteById(Integer sid) {
studentRepository.deleteById(sid);
}
}
应用程序.yml 文件
micronaut:
application:
name: micronautDataAcess
datasources:
default:
url: jdbc:mysql://localhost:3306/micronautspractises
user: root
password: ""
driverClassName: com.mysql.cj.jdbc.Driver
dialect: MYSQL
flyway:
datasources:
packages-to-scan: 'StudentModel'
default:
enabled: true
所以我附上了程序中使用的所有依赖项和其他文件。如果有人可以提出更好的方法来解决这个问题。