I'm trying to use the Spring Framework in my project, but I have a problem with the import. I'm working with Gradle for builds, React JS for the front end and Java for the back end.
The weird thing is that I can use classes as RowMapper and JdbcTemplate (I can read and write in my database using these classes) even if VS Code is telling me that my import cannot be resolved.
When I build with Gradle (gradle build then gradle bootrun in my command prompt) it works too.
It's pretty boring working with errors that are not supposed to be present. Can someone help me ?
Personally I think it's an error in my build.gradle file or a configuration in my VS Code, but I'm not sure.
Here's my build.gradle with my dependencies and my repositories :
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
apply plugin: 'java'
apply plugin: 'org.liquibase.gradle'
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter:2.0.6.RELEASE")
// Use MySQL Connector-J
runtime 'mysql:mysql-connector-java:8.0.12'
compile("org.springframework:spring-jdbc:3.2.4.RELEASE")
testCompile('org.junit.jupiter:junit-jupiter-api:5.3.1')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.3.1')
compile ('commons-dbcp:commons-dbcp:1.4')
liquibaseRuntime 'org.liquibase:liquibase-core:3.6.1'
liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:2.0.1'
liquibaseRuntime 'mysql:mysql-connector-java:8.0.12'
}
Here's a class of my project where the error occurs :
package be.heh.petclinic.component.pet;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import be.heh.petclinic.domain.Pet;
public class JdbcPetDao {
private DataSource dataSource;
public JdbcPetDao(DataSource dataSource){
this.dataSource = dataSource;
}
public List<Pet> getPets() {
JdbcTemplate select = new JdbcTemplate(dataSource);
return select.query("SELECT name, birth_date, owner_id, type_id FROM pets", new PetRowMapper());
}
}
org.springframework is underlined and therefore JdbcTemplate as well.
I was able to resolve this issue with a project of mine by forcing vscode to reload my project configuration. I followed the steps from here: https://github.com/Microsoft/vscode-java-debug/blob/master/Troubleshooting.md#build-failed-do-you-want-to-continue
Edit: The wording of the command has changed, but the above linked doc has not been updated. I updated my answer as mentioned in the comments.
It was this command that fixed vscode for me:
Open your Maven pom.xml file or Gradle build.gradle file, then run VS Code command "Java: Reload project" to force the language server to update the project configuration/classpath.
I recently helped a friend with a similar issue, and for her we had to delete the .classpath file from the project directory before doing the Update project configuration command.
It can also be found by right-clicking the build file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With