Cannot access com.mongodb.MongoClientSettings - MongoDB Java driver 3.7+ - java

I'm following the guide using MongoDB Java driver 3.7+ versions with Spring boot starter as dependency. And I get the error:
java: cannot access com.mongodb.MongoClientSettings class file for
com.mongodb.MongoClientSettings not found
My POM file looks like:
<?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>bot</groupId>
<artifactId>maven-bot</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.data.version>1.11.0.RELEASE</org.springframework.data.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${org.springframework.data.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>3.10.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>project.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
My class-entity is like:
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Data
#Document(collection = "messages")
public class Messages {
#Id
private Integer user_id;
private String message;
public Messages(String message) {
this.message = message;
}
}
Repository interface for pagination:
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface Repository extends MongoRepository<Messages, String> {
Page<Messages> findByMessages(String message, Pageable pageable);
}
After analyzing and testing, I understand this error appears because of conflict between of dependencies spring boot starter and newer versions of mongodb java driver 3.7+ :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
If I delete this parent dependency then everything is good with MongoDB Java driver. But I need it using annotations for interface and class.
I tried to solve this error writting explicitly Mongoclient:
com.mongodb.client.MongoClient mongoClient = MongoClients.create("mongodb+srv://admin:password#cluster0-ox90k.mongodb.net/test?retryWrites=true");
Instead of:
MongoClient mongoClient = MongoClients.create("mongodb+srv://admin:password#cluster0-ox90k.mongodb.net/test?retryWrites=true");
But it doesn't help me to avoid the issue.
Also I checked via command: mvn dependency:resolve how it resolves all the project dependencies from the repository, I get:
Build success.
Can someone tell me, please, how to solve this problem correctly using newer MongoDB Java drivers? Thanks.

The issue was solved by adding newer version spring boot starter in POM file:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
So, I highly recommend to use last versions of drivers and spring boot starter dependencies. And it should save you from such errors.
But also you can avoid it using older versions of mongodb java drivers.

Related

The import org.springframework.cloud.task.Task cannot be resolved

Trying to write a custom task in Spring Cloud Dataflow which will create a Spring Batch application that implements the Task interface provided by the Spring Cloud Task.
I have try all posible way to impliment this but i am getting this error The import org.springframework.cloud.task.Task cannot be resolved
I have addedd following dependecny iin my project.
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-cloud-task-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cloud-task-example</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-task-core</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>```
And java Code
package com.example.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.task.Task;
import org.springframework.cloud.task.configuration.EnableTask;
#EnableTask
#SpringBootApplication
public class SpringCloudTaskExample implements Task {
public static void main(String[] args) {
SpringApplication.run(SpringCloudTaskExample.class, args);
}
#Override
public void run(String... args) throws Exception {
System.out.println("Running Spring Cloud Task Example!");
}
}
The reason it is not working is that org.springframework.cloud.task.Task does not exist.
Maybe you were trying to implement org.springframework.cloud.task.configuration.TaskConfigurer ?
Please update your Spring Boot Version to 2.7.8
Please replace the following in your pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-task-core</artifactId>
<version>2.4.1</version>
</dependency>
with
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-task</artifactId>
</dependency>

Even though I have annotated with #Repository in my JpaRepository interface still I am getting the error

Currently I'm facing an issue in Autowire configuration between Repository and the service layer.
I'm unable to trace my mistakes.
Repository - OrderRepository
package com.kakashi.orderservice.repository;
import com.kakashi.orderservice.model.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface OrderRepository extends JpaRepository\<Order, Long\> {
}\`
Service - OrderService
package com.kakashi.orderservice.service;
import com.kakashi.orderservice.dto.OrderLineItemsDto;
import com.kakashi.orderservice.dto.OrderRequest;
import com.kakashi.orderservice.model.Order;
import com.kakashi.orderservice.model.OrderLineItems;
import com.kakashi.orderservice.repository.OrderRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.UUID;
#Service
#RequiredArgsConstructor
public class OrderService {
private final OrderRepository orderRepository;
public void placeOrder(OrderRequest orderRequest) {
Order order = new Order();
order.setOrderNumber(UUID.randomUUID().toString());
List<OrderLineItems> orderLineItems = orderRequest.getOrderLineItemsDtoList()
.stream()
.map(orderLineItemsDto -> mapToDto(orderLineItemsDto)).toList();
order.setOrderLineItemsList(orderLineItems);
orderRepository.save(order) ;
}
private OrderLineItems mapToDto(OrderLineItemsDto orderLineItemsDto) {
OrderLineItems orderLineItems = new OrderLineItems();
orderLineItems.setPrice(orderLineItemsDto.getPrice());
orderLineItems.setQuantity(orderLineItemsDto.getQuantity());
orderLineItems.setSkuCode(orderLineItemsDto.getSkuCode());
return orderLineItems;
}
}
`
But I am getting the Below error:#
\`\*\*\*\*\*\*\*\*\*\*\*\*\
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.kakashi.orderservice.service.OrderService required a bean of type 'com.kakashi.orderservice.repository.OrderRepository' that could not be found.
Action:
Consider defining a bean of type 'com.kakashi.orderservice.repository.OrderRepository' in your configuration.
Please do guide me I am using java 17, here I am pasting my application.properties file and pom.xml for your reference
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/order-service
spring.datasource.username=root
spring.datasource.password=""
spring.jpa.hibernate.ddl-auto=update
server.port=8081
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.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kakashi</groupId>
<artifactId>order-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>order-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</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>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>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Previously it was working fine, but I dunno why its misbehaving like this.
Try adding:
#RequiredArgsConstructor(onConstructor = #__(#Autowired))
on service class hope it would work.

can't import #Entity annotation from javax.persistence using Spring Data JPA

So I'm trying to solve this problem for hours and it's really annoying.
I used Spring Initializr to generate a spring boot project: java 19, spring 3.0.0, maven, with mariadb driver, spring web, spring data jpa dependencies.
The problem is that after I create a model class, Student, I can't import #Entity annotation from javax.persistence. All the tutorials and videos I watched, they imported from javax.persistence but I got only jakarta.persistence. Why?
I tried to add myself dependency for javax.persistency but after I create the model with all annotation (#Entity, #Table, #column) the table is not created.
Here Are my files:
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.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>19</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.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</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>
Student.class
package com.example.demo;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
#Entity(name = "student") // CAN'T GET FROM JAVAX.PERSISTENCE, SAME FOR ANNOTATIONS BELOW
public class Student {
#Id
#Column(name="id")
private int id;
#Column(name="name")
private String name;
}
application.properties locates in src/main/resources
spring.datasource.url=jdbc:mariadb://localhost:3307/db1
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
The entry point
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
What am I Doing wrong??
In the newest version of Spring Boot, javax library is replaced by jakarta (see more details here https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0.0-M1-Release-Notes) and here https://spring.io/blog/2022/01/20/spring-boot-3-0-0-m1-is-now-available, so it's ok that you can only find jakarta. You may go ahead and use it.

Spring boot not compiling due to QClass missing

Hi im new to spring boot, ive been trying to integrate querDSL into my project but every time I package it it gives me errors. For example it would say my QClasses would not exist even though they have been properly imported through maven. Ive tried different versions of queryDSL but have come to no solution. I think its an issue with my pom file though I'm not sure as I had followed the tutorial made by Baeldung. Any advice would help, thank you.
My exact error
Error:(11, 8) java: cannot access com.querydsl.core.types.OrderSpecifier
class file for com.querydsl.core.types.OrderSpecifier not found
This is in the Repo class below
My 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>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>rc</groupId>
<artifactId>springboot-mongodb-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mongodb-demo</name>
<description>Demo project for Spring Boot with Mongo Db</description>
<properties>
<java.version>1.8</java.version>
<querydsl.version>4.1.3</querydsl.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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-mongodb</artifactId>
<version>3.6.6</version>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>3.6.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>
org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor
</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
My Repo Layer code
package rc.springbootmongodbdemo;
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.*;
#Repository
public interface HotelRepository extends MongoRepository<Hotel, String>, QuerydslPredicateExecutor<Hotel> {
Hotel findByID(String id);
List<Hotel> findByPricePerNightLessThan(int maxPrice);
#Query(value = "{address.city:?0}")
List<Hotel> findByCity(String city);
}
check this question answered before
Spring Boot+JPA+QueryDSL=OrderSpecifier not found
also go through this
https://www.baeldung.com/intro-to-querydsl
SOLUTION:
I had a repository class that was extending MongoRepository. I was passing in the following
public interface HotelRepository extends MongoRepository<Hotel, String>, QuerydslPredicateExecutor<Hotel>{
after removing:
QuerydslPredicateExecutor
My error went away and my program began running again as it should.
I feel like the querydslpredicateexecutor is maybe an older code format and it wasn't compiling into my new version. Just a theory.

error:java package xxx does not exist despite IDEA being aware of them

I'm leanring springboot with some source code.Recent days,when I begin learning new lessons and want to import the example project I found I fail to import all the packages when I open the example project. Although previous example projects works fine.
It's especially strange that the editor actually can identify those packages - it even offered me the appropriate classes when I manually deleted the import statements, I can see the library in the Project tree under External Libraries,I even can skip to see where the package is through ctrl+click. However, I always get a list of "java: package ... does not exist" upon compilation.How should I solve the problem?Thanks!!
I hava tried following solutions:
reimport in maven settings
invalidate and restart
3.check pom.xml
my pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.neo</groupId>
<artifactId>spring-boot-file-upload</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
some of the wrong import
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
error messages while compiling:
fail to import all the packages
therefore can't resolve all the related symbols
Your project is missing some dependencies. You are not only using Spring Boot, but also Spring Web and Spring MVC.
Try adding this to your pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
You may want to adapt depending on the Spring version you are using.

Categories

Resources