构造函数抛出异常;嵌套异常为java.lang.IllegalArgumentException:找不到域类的查询类

时间:2018-11-16 21:51:40

标签: java spring maven spring-boot querydsl

我在sts中创建了一个maven项目,并且试图为该项目创建一个war文件。我也为我的班级配置了querydsl。当我运行maven install时,一类给我一个错误响应。错误:在文件[C:\ Users \ vince \ Documents \ workspace-spring-tool-suite-4-4.0.1.RELEASE \ project \ target \ test-classes \ com \ project中定义的名称为'productBrandController'的bean创建时出错\ project \ StoreBased \ ProductBrandController.class]:通过构造函数参数0表示的不满意依赖项;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'productBrandRepository'的bean时出错:调用init方法失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]:构造函数引发了异常;嵌套的异常是java.lang.IllegalArgumentException:找不到域类project.StoreBased.ProductBrand的查询类project.QProductBrand!我需要有关我的pom文件的帮助(不确定javax.xml.bind是否可能是罪魁祸首),也不确定我的输出目录是否正确(我已经尝试使用java代替输出注释)。非常感谢您的帮助!

这是课程

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.IndexDirection;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;


@Document(collection = "ProductBrand")
public class ProductBrand {

    @Id
    private String id;
    private ProductsBasic basic;
    @Indexed(direction = IndexDirection.ASCENDING)
    private String name;
    @Indexed(direction = IndexDirection.ASCENDING)
    private String units;
    @Indexed(direction = IndexDirection.ASCENDING)
    private double price;
    @Indexed(direction = IndexDirection.ASCENDING)
    private double price100g;
    private String manufacturer;
    private List<Manufacturer> manufacturerIndex;
    private boolean organic;
    private List<Organic> organicIndex;
    private String category;
    private List<Category> categoryIndex;
    private String subCategory;
    private List<SubCategory> subCategoryIndex;
    private boolean seasonal;
    private List<Seasonal> seasonalIndex;
    private boolean frozenGoods;
    private List<FrozenGoods> frozenGoodsIndex;
    private boolean refrigeratedGoods;
    private List<RefrigeratedGoods> refrigeratedGoodsIndex;
    private boolean available;
    private List<Available> availableIndex;
    @Indexed(direction = IndexDirection.ASCENDING)
    private String store;
    private List<Store> storeIndex;

    // public ProductBrand() {
    // super();
    // }

    public ProductBrand() {
        this.setAvailableIndex(new ArrayList<>());
        this.setCategoryIndex(new ArrayList<>());
        this.setSubCategoryIndex(new ArrayList<>());
        this.setFrozenGoodsIndex(new ArrayList<>());
        this.setManufacturerIndex(new ArrayList<>());
        this.setOrganicIndex(new ArrayList<>());
        this.setRefrigeratedGoodsIndex(new ArrayList<>());
        this.setSeasonalIndex(new ArrayList<>());
        this.setStoreIndex(new ArrayList<>());
    }

    public ProductBrand(String id, ProductsBasic basic, String name, String units, double price, double price100g,
        String manufacturer, List<Manufacturer> manufacturerIndex, boolean organic, List<Organic> organicIndex,
        String category, List<Category> categoryIndex, String subCategory, List<SubCategory> subCategoryIndex,
        boolean seasonal, List<Seasonal> seasonalIndex, boolean frozenGoods, List<FrozenGoods> frozenGoodsIndex,
        boolean refrigeratedGoods, List<RefrigeratedGoods> refrigeratedGoodsIndex, boolean available,
        List<Available> availableIndex, String store, List<Store> storeIndex) {
        super();
        this.id = id;
        this.basic = basic;
        this.name = name;
        this.units = units;
        this.price = price;
        this.price100g = price100g;
        this.manufacturer = manufacturer;
        this.setManufacturerIndex(manufacturerIndex);
        this.organic = organic;
        this.setOrganicIndex(organicIndex);
        this.category = category;
        this.setCategoryIndex(categoryIndex);
        this.subCategory = subCategory;
        this.setSubCategoryIndex(subCategoryIndex);
        this.seasonal = seasonal;
        this.setSeasonalIndex(seasonalIndex);
        this.frozenGoods = frozenGoods;
        this.setFrozenGoodsIndex(frozenGoodsIndex);
        this.refrigeratedGoods = refrigeratedGoods;
        this.setRefrigeratedGoodsIndex(refrigeratedGoodsIndex);
        this.available = available;
        this.setAvailableIndex(availableIndex);
        this.store = store;
        this.setStoreIndex(storeIndex);
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public boolean isOrganic() {
        return organic;
    }

    public void setOrganic(boolean organic) {
        this.organic = organic;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getSubCategory() {
        return subCategory;
    }

    public void setSubCategory(String subCategory) {
        this.subCategory = subCategory;
    }

    public boolean isSeasonal() {
        return seasonal;
    }

    public void setSeasonal(boolean seasonal) {
        this.seasonal = seasonal;
    }

    public boolean isFrozenGoods() {
        return frozenGoods;
    }

    public void setFrozenGoods(boolean frozenGoods) {
        this.frozenGoods = frozenGoods;
    }

    public boolean isRefrigeratedGoods() {
        return refrigeratedGoods;
    }

    public void setRefrigeratedGoods(boolean refrigeratedGoods) {
        this.refrigeratedGoods = refrigeratedGoods;
    }

    public boolean isAvailable() {
        return available;
    }

    public void setAvailable(boolean available) {
        this.available = available;
    }

    public void addProduktBrand(ProductBrand productBrand) {
        ProductBrand.add(productBrand);
    }

    public static void add(ProductBrand productBrand) {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public ProductsBasic getBasic() {
        return basic;
    }

    public void setBasic(ProductsBasic basic) {
        this.basic = basic;
    }

    public String getStore() {
        return store;
    }

    public void setStore(String store) {
        this.store = store;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUnits() {
        return units;
    }

    public void setUnits(String units) {
        this.units = units;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getPrice100g() {
        return price100g;
    }

    public void setPrice100g(double price100g) {
        this.price100g = price100g;
    }

    public List<Manufacturer> getManufacturerIndex() {
        return manufacturerIndex;
    }

    public void setManufacturerIndex(List<Manufacturer> manufacturerIndex) {
        this.manufacturerIndex = manufacturerIndex;
    }

    public List<Organic> getOrganicIndex() {
        return organicIndex;
    }

    public void setOrganicIndex(List<Organic> organicIndex) {
        this.organicIndex = organicIndex;
    }

    public List<Category> getCategoryIndex() {
        return categoryIndex;
    }

    public void setCategoryIndex(List<Category> categoryIndex) {
        this.categoryIndex = categoryIndex;
    }

    public List<SubCategory> getSubCategoryIndex() {
        return subCategoryIndex;
    }

    public void setSubCategoryIndex(List<SubCategory> subCategoryIndex) {
        this.subCategoryIndex = subCategoryIndex;
    }

    public List<Seasonal> getSeasonalIndex() {
        return seasonalIndex;
    }

    public void setSeasonalIndex(List<Seasonal> seasonalIndex) {
        this.seasonalIndex = seasonalIndex;
    }

    public List<FrozenGoods> getFrozenGoodsIndex() {
        return frozenGoodsIndex;
    }

    public void setFrozenGoodsIndex(List<FrozenGoods> frozenGoodsIndex) {
        this.frozenGoodsIndex = frozenGoodsIndex;
    }

    public List<RefrigeratedGoods> getRefrigeratedGoodsIndex() {
        return refrigeratedGoodsIndex;
    }

    public void setRefrigeratedGoodsIndex(List<RefrigeratedGoods> refrigeratedGoodsIndex) {
        this.refrigeratedGoodsIndex = refrigeratedGoodsIndex;
    }

    public List<Available> getAvailableIndex() {
        return availableIndex;
    }

    public void setAvailableIndex(List<Available> availableIndex) {
        this.availableIndex = availableIndex;
    }

    public List<Store> getStoreIndex() {
        return storeIndex;
    }

    public void setStoreIndex(List<Store> storeIndex) {
        this.storeIndex = storeIndex;
    }

}

这是该类的控制器

import java.util.List;
import java.util.Optional;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import project.QProductBrand;
import com.querydsl.core.types.dsl.BooleanExpression;


@RestController
@RequestMapping("/productbrand")
public class ProductBrandController {
    private ProductBrandRepository productBrandRepository;

    public ProductBrandController(ProductBrandRepository productBrandRepository) {
        this.productBrandRepository = productBrandRepository;
    }

    @GetMapping("/all")
    public List<ProductBrand> getAll() {
        List<ProductBrand> product = this.productBrandRepository.findAll();

        return product;
    }

    @GetMapping("/productbrand/{name}")
    public List<ProductBrand> getByName(@PathVariable("name") String name) {
        List<ProductBrand> product = this.productBrandRepository.findByName(name);

        return product;
    }

    @PutMapping
    public void insert(@RequestBody ProductBrand productBrand) {
        this.productBrandRepository.insert(productBrand);
    }

    @PostMapping
    public void update(@RequestBody ProductBrand productBrand) {
        this.productBrandRepository.save(productBrand);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") ProductBrand id) {
        this.productBrandRepository.delete(id);
    }

    @GetMapping("/{id}")
    public Optional<ProductBrand> getById(@PathVariable("id") String id) {
        Optional<ProductBrand> brand = this.productBrandRepository.findById(id);
        return brand;

    }

    @GetMapping("/productBrand/{organic}")
    public List<ProductBrand> getByOrganic(@PathVariable("organic") boolean organic) {
        List<ProductBrand> brand = this.productBrandRepository.findByOrganic(organic);

        return brand;
    }

    @GetMapping("/productBrand/{manufacturer}")
    public List<ProductBrand> getByManufacturer(@PathVariable("manufacturer") String manufacturer) {
        List<ProductBrand> brand = this.productBrandRepository.findByManufacturer(manufacturer);

        return brand;
    }

    @GetMapping("/productBrand/{category}")
    public List<ProductBrand> getByCategory(@PathVariable("category") String category) {
        List<ProductBrand> brand = this.productBrandRepository.findByCategory(category);

        return brand;
    }

    @GetMapping("/productBrand/{seasonal}")
    public List<ProductBrand> getBySeasonal(@PathVariable("seasonal") boolean seasonal) {
        List<ProductBrand> brand = this.productBrandRepository.findBySeasonal(seasonal);

        return brand;
    }

    @GetMapping("/productBrand/{frozenGoods}")
    public List<ProductBrand> getByFrozenGoods(@PathVariable("frozenGoods") boolean frozenGoods) {
        List<ProductBrand> brand = this.productBrandRepository.findByFrozenGoods(frozenGoods);

        return brand;
    }

    @GetMapping("/productBrand/{refrigeratedGoods}")
    public List<ProductBrand> getByRefrigeratedGoods(@PathVariable("refrigeratedGoods") boolean refrigeratedGoods) {
        List<ProductBrand> brand = this.productBrandRepository.findByRefrigeratedGoods(refrigeratedGoods);

        return brand;
    }

    @GetMapping("/productBrand/{available}")
    public List<ProductBrand> getByAvailable(@PathVariable("available") boolean available) {
        List<ProductBrand> brand = this.productBrandRepository.findByAvailable(available);

        return brand;
    }


    @GetMapping("/recommended")
    public List<ProductBrand> getRecommended() {
        final boolean isOrganic = true;
        final boolean isSeasonal = true;

        QProductBrand qBrand = new QProductBrand("ProductBrand");

        BooleanExpression filterByOrganic = (qBrand.organicIndex).any().organic.gt(isOrganic);

        BooleanExpression filterBySeasonal = (qBrand.seasonalIndex).any().seasonal.gt(isSeasonal);

        List<ProductBrand> brand = (List<ProductBrand>) this.productBrandRepository
                .findAll(filterByOrganic.and(filterBySeasonal));

        return brand;
    }

}

这是我的存储库

import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductBrandRepository extends MongoRepository<ProductBrand, String>, QuerydslPredicateExecutor<ProductBrand> {

    List<ProductBrand> findByName(String name);

    List<ProductBrand> findByOrganic(boolean organic);

    List<ProductBrand> findBySeasonal(boolean seasonal);

    List<ProductBrand> findByFrozenGoods(boolean frozenGoods);

    List<ProductBrand> findByRefrigeratedGoods(boolean refrigeratedGoods);

    List<ProductBrand> findByAvailable(boolean available);

    List<ProductBrand> findByCategory(String category);

    List<ProductBrand> findByManufacturer(String manufacturer);

}

这是我的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>com.BETAapp</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>project</name>
    <description>demo</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</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-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-groovy-templates</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mustache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</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-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.4.0-b180725.0427</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mongodb.morphia/morphia -->
        <dependency>
            <groupId>org.mongodb.morphia</groupId>
            <artifactId>morphia</artifactId>
            <version>0.107</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.querydsl/querydsl-apt -->
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-mongodb</artifactId>
            <version>4.2.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mongodb</groupId>
                    <artifactId>mongo-java-driver</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <version>1.1.3</version>
                <dependencies>
                    <dependency>
                        <groupId>com.querydsl</groupId>
                        <artifactId>querydsl-apt</artifactId>
                        <version>4.2.1</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/generated-sources/annotations</outputDirectory>
                            <processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor>
                            <logOnlyOnError>true</logOnlyOnError>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

测试集:project.StoreBased.ApplicationTests

运行测试:1,失败:0,错误:1,跳过:0,经过的时间:11.036 s <<<故障! -在project.StoreBased.ApplicationTests中 contextLoads(project.StoreBased.ApplicationTests)经过的时间:0.002 s <<<错误! java.lang.IllegalStateException:无法加载ApplicationContext 由以下原因引起:org.springframework.beans.factory.UnsatisfiedDependencyException:在文件[C:\ Users \ vince \ Documents \ workspace-spring-tool-suite-4-4.0.1.RELEASE \中创建名称为'productBrandController'的bean时出错project(18.11.2018)\ target \ test-classes \ com \ project \ project \ StoreBased \ ProductBrandController.class]:通过构造函数参数0表示的不满意依赖项;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为'productBrandRepository'的bean时出错:调用init方法失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]:构造函数引发了异常;嵌套的异常是java.lang.IllegalArgumentException:找不到域类project.StoreBased.ProductBrand的查询类project.StoreBased.QProductBrand! 由以下原因引起:org.springframework.beans.factory.BeanCreationException:创建名称为'productBrandRepository'的bean时出错:调用init方法失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]:构造函数引发了异常;嵌套的异常是java.lang.IllegalArgumentException:找不到域类project.project.StoreBased.ProductBrand的查询类project.StoreBased.QProductBrand! 引起原因:org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]:构造函数引发了异常;嵌套的异常是java.lang.IllegalArgumentException:找不到域类project.StoreBased.ProductBrand的查询类project.StoreBased.QProductBrand! 原因:java.lang.IllegalArgumentException:找不到域类project.StoreBased.ProductBrand的查询类project.StoreBased.QProductBrand! 原因:java.lang.ClassNotFoundException:project.StoreBased.QProductBrand

1 个答案:

答案 0 :(得分:0)

此问题的原因:

Your Query Classes are generated in different package (i.e. inside /annotations) and 
you are trying to find it from different package.

解决方案:

更改outputDirectory路径(即用于生成'Q'前缀类的标签位置

<outputDirectory>target/generated-sources/annotations</outputDirectory>

<outputDirectory>target/generated-sources/apt</outputDirectory>

删除目标文件夹并执行

  

mvn全新安装