incompatible types: java.util.Optional cannot

2019-09-13 16:15发布

问题:

I'm getting an incompatible types error message at the following line in my spring-boot application:

RequestDO request = requestRepository.findOne(requestModel.getRequestId());
Users employee = usersRepository.findOne(request.getUsers().getId());

I have not experienced this error in this context before. My app has been running successfully since its first production build about one month ago.

Build failure:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project tmt: Compilation failure: Compilation failure:
[ERROR] /C:/Users/martin/Documents/Web Projects/home.office/src/main/java/xyz/app/controller/UserController.java:[45,45] incompatible types: java.util.Optional<xyz.app.entity.Users> cannot be converted to xyz.app.entity.Users
[ERROR] /C:/Users/martin/Documents/Web Projects/home.office/src/main/java/xyz/app/controller/RequestController.java:[139,54] incompatible types: java.util.Optional<xyz.app.entity.RequestDO> cannot be converted to xyz.app.entity.RequestDO
[ERROR] /C:/Users/martin/Documents/Web Projects/home.office/src/main/java/xyz/app/controller/RequestController.java:[140,49] incompatible types: java.util.app<xyz.app.entity.Users> cannot be converted to xyz.app.entity.Users
[ERROR] /C:/Users/martin/Documents/Web Projects/home.office/src/main/java/xyz/app/controller/RequestController.java:[155,54] incompatible types: java.util.Optional<v.app.entity.RequestDO> cannot be converted to xyz.app.entity.RequestDO
[ERROR] /C:/Users/martin/Documents/Web Projects/home.office/src/main/java/xyz/app/controller/RequestController.java:[157,49] incompatible types: java.util.Optional<xyz.app.entity.Users> cannot be converted to xyz.app.entity.Users

I have made no code changes since I've last successfully re-built my project. This leads me to believe there has been a recent change in the springframework Maven dependency that has caused my code to break. Is it possible to test this by reverting to an older snapshot in my pom.xml? Also, what could be causing the error?

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>xyz.app</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>app</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--<dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0.1</version>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-ldap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.directory.server</groupId>
            <artifactId>apacheds-server-jndi</artifactId>
            <version>1.5.5</version>
        </dependency>
        <dependency>
            <groupId>it.ozimov</groupId>
            <artifactId>spring-boot-email-core</artifactId>
            <version>0.4.0</version>
        </dependency>
        <dependency>
            <groupId>it.ozimov</groupId>
            <artifactId>spring-boot-freemarker-email</artifactId>
            <version>0.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

回答1:

I had a similar issue using 2.0.0.BUILD-SNAPSHOT, so unless you're experimenting with some low-level spring boot features, change the version to 1.5.2.RELEASE