mvn compile works, but in IntelliJ IDEA compile fails: package org.springframework.web.bind.annotation does not exist - spring

Development environment: IntelliJ IDEA 2020.1.1, Maven 3.6.3, Spring Boot 2.4.0,
below sample code. File 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.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
File TestController.java
package com.example.demo2;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class TestController {
}
Command mvn compile works.
But execute IntelliJ IDEA memnu Build`Recompile` , file 'TestController.java' fails with below error
Error:(3, 47) java: package org.springframework.web.bind.annotation does not exist
I tried removing file *.iml , *.idea but the problem cannot resolve.
I tried mvn clean the problem cannot resolve.
I changed Spring Boot from 2.4.0 to 1.5.9.RELEASE, it works. But why I used version 2.4.0 it did not work?
Update:
Sample project and idea.log at https://pan.baidu.com/s/1EeP7HO_XKHfoVNhcaAbM8w . Extract code: 1tq6

Checking the Maven settings for
Maven home path
and changing its value to Bundled (Maven 3) solved the issue!
Somehow it was set to Use maven wrapper.

Related

Unable to find org.junit.Test in spring boot 2.7.0

I am unable to find org.junit.Test in spring boot 2.7.0
I am using eclipse and when I press ctrl+space after writing #Test, I can see only option for import that is org.junit.jupiter.api.Test as shown in screenshot below.
2.7.0 doesn't support org.junit.Test anymore?
pom.xml below
<?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.7.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.rishi</groupId>
<artifactId>BasicSpringExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>BasicSpringExample</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The old org.junit package is for JUnit 4.x.
The new org.junit.jupiter.api package is for JUnit 5.x.
You should prefer 5.x. JUnit 4.x is old at this point.

Spring Data Source is an unknown property?

I am trying to configure my data source in application properties resource and getting a yellow warning that spring data source is an unknown property. Error is coming up with all pasted line below.
How to properly configure it so i can get rid of this error?
I have checked the pom.xml and mysql and jpa dependencies are also imported correctly.
This is my first project so i don't know how to solve this error. I think if i dont resolve it here it will turn into more severe error.
spring.datasource.url=jdbc:mysql://localhost:3306/projectdb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true
Here's the main class code:
package com.bilal.student.dal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class StudentdalApplication {
public static void main(String[] args) {
SpringApplication.run(StudentdalApplication.class, args);
}
}
Here's the pom.xml file:
<?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.bilal.student.dal</groupId>
<artifactId>studentdal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>studentdal</name>
<description>STUDENT DAL</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</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>
I have resolved the error #Martin Zaragoza. Actually problem was in my project build path. I have deleted the .m2 folder in my C:\Users\hp.m2 and all the error gone. Now i am successfully building my project. Thanks for sticking up with me.

Where is the Spring jar file located?

I'm trying to install Spring Boot using Maven. I use IntelliJ Community Edition (that doesn't have a built-in support for Spring). Therefore, I entered here: http://start.spring.io/, and downloaded an initial Spring Boot project for Maven. The first thing I did is running "install" in order to install the dependencies.
When the installation completed, I looked at the Java file inside src/main/java folder:
package com.example;
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);
}
}
I get errors: "Can't resolve symbol springframework/SpringApplication". I understand that these errors show, as my Spring jar file is not included in the classpath.
My question is where is this file located, so I can add it to the classpath?
My pom.xml file is:
<?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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.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</artifactId>
</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>
I have only one jar file in the target directory:
$ ls target/*.jar
target/demo-0.0.1-SNAPSHOT.jar
I finally found it under:
~/.m2/repository/org/springframework/

Spring Boot with spring-boot-starter-web fails to compile in maven

I am using jdk1.7 and spring boot 1.4.2.Release.I created a brand new Spring boot project from start.spring.io. It compiled fine. I tried to add the below dependency for exposing a REST web service.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
I get the below error:
[ERROR] Failed to execute goal on project demo: Could not resolve
dependencies for project com.example:demo:jar:0.0.1-SNAPSHOT: Failed to
collect dependencies at org.springframework.boot:spring-boot-starter-
web:jar:1.4.2.RELEASE -> org.hibernate:hibernate-validator:jar:5.2.4.Final:
Failed to read artifact descriptor for org.hibernate:hibernate-
validator:jar:5.2.4.Final: Could not transfer artifact
org.jboss.shrinkwrap:shrinkwrap-bom:pom:1.2.2 from/to spring-ext
(http://repo.spring.io/ext-release-local/): repo.spring.io: Unknown host
repo.spring.io -> [Help 1]
It takes the http://repo.spring.io/ext-release-local link which does not have jboss related files.
Please let me know if I am doing something wrong.
Your pom should look like this
<?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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.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-rest</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
after that you need to run it with command mvn spring-boot:run from the project directory
I hope this time it will defenetly work only need to copy and pase this pom content to your pom file
To build spring boot application from scratch you can follow the link
In my case, just adding the repository configuration to the pom.xml didn't work. I had to add the following code into /maven-installation-dir/conf/settings.xml file.
<mirror>
<id>central-proxy</id>
<name>central-proxy</name>
<url>http://corporate-intranet/repo/url</url>
<mirrorOf>central</mirrorOf></mirror>
I hope it helps someone.

POM error: Failure to find org.springframework.boot

I have been trying to setup a spring boot project but am getting an error. I researched and tried various options but no luck so far. Any help would be appreciated.
Project build error: Non-resolvable parent POM: Failure to find org.springframework.boot:spring-boot-starter-parent:pom:1.1.3.RELEASE in http://repo.maven.apache.org/
maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced and
'parent.relativePath' points at no local POM
POM.XML (error in parent tag)
<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.programmingfree</groupId>
<artifactId>spring-data-rest-angular</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Data Rest + AngularJS</name>
<description>This project uses Spring Data Rest and AngularJS to perform CRUD operations against MySql Database.</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.programmingfree.springservice.Application</start-class>
<java.version>1.7</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
There might have been some intermittent Internet issue or something. I was facing the same problem yesterday. Updating the the project (On STS or Eclipse, right click on the project and Maven -> Update project -> tick everything except Offline) after sometime fixed it.
I got a similar exception while maven-build process.
If you are using a proxy network - update settings.xml in your maven folder
../apache-maven-3.2.3\conf/settings.xml
settings.xml content
<proxies>
<proxy>
<id>...</id>
<active>true</active>
<protocol>http</protocol>
<username>....</username>
<password>....</password>
<host>....</host>
<port>....</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
</proxies>
project(right click) -> Maven -> Update project -> tick everything (except OfflinI)
I got the issue resolved.
Check Settings.xml file mapping in Eclipse Window->Project->Maven->User Settings, It should be default xml provided from Maven config folder. You might have configured your own xml file which suites for you project needs.
I tried few things
1. set up the settings.xml file
2. Update maven project option for eclipse didin't solved my issue
3. Run the following command manually , my issue got resolved
mvn dependency:purge-local-repository
Change the version of springboot to the next version or previous version and do update the pom file. It worked for me. I had issue with spring boot 2.1.5 so I changed it to 2.1.4 in the pom file and updated it. It worked.
Delete the repository in .m2 folder, perform Maven install and update the project , it worked for me.
Attaching pom.xml file here.
<?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.5.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springbootproject</groupId>
<artifactId>SpringBootRestServices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootRestServices</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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.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>
This happened because the STS is not able to find the Parent POM location. You can go to Command Prompt and exectute mvn -x to get location of your Global and Local Settings.xml file.Go to Window-> Preferences-> Maven, and then set the locations for both Gloabal and User level with the above command locations. Then right click on the project and Goto Maven-> UpdateProject. This will solve the problem
<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.programmingfree</groupId>
<artifactId>spring-data-rest-angular</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Data Rest + AngularJS</name>
<description>This project uses Spring Data Rest and AngularJS to perform CRUD operations against MySql Database.</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.programmingfree.springservice.Application</start-class>
<java.version>1.7</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I am facing this issue as i am writing this answer. Well seems my office internet settings has restricted downloading content using tools like eclipse. I have faced issues with multiple tools like eclipse, youtube-dl. When I use my mobile internet for my laptop it works fine. That means it has got to do something with the internet settings.

Resources