Spring Boot: Running as a Java application but classpath contains spring-web - spring-boot

I've successfully managed to create a Spring boot application that runs as a Java application as follows:
#SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication springApplication = new SpringApplication();
springApplication.setWebEnvironment(false);
springApplication.run(Application.class, args);
}
The problem is that my application requires the spring-web module as it is a client to a REST service.
Once I add the spring-web module I get an error:
Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
How can I get it to run as a Java application with spring-web on the classpath

I have "same setup" as you - command line spring boot app that uses RestTemplate from spring-web and everything works well. Maybe it is just that I use "full" spring web starter.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
(and just slightly different main but it shouldn't be a difference)
SpringApplication app = new SpringApplication(MyApplication.class);
app.setWebEnvironment(false);
app.run(args);

Related

Swagger UI - org.springdoc - throws whitelabel error

To mitigate Log4j vulnerability(2.14 to 2.17), we have migrated from spring boot 2.4.0 to 2.6.4. Since Spring Fox was having some issues we migrated to spring doc.
Swagger UI works fine on local, when moved to server (JBOSS), we get white label error , sometimes it works .
can someone help me on this or best alternative to mitigate the Log4j vulnerability.
Used Below Dependency in pom.xml:
Spring Boot version from 2.4.0 to 2.6.4
<dependency>
<groupId>org.springdoc</groupId>
<artifact>springdoc-openapi-ui</artifact>
<version>1.6.6</version>
</dependency>
application.properties :
application-description=#project.description#
application-version=#project.version#
main class :
#Bean
public OpenAPI Internal_APIs(#Value("${application-description}") String appDesciption, #Value("${application-version}") String appVersion)
{
return new OpenAPI()
.info(new Info()
.title("Internal")
.version(appVersion)
.description(appDesciption)
.license(new License()
.name("Apache 2.0")
.url("http://springdoc.org")))
.externalDocs(new External Documentation()
.url("http://springdoc.org"));
}

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>

how load external configuration class in spring boot

I have a spring boot application
The structure of my application is:
src
main
java
org
Application.java
service
--another classes are here
Application.java
package org.baharan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
}
}
Another confirguration class files sush as oracle config and security config are in another application(is named core) is added in my pom.xml
<dependency>
<groupId>org.baharan.amad</groupId>
<artifactId>core</artifactId>
<version>1.0-releases</version>
<type>war</type>
</dependency>
When i build my application, all of classes and properties files of core application are added in my target by overlay maven
When i excute Application.java,spring boot couldn't find any config class isn't in my application but they are in core(after build all of them is added in my target)
In other word how spring boot load configuration classes which dont exist in current application.
please help me.
If they're spring configs you can still use #ComponentScan to load in other bean configs e.g.
#ComponentScan("classpath*:com.myorg.conf")
and
#ComponentScan("classpath*:conf/appCtx.xml")

logging.config configuration for spring boot

I wanted to configure location of log4j.xml file in my spring boot application.
For that I have added logging.config property to my application.properties configuration, indicating log4j.xml file path.
But seems this property is ignored.
But it should work accorindg to spring boot docs:
logging.config= # location of config file (default classpath:logback.xml for logback)
Have I did something wrong?
Spring Boot includes some starters that can be used if you want to exclude or swap specific technical facets. It's using logback by default, if you're gonna use log4j add spring-boot-starter-log4j in your classpath. For example, with maven it would be something like this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
and for log4j 1.x:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
Then add logging.config to your application.properties:
logging.config = classpath:path/to/log4j.xml
I find out that in some cases external logging config(logback.xml)is not ignored: when application is started from application folder, it works properly.
Some clarification on this point: application is run through script, which can be called from any place.
I have not yet gone deep and found out why it works in that way, but if I provide config file path as an argument during the start up, it will work. So we just add this argument to running script:
--spring.config.location=/configPath/application.properties
Probably this problem is caused by Spring loading stages.
If you have any idea what is the root cause of this problem , please share:)
According to spring boot docs :
If you are using the starter poms for assembling dependencies that means you have to exclude Logback and then include your chosen version of Log4j instead.
like this :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
I spend few days to understand whether this should even work and I have doubts regarding this. Despite it is clearly mentioned in the documentation how to use Custom Log Configuration, some treat it differently. There many issues regarding this property is not working here and there on spring github issue tracker, like this and this. And another valid point is that logging configuration must be done as earlier as possible to correctly log application initialization. Thus system property looks like most savvy option here. And you can keep it within your application code. The only requirements would be to set it before spring context initialization.
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
// to start from command line
System.setProperty("logging.config", "classpath:portal-log4j2.yaml");
SpringApplication.run(Application.class, args);
}
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
// to start within container
System.setProperty("logging.config", "classpath:portal-log4j2.yaml");
// this has SpringApplication::run() inside
super.onStartup(servletContext);
}
}
Because all apps in Tomcat web container is loaded within the same JVM, there is no sense to deal with custom logging.config but use single config for the whole container with default file name.

Spring Boot + Jetty & hot deployment

I've read the Hot swapping in Spring Boot but didn't find something that will help my case.
I have a spring-boot app on embedded jetty servers using thymeleaf. My app will serve html,css,js(AngularJS) and REST services.
Folder structure is like this:
/java
----
/resources
/static
/js
/css
/templates (html)
pom.xml
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
But css/html/js is not hot deployed when I change them. I have to restart server every time.
+bonus = when page loads it locks resources (js) and even Ant script cannot replace them.
Can I set scanIntervalSeconds anywhere?
--EDIT--
Main.java
#Configuration
#ComponentScan
#EnableJpaRepositories
#EnableAutoConfiguration
#Import({RepositoryRestMvcConfiguration.class, PersistenceConfig.class, ThymeleafConfig.class})
public class Main {
public static void main(String[] args) throws Exception {
SpringApplication.run(Main.class, args);
}
}
I've run it by right click on class and Debug in IDEA.
How are you launching the app? If you use an IDE with debug mode it should work (except for the locking problem which I believe is Windows OS), or if you launch it with "mvn spring-boot:run", or "gradle bootRun".
I develop using NetBeans 8.0.1.
I fixed the issue of not reloaded static resources in src/main/resources like css (in src/main/resources/resources/css (yes, really twice "resources"!), html-thymeleaf-templates (in src/main/resources/templates) the following way:
My Spring Boot webapp is a JAR project (pom.xml)
Add src/main/resources/application.properties:
spring.template.cache=false
spring.thymeleaf.cache=false
Added custom maven build: Project - Properties - Custom... - Goals...
Execute Goals: spring-boot:run
Set Properties:
jpda.listen=maven (to run it in debug mode)
Env.spring.profiles.active=DEV (optional, but I need it for different SpringConfig-....properties in development, production,...)
Just run the custom maven build for starting the webapp in debug mode. Changes in Thymeleaf-Templates (that are in src/main/resources/templates, eg. index.html (not .xhtml!)) are visible immediately on browser reload.
if you move the css and java script to a folder under
src/main/java/webapp it should work.
for some reason resources under src/main/java/resources didnt seem to get hot deployed when changed.
to fix this as the above post suggested I added
spring.template.cache=false
spring.thymeleaf.cache=false
to Application.properties inside the resources folder.
Note:
I also added this
-javaagent:springloaded-1.2.0.RELEASE.jar -noverify
as a vm runtime argument.

Resources