#SpringBootApplication not recognized when using spring-boot-starter-parent version 1.5.9.RELEASE - spring-boot

from SPRING STS I've created a fresh spring boot project starter , I've added "WEB" feature, and then finished creating the project
for some reason the import
import org.springframework.boot.autoconfigure.SpringBootApplication;
is not recognized (I get -" The import org.springframework.boot.autoconfigure.SpringBootApplication cannot be resolved")
and thus the #SpringBootApplication annotation is also unrecognized
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Newtest1sdfsdfApplication {
public static void main(String[] args) {
SpringApplication.run(Newtest1sdfsdfApplication.class, args);
}
}
NOTE: I'm using the project that was automatically created
if i change the project version of spring-boot-starter-parent (in pom.xml)
from 1.5.9 to 1.5.8 - all is well again. setting back to 1.5.9 shows the error
meaning changing from :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
to
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Any ideas what's the issue here?

I ended up :
deleting two sub-folders from inside the .m2 folder
The sub-folders were : org and com
and then -> from eclipse/STS : Right-click the Project name -> MAVEN -> UPDATE PROJECT

Related

Spring 2.2.1 Build Error Missing artifact com.microsoft.sqlserver:sqljdbc4:jar:4.0

For the part of spring 4.1.7 to spring boot 2.2.1,i have added spring-boot-starter-parent in my parent project as given below.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/>
</parent>
when I tried to build my parent and child project, it getting an error
Missing artifact com.microsoft.sqlserver:sqljdbc4:jar:4.0
.m2 folder structure
Am also refer the following link
stackoverflow
How to solve this issue.
I tied to delete C:/Users/Administrator/.m2/repository/com/microsoft/sqlserver/sqljdbc4/4.0 folder then rebuild the project .Now its working fine for me.

Spring Boot 2: Unable to start tomcat

