找不到 Spring Boot 2 包

Posted

技术标签:

【中文标题】找不到 Spring Boot 2 包【英文标题】:Spring Boot 2 Package not found 【发布时间】:2019-06-03 16:22:32 【问题描述】:

大家好,我刚开始学习spring。 maven打包时出现以下错误

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project pet-clinic-web: Compilation failure: Compilation failure:
[ERROR] /C:/Users/user/Dropbox/yyy/spring/frameworkGuru/my-pet-clinic/pet-clinic-web/src/main/java/xxx/xxx/mypetclinic/controllers/OwnerController.java:[3,45] package xxx.xxx.mypetclinic.services does not exist
[ERROR] /C:/Users/user/Dropbox/yyy/spring/frameworkGuru/my-pet-clinic/pet-clinic-web/src/main/java/xxx/xxx/mypetclinic/controllers/OwnerController.java:[12,19] cannot find symbol
[ERROR] symbol:   class OwnerService
[ERROR] location: class xxx.xxx.mypetclinic.controllers.OwnerController
[ERROR] /C:/Users/user/Dropbox/yyy/spring/frameworkGuru/my-pet-clinic/pet-clinic-web/src/main/java/xxx/xxx/mypetclinic/controllers/OwnerController.java:[14,28] cannot find symbol
[ERROR] symbol:   class OwnerService
[ERROR] location: class xxx.xxx.mypetclinic.controllers.OwnerController
[ERROR] -> [Help 1]

我有 2 个模块 dataweb,我正在尝试从 OwnerController(web->controllers) 访问 OwnerService(data->services)。我在 web pom 中添加了 data 模块作为依赖项

Intellij 不显示错误,我什至可以列出所有运行良好的所有者,但我尝试打包并显示错误。这是一些项目文件。

我尝试使用较旧的 Spring Boot 版本 2.0.3,错误消失了,但在 2.1.1 中仍然存在。

项目 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>xxx.xxx</groupId>
    <artifactId>my-pet-clinic</artifactId>
    <version>0.0.2-SNAPSHOT</version>


    <modules>
        <module>pet-clinic-data</module>
        <module>pet-clinic-web</module>
    </modules>

    <packaging>pom</packaging>

    <name>my-pet-clinic</name>
    <description>My recode of spring pet clinic</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>9</java.version>
        <start-class>xxx.xxx.mypetclinic.MyPetClinicApplication</start-class>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <!--<version>2.5.3</version>-->
                <configuration>
                    <goals>install</goals>
                    <autoVersionSubmodules>true</autoVersionSubmodules>
                </configuration>
            </plugin>
        </plugins>
    </build>



</project>

数据点

<parent>
    <artifactId>my-pet-clinic</artifactId>
    <groupId>xxx.xxx</groupId>
    <version>0.0.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>pet-clinic-data</artifactId>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </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>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>

    </plugins>
</build>

网络 pom

    <parent>
        <artifactId>my-pet-clinic</artifactId>
        <groupId>xxx.xxx</groupId>
        <version>0.0.2-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>pet-clinic-web</artifactId>

    <properties>
        <!-- Web dependencies -->
        <webjars-bootstrap.version>3.3.6</webjars-bootstrap.version>
        <webjars-jquery-ui.version>1.11.4</webjars-jquery-ui.version>
        <webjars-jquery.version>2.2.4</webjars-jquery.version>
        <wro4j.version>1.8.0</wro4j.version>
    </properties>

    <dependencies>
        <dependency>
            <artifactId>pet-clinic-data</artifactId>
            <groupId>xxx.xxx</groupId>
            <version>0.0.2-SNAPSHOT</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>$webjars-jquery.version</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery-ui</artifactId>
            <version>$webjars-jquery-ui.version</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>$webjars-bootstrap.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>



</project>

所有者控制器

package xxx.xxx.mypetclinic.controllers;

import xxx.xxx.mypetclinic.services.OwnerService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("/owners")
@Controller
public class OwnerController 

    private final OwnerService ownerService;

    public OwnerController(OwnerService ownerService) 
        this.ownerService = ownerService;
    

    @RequestMapping("","/","/index", "/index.html")
    public String listOwners(Model model)

        model.addAttribute("owners",ownerService.findAll());

        return "owners/index";
    

业主服务

package xxx.xxx.mypetclinic.services;

import xxx.xxx.mypetclinic.model.Owner;

public interface OwnerService extends CrudService<Owner, Long>

    Owner findByLastName(String lastName);


和 OwnerServiceMap 实现

package xxx.xxx.mypetclinic.services.map;

import xxx.xxx.mypetclinic.model.Owner;
import xxx.xxx.mypetclinic.services.OwnerService;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class OwnerServiceMap extends AbstractMapService<Owner, Long> implements OwnerService 

    @Override
    public Set<Owner> findAll() 
        return super.findAll();
    

    @Override
    public Owner findById(Long id) 
        return super.findById(id);
    

    @Override
    public Owner save(Owner object) 
        return super.save(object);
    

    @Override
    public void delete(Owner object) 
        super.delete(object);
    

    @Override
    public void deleteById(Long id) 
        super.deleteById(id);
    

    @Override
    public Owner findByLastName(String lastName) 
        return null;
    

Code-repo

【问题讨论】:

没有找到OwnerService,所以应该在你的Controller中调用OwnerServiceMap而不是OwnerService。 我试过了,它显示同样的错误,它找不到整个包。 throws package xxx.xxx.mypetclinic.services does not exist 在其他地方需要的 Web 项目中没有类。 如果不是问题,如果你能分享回购链接会更好 github.com/brutumi/my-pet-clinic 我回滚到 2.0.3 它现在已经消失但在 2.1.1 仍然存在 【参考方案1】:

我认为你错过了一个


<dependencyManagement>
   <dependencies>
      <dependency>
        <!-- Import dependency management from Spring Boot -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>SPRING-BOOT-VERSION-HERE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
   </dependencies>
</dependencyManagement>

来自您的pom.xml

更多详情见:https://***.com/a/21318359/2891426

【讨论】:

以上是关于找不到 Spring Boot 2 包的主要内容,如果未能解决你的问题,请参考以下文章

Jar包找不到文件路径问题

Maven json包找不到解决办法

找不到 Spring Boot 2 包

android studio导jar包找不到类的解决方法

在Python中出现这样的这样,第三方依赖包找不到,怎么解决

restlet 2.3.5 org.restlet包导入eclipse出现的com.sun.net.httpserver类包找不到问题