我收到的错误消息:
java.lang.Error: Unresolved compilation problems:
Optional cannot be resolved to a type
The method findById(Integer) from the type CrudRepository<Employee,Integer> refers to the missing type Optional
package Spring.RestProject.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import Spring.RestProject.entity.Employee;
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
}
employeService.java
package Spring.RestProject.service;
import java.util.List;
import Spring.RestProject.entity.Employee;
public interface EmployeeService {
Employee findById(int id);
List<Employee> findAll();
}
employeeserviceImpl.java
package Spring.RestProject.service;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import Spring.RestProject.entity.Employee;
import Spring.RestProject.repository.EmployeeRepository;
@Service
public class EmployeeServiceImpl implements EmployeeService {
EmployeeRepository employeeRepository;
@Autowired
public EmployeeServiceImpl (EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
@Override
public Employee findById(int id) {
Optional<Employee> result = employeeRepository.findById(id);
Employee theEmployee;
if(result.isPresent()) {
theEmployee = result.get();
} else {
return null;
}
return theEmployee;
}
@Override
public List<Employee> findAll() {
return employeeRepository.findAll();
}
}
Controller.java
package Spring.RestProject.restcontroller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import Spring.RestProject.entity.Employee;
import Spring.RestProject.service.EmployeeService;
@RestController
@RequestMapping("/api")
public class Controller {
EmployeeService employeeService;
@Autowired
public Controller (EmployeeService employeeService) {
this.employeeService = employeeService;
}
@GetMapping("/employees/{id}")
public Employee findById(@PathVariable int id) {
return employeeService.findById(id);
}
@GetMapping("/employees")
public List<Employee> findAll () {
return employeeService.findAll();
}
}
POM.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>Spring</groupId>
<artifactId>RestProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>RestProject</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>23</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I安装了JDK 23,现在代码有效。我认为我什至没有正确安装JDK。感谢您的帮助。