I am new to spring boot.. Getting started by referring to the link: https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-first-application.html
After running the project its not starting the tomcat server.. Getting the error as [ERROR] error reading /home/rahul/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.31/tomcat-embed-core-8.5.31.jar; invalid LOC header (bad signature)
Not able to address this issue.. Any help would be appreciated..
Thanks a lot in advance..
My code is like below,
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wocs</groupId>
<artifactId>REST</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Example.java
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
#RestController
#EnableAutoConfiguration
public class Example {
#RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
Seems like you have a corrupted file. Try clean your maven cache with rm -rf ~/.m2/repository and run again
Looks like you have a corrupted jar that is under your default MAVEN folder. You can delete the specific jar file that is causing this issue and trying the follow should help!
Take a Maven update by right clicking on your Project -> then select Maven and then click on 'Update Project'. You've got to wait until these dependencies are downloaded.
Run target Clean Maven (Project -> Run as -> Clean Maven)
Then finally Install Maven (Project -> Run as -> Install Maven)
These steps should resolve your invalid LOC header error.
Here is what worked for me on Windows 10:
Deleted this jar file from the shell command line
mvn spring-boot:run
After that, the jar was downloaded with no error and all is running fine.
and all is running fine

ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

I have written a spring batch application using Spring boot. When I am trying to run that application using command line and classpath on my local system it is running fine. However, when I tried to run it on linux server it is giving me following exception
Unable to start web server; nested exception is
org.springframework.context.ApplicationContextException:
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
Below is the way I am running it:
java -cp jarFileName.jar; lib\* -Dlogging.level.org.springframework=DEBUG -Dspring.profiles.active=dev -Dspring.batch.job.names=abcBatchJob com.aa.bb.StartSpringBatch > somelogs.log
Case 1:
#SpringBootApplication annotation missing in your spring boot starter class.
Case 2:
For non-web applications, disable web application type in the properties file.
In application.properties:
spring.main.web-application-type=none
If you use application.yml then add:
spring:
main:
web-application-type: none
For web applications, extends *SpringBootServletInitializer* in the main class.
#SpringBootApplication
public class YourAppliationName extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(YourAppliationName.class, args);
}
}
Case 3:
If you use spring-boot-starter-webflux then also add spring-boot-starter-web as dependency.
Probably you missing #SpringBootApplication in your spring boot starter class.
#SpringBootApplication
public class LoginSecurityAppApplication {
public static void main(String[] args) {
SpringApplication.run(LoginSecurityAppApplication.class, args);
}
}
The solution is:
I explicitly set the below property to none in application.yml file.
spring:
main:
web-application-type: none
My solution had to do with a bad dependency. I had:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
In my pom and I had to comment out the exclusion to get it working. It must look for this tomcat package for some reason.
In my case the issue resolved on commenting the tomcat dependencies exclusion from spring-boot-starte-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- <exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions> -->
</dependency>
You probably use this in your project:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
in which case you'll have to also add:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
and the magic happens :)
PS: that's because Spring will use by default web-MVC instead of web-flux when both are available
Adding following bean worked for me.
#Bean
public ServletWebServerFactory servletWebServerFactory() {
return new TomcatServletWebServerFactory();
}
I was running non web spring application using SpringApplication.run(MyApplication.class, args); without #SpringBootApplication annotation.
Annotate class public static void main with, for example: #SpringBootApplication
To convert an Spring boot wen application to standalone:
Either configure application.properties:
spring.main.web-application-type=none
Or Update application context with NONE web context.
ApplicationContext ctx = new SpringApplicationBuilder(MigrationRunner.class)
.web(WebApplicationType.NONE).run(args);
Using application context, you can get your beans:
myBean bean = (MyBean) ctx.getBean("myBean", MyBean.class);
bean.call_a_method(..);
I had this problem during migration to Spring Boot. I've found a advice to remove dependencies and it helped. So, I removed dependency for jsp-api Project had. Also, servlet-api dependency has to be removed as well.
compileOnly group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.2'
As for me, I removed the provided scope in tomcat dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope> // remove this scope
</dependency>
I did right click on my project in IntelliJ IDEA then Maven -> Reload project, problem solved.
In case you're using IntelliJ and this is happening to you (like it did to my noob-self), ensure the Run setting has Spring Boot Application and NOT plain Application.
I was getting same error while using tomcat-jasper newer version
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>10.0.6</version>
</dependency>
I replaced with the stable older version it worked fine for me.
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.46</version>
</dependency>
Apart from the possible solutions in other answers, it is also possible that somehow Maven dependency corruption has occurred on your machine. I was facing the same error on trying to run my (Web) Spring boot application, and it got resolved by running the following -
mvn dependency:purge-local-repository -DreResolve=true
followed by
mvn package
I came onto this solution looking into another issue where Eclipse wouldn't let me run the main application class from the IDE, due to a different error, similar to one on this SO thread -> The type org.springframework.context.ConfigurableApplicationContext cannot be resolved. It is indirectly referenced from required .class files
Similar to the solution of making sure org.springframework.boot:spring-boot-starter-tomcat was installed, I was missing org.eclipse.jetty:jetty-server from my build.gradle
org.springframework.boot:spring-boot-starter-web needs a server be it Tomcat, Jetty or something else - it will compile but not run without one.
I wanted to run the WAR type spring boot application, and when I was trying to run the app as spring boot application I was getting above error. So declaring the web application type in application.properties has worked for me.
spring.main.web-application-type=none
Possible web application type:
NONE - the application should not run as a web application and should not start an embedded web server.
REACTIVE - the application should run as a reactive web application and should start an embedded reactive web server.
SERVLET - the application should run as a servlet-based web application and should start an embedded servlet web server.
In my case, the problem was I didn't had a Tomcat server separately installed in my eclipse. I assumed my Springboot will start the server automatically within itself.
Since my main class extends SpringBootServletInitializer and override configure method, I definitely need a Tomcat server installed in my IDE.
To install, first download Apachce Tomcat (version 9 in my case) and create server using Servers tab.
After installation, run the main class on server.
Run As -> Run on Server
I was trying to create a web application with spring boot and I got the same error.
After inspecting I found that I was missing a dependency. So, be sure to add following dependency to your pom.xml file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Missing dependency could be cause of this issue
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
I encountered this problem when attempint to run my web application as a fat jar rather than from within my IDE (IntelliJ).
This is what worked for me. Simply adding a default profile to the application.properties file.
spring.profiles.active=default
You don't have to use default if you have already set up other specific profiles (dev/test/prod). But if you haven't this is necessary to run the application as a fat jar.
Upgrading spring-boot-starter-parent in pom.xml to the latest version fixed it for me.
In my case, I was using an TOMCAT 8 and updating to TOMCAT 9 fixed it:
<modelVersion>4.0.0</modelVersion>
<groupId>spring-boot-app</groupId>
<artifactId>spring-boot-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<tomcat.version>9.0.37</tomcat.version>
</properties>
Related issues:
https://github.com/spring-projects/spring-boot/issues?q=missing+ServletWebServerFactory+bean
https://github.com/spring-projects/spring-boot/issues/22013 - Spring Boot app as a module
https://github.com/spring-projects/spring-boot/issues/19141 - Application fails to load when main class extends a base class annotated with #SpringBootApplication when spring-boot-starter-web is included as a dependency
My problem was the same as that in the original question, only that I was running via Eclipse and not cmd. Tried all the solutions listed, but didn't work. The final working solution for me, however, was while running via cmd (or can be run similarly via Eclipse). Used a modified command appended with spring config from cmd:
start java -Xms512m -Xmx1024m <and the usual parameters as needed, like PrintGC etc> -Dspring.config.location=<propertiesfiles> -jar <jar>
I guess my issue was the spring configurations not being loaded correctly.
In my case, the gretty plugin (3.0.6) was still active. Gretty somehow influences the embedded tomcat dependency. Removing gretty fixed the error
Just comment the provided like below

Deploying SpringBoot (rest services) war via IntelliJ on Tomcat gives 404's

Starting a small SpringBoot application by running the application (directly), the REST service localhost:8080/xyz/a gives a correct JSON result.
Via IntelliJ I configured a Tomcat server. I added this WAR with a context root of '/contextroot'. So I expected the URL REST to be localhost:8080/contextroot/xyz/a. This keeps on getting 404 errors.
Can you help me getting the right configuration or URL?
Is there a way to see which URL's are mapped from the controller to the URL's? Or: how can I solve these mapping issues more easily (from Tomcat)?
In the Maven Pom.xml I have:
<groupId>nl.xyz</groupId>
<artifactId>contextroot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>contextroot</name>
<packaging>war</packaging>
The within is also contextroot.
#Jonhib - thank you for helping.
The problem was the way I deployed a SpringBoot (jar) application as a WAR.
Take these 3 steps to change a SpringBoot standard application into a deployable (old fashioned) WAR. This is needed because I deploy it together with a Angular4 application.
Step 1 - Change the standard SpringBoot application:
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application .class);
}
public static void main(String[] args) {
SpringApplication.run(Application .class, args);
}
}
Change the target of the building process from 'jar' into a 'war'.
<packaging>war</packaging>
Add a new dependency into the pom.xml maven file:
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
...
</dependencies>

spring cloud config client not loading configuration from config server

I am following this link:
http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_client_side_usage
I tested this again and again and not seeing Spring cloud client is loading configuration from cloud server, please help to see where is the error:
POM:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Application:
#Configuration
#EnableAutoConfiguration
#RestController
public class ConfigclientApplication {
#Value("${spring.cloud.config.uri}")
String url;
#Value("${production.host}")
String host;
#RequestMapping("/")
public String home() {
return "Host is => " + this.host ;
}
public static void main(String[] args) {
SpringApplication.run(ConfigclientApplication.class, args);
}
}
bootstrap.properties:
spring.cloud.config.uri=http://localhost:8888
The config server is good:
http://localhost:8888/spirent/default
{
"name": "spirent",
"profiles": [
"default"
],
"label": "master",
"propertySources": [
{
"name": "classpath:/spirent.yml",
"source": {
"production.host": "server1",
"production.port": 9999,
"production.value1": 12345,
"test.host": "server2.com",
"test.port": 4444,
"test.value": "hello123"
}
}
]
}
now http://localhost:8080/ can not be started at all.
Error creating bean with name 'configclientApplication'
It seemed the auto inject of #Value can not find the production.host environment value.
How can I read the configuration in client once loaded from config server?
Thanks for your help.
If you are using 2020.0.0 version of spring cloud than you need to this dependency in your maven dependencies to enable bootstrap, which is desabled by default in 2020.0.0.
Breaking changes in 2020.0.0
It work for me.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
For those getting here after using the default Maven Spring Cloud dependency version related to Spring Boot to 2.4.0 or 2.4.1 (e.g., 2020.0.0-M5 for Spring Boot 2.4.0), besides following RubesMN's good advice, be aware that bootstrapping is not enabled by default in such Spring Cloud dependency. According to the Spring Cloud 2020.0 Release Notes:
Bootstrap, provided by spring-cloud-commons, is no longer enabled by default. If your project requires it, it can be re-enabled by properties or by a new starter.
To re-enable by properties set spring.cloud.bootstrap.enabled=true or spring.config.use-legacy-processing=true. These need to be set as an environment variable, java system property or a command line argument.
The other option is to include the new spring-cloud-starter-bootstrap (in your POM file).
I used the second option and worked just fine.
If you are using 2020.0.0 version of spring cloud then you need to add this dependency in your maven dependencies to enable bootstrap, which is disabled by default:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
As Deinum implies, I'd ensure you have the client configured with the parent as spring-cloud-starter-parent and give it a version. Maven plugins provided by spring cloud wont work when you include in your dependencies and remember cloud is a different project than boot. Change it to:
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>1.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
Second, as a new discipline (and likely not your problem here), I'd use the new annotation on your Application instead of #Configuration and #EnableAutoConfiguration
#SpringBootApplication
Third, double check you have #EnableConfigServer on your config server
Fourth, be sure your bootstrap.properties on your client has your spring application name specified:
spring.application.name=spirent
Finally, if you used the spring-cloud-config example project, there is a default user and security password that you have to set in your URI:
http://user:ddf4757e-0077-42e4-b2ad-2ae04340b08c#localhost:8888
Otherwise, try starting from the spring-cloud-config project located here to ensure your config server is setup correctly:
https://github.com/spring-cloud/spring-cloud-config
I am adding my findings. For me, after spending healthy 2 hours I found that,
Config Server should have: [spring-cloud-config-server]
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
Client Services should have: [spring-cloud-starter-config]
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
Client Services should declare: in application.properties or application.yml
#This does not work
#spring.cloud.config.uri=http://localhost:8880/config-server
spring.application.name=name-by-which-properties/yml-file-is-created
spring.config.import=optional:configserver:http://host:port/application-context
management.endpoints.web.exposure.include=*
What did not work for me:
Adding spring-cloud-starter-bootstrap
Adding spring-cloud-starter-parent
Hope this will help others like me with Spring Boot (2.5.3) and Spring Cloud 2020.0.3
In case, anyone is facing this issue on PCF (with marketplace config-server) even after adding spring-cloud-starter-bootstrap for new spring boot versions, make sure following dependency is added as well -
<dependency>
<groupId>io.pivotal.spring.cloud</groupId>
<artifactId>spring-cloud-services-starter-config-client</artifactId>
</dependency>
It is a problem with spring-boot versions. Spring-cloud-starter-config dependency doesn't work well with the older spring-boot version. Try changing your spring-boot version to some latest version probably above 2.4.0.
The following gradle implementation worked for me:
id 'org.springframework.boot' version '2.4.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'

Resources