比较来自世界各地的卖家的域名和 IT 服务价格

如何使用查询 DSL 从 MongoDB 在 Spring Boot

我正试图使用​​该请求 DSL 从 MongoDB 在 Spring Boot 我弄错了。 应用程序在不使用库请求的情况下成功工作 DSL 为了 MongoDB. 而且我想使用这个库,因为我想使用更复杂的请求。 代码必须工作,我认为某个地方有一个小错误。

问题是当我点击 Maven package, 不幸的是,我得到了这些错误,我无法在这里发布所有输出:


org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hotelController' defined in file [C:\Users\dgs\IdeaProjects\springboot-mongodb\target\classes\com\dgs\springbootmongodb\controller\HotelController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hotelRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Did not find a query class com.dgs.springbootmongodb.models.QHotel for domain class com.dgs.springbootmongodb.models.Hotel!

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hotelRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Did not find a query class com.dgs.springbootmongodb.models.QHotel for domain class com.dgs.springbootmongodb.models.Hotel!

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hotelController' defined in file [C:\Users\dgs\IdeaProjects\springboot-mongodb\target\classes\com\dgs\springbootmongodb\controller\HotelController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hotelRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Did not find a query class com.dgs.springbootmongodb.models.QHotel for domain class com.dgs.springbootmongodb.models.Hotel!

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hotelRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Did not find a query class com.dgs.springbootmongodb.models.QHotel for domain class com.dgs.springbootmongodb.models.Hotel!

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Did not find a query class com.dgs.springbootmongodb.models.QHotel for domain class com.dgs.springbootmongodb.models.Hotel!

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.26 s <<< FAILURE! - in com.dgs.springbootmongodb.SpringbootMongodbApplicationTests
[ERROR] contextLoads/com.dgs.springbootmongodb.SpringbootMongodbApplicationTests/ Time elapsed: 0.001 s <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hotelController' defined in file [C:\Users\dgs\IdeaProjects\springboot-mongodb\target\classes\com\dgs\springbootmongodb\controller\HotelController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hotelRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Did not find a query class com.dgs.springbootmongodb.models.QHotel for domain class com.dgs.springbootmongodb.models.Hotel!
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hotelRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Did not find a query class com.dgs.springbootmongodb.models.QHotel for domain class com.dgs.springbootmongodb.models.Hotel!
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.repository.support.QuerydslMongoPredicateExecutor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Did not find a query class com.dgs.springbootmongodb.models.QHotel for domain class com.dgs.springbootmongodb.models.Hotel!
Caused by: java.lang.IllegalArgumentException: Did not find a query class com.dgs.springbootmongodb.models.QHotel for domain class com.dgs.springbootmongodb.models.Hotel!
Caused by: java.lang.ClassNotFoundException: com.dgs.springbootmongodb.models.QHotel

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.21.0:test /default-test/ on project springboot-mongodb: There are test failures.


这是酒店预订申请和本准则:

酒店型号:


package com.dgs.springbootmongodb.models;

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;

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

// The Hotel is the aggregate root so this is the entity on which we will apply our annotations

@Document/collection = "Hotels"/
public class Hotel {

@Id
private String id;
private String name;

@Indexed/direction = IndexDirection.ASCENDING/
private int pricePerNight;
private Address address;
private List<review> reviews;

protected Hotel// {
this.reviews = new ArrayList&lt;&gt;//;
}

public Hotel/String name, int pricePerNight, Address address, List<review> reviews/ {
this.name = name;
this.pricePerNight = pricePerNight;
this.address = address;
this.reviews = reviews;
}

public String getId// {
return id;
}

public String getName// {
return name;
}

public int getPricePerNight// {
return pricePerNight;
}

public Address getAddress// {
return address;
}

public List<review> getReviews// {
return reviews;
}
}


界面 HotelRepository:


package com.dgs.springbootmongodb.dao;

import com.dgs.springbootmongodb.models.Hotel;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface HotelRepository extends MongoRepository<hotel, string="">, QuerydslPredicateExecutor<hotel> {

// findBy + PricePerNight /property name/ + LessThan /filter/

List<hotel> findByPricePerNightLessThan/int maxPrice/;

@Query/value = "{address.city:?0}"/
List<hotel> findByCity/String city/;
}


在 HotelController:


package com.dgs.springbootmongodb.controller;

import com.dgs.springbootmongodb.dao.HotelRepository;
import com.dgs.springbootmongodb.models.Hotel;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping/"/hotels"/
public class HotelController {

private HotelRepository hotelRepository;

public HotelController/HotelRepository hotelRepository/ {
this.hotelRepository = hotelRepository;
}

@GetMapping/"/all"/
public List<hotel> getAllHotels// {

List<hotel> hotels = hotelRepository.findAll//;

return hotels;
}

@GetMapping/"/{id}"/
public Optional<hotel> getOneHotel/@PathVariable String id/ {

return hotelRepository.findById/id/;
}

@PostMapping
public Hotel create/@RequestBody Hotel hotel/ {

return hotelRepository.save/hotel/;
}

@PutMapping
public Hotel update/@RequestBody Hotel hotel/ {

return hotelRepository.save/hotel/;
}

@DeleteMapping/"/delete/{id}"/
public List<hotel> delete/@PathVariable String id/ {

hotelRepository.deleteById/id/;

return hotelRepository.findAll//;
}

@GetMapping/"/price/{maxPrice}"/
public List<hotel> getByPricePerNight/@PathVariable int maxPrice/ {

List<hotel> hotels = hotelRepository.findByPricePerNightLessThan/maxPrice/;

return hotels;
}

@GetMapping/"address/{city}"/
public List<hotel> getByCity/@PathVariable String city/ {

List<hotel> hotels = hotelRepository.findByCity/city/;

return hotels;
}
}


它 pom.xml:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="[url=http://maven.apache.org/POM/4.0.0"]http://maven.apache.org/POM/4.0.0"[/url] xmlns:xsi="[url=http://www.w3.org/2001/XMLSchema-instance"]http://www.w3.org/2001/XMLSchema-instance"[/url] xsi:schemalocation="[url=http://maven.apache.org/POM/4.0.0]http://maven.apache.org/POM/4.0.0[/url] [url=http://maven.apache.org/xsd/maven-4.0.0.xsd">]http://maven.apache.org/xsd/ma ... gt%3B[/url]
<modelversion>4.0.0</modelversion>
<groupid>com.dgs</groupid>
<artifactid>springboot-mongodb</artifactid>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-mongodb</name>
<description>Demo project for Spring Boot with Mongo DB</description>
<parent>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-parent</artifactid>
<version>2.0.5.RELEASE</version>
<relativepath></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-mongodb</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-test</artifactid>
<scope>test</scope>
</dependency>
<!-- Add support for Mongo Query DSL -->
<dependency>
<groupid>com.querydsl</groupid>
<artifactid>querydsl-mongodb</artifactid>
<version>4.1.3</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>
<!-- Add plugin for Mongo Query DSL -->
<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.1.3</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>


更新

这是起始课程:


@SpringBootApplication
public class SpringbootMongodbApplication {

public static void main/String[] args/ {
SpringApplication.run/SpringbootMongodbApplication.class, args/;
}
}


这是应用程序的结构:

https://i.stack.imgur.com/YcdtL.jpg
</hotel></hotel></hotel></hotel></hotel></hotel></hotel></hotel></hotel></hotel></hotel></hotel,></review></review></review>
已邀请:

小姐请别说爱

赞同来自:

删除目标文件夹,然后重新开始。

apt-maven-plugin 生成,编译源代码并添加到 classpath. 配置插件的输出目录 target/generated-sources/apt.

运行包阶段 maven 并确保类文件 Q 在该位置的模型数据包内 targets/classes, 和源文件 Q 在位置的模型包内 target/generated-sources/apt.

小明明

赞同来自:

我设法遵循以下内容,强制您的代码工作:

修正案
outputDirectory


<outputdirectory>target/generated-sources</outputdirectory>


使能够
@Autowired


HotelController



@Autowired
private HotelRepository hotelRepository;

要回复问题请先登录注册