Camel-Spring Boot: Route doesn't stop after execution - spring-boot

I am using Spring Boot + Camel with below config.
Main Class
#SpringBootApplication
public class Application extends RouteBuilder {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
public void configure() throws Exception {
from("sql:{{list.sql}}?dataSource=#dataSource")
.log("process row ${body}")
.to("stream:out");
}}
application.properties
camel.springboot.name=ListJob
camel.springboot.main-run-controller=true
list.sql=<SELECT QUERY>
spring.datasource.driver=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://IP:3306/test
spring.datasource.username=un
spring.datasource.password=pwd
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.abc</groupId>
<artifactId>wl-event-notification-batch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>notification</name>
<description></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>
<!-- Camel -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stream</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-sql</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jdbc</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.17.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</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>
</project>
My Spring Boot runs the route in infinite loop executing SELECT query because of "camel.springboot.main-run-controller" property until I hit CNTRL+C. If I remove this property, application just starts and stops the route immediately without completing the execution.
Can someone please help me, to get my route start, execute the steps and stop when I run Spring Boot application?

You can use the spring-boot properties:
# handle only one 1 message and then stop the route
camel.springboot.duration-max-messages=1
# run for 180 seconds and then gracefully shutdown
camel.springboot.duration-max-seconds=180
# a polling consumer will cancel the graceful shutdown so set the shutdownTimeout to a minimum
camel.springboot.shutdownTimeout=1

add .stop():
public void configure() throws Exception {
from("sql:{{list.sql}}?dataSource=#dataSource")
.log("process row ${body}")
.stop();
}

Related

always throwing Bad credentials in Ldap authentication using Spring Boot

I tried many times, I think issue is in config class ,I was stuck from 2 days,
please solve this
thank you in advance
my pom.xml
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>ldap</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ldap</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-security</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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
</dependency>
<dependency>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
this is config class and issue in this b'coz I checked filter can successfully
find the Ldif file but not check value of this cridentials...
myConfig class
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().fullyAuthenticated().and().formLogin();
}
#Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.ldapAuthentication().userDnPatterns("uid={0},ou=people")
.contextSource(contextSource())
.passwordCompare()
.passwordAttribute("userPassword");}
#Bean
public DefaultSpringSecurityContextSource contextSource() {
return new DefaultSpringSecurityContextSource(Arrays.asList("localhost:8389/"),
"dc=springframework,dc=org");
}
}
my application.properties file
spring.ldap.embedded.ldif=classpath:test-server.ldif
spring.ldap.embedded.base-dn=dc=springframework,dc=org
spring.ldap.embedded.port=8389

Spring Web-mvc not working with Spring Boot[Spring cloud]

I am new to microservices.I have created a microservice application using Spring.I am using "spring-cloud-starter-eureka-server".I have total 3 modules:
microservicecldm
account-microservice
webclient-microservice-server
Find below their main classes accordingly:
#EnableEurekaServer
#SpringBootApplication
public class MicroservicecldmApplication {
public static void main(String[] args) {
SpringApplication.run(MicroservicecldmApplication.class, args);
}
}
#EnableDiscoveryClient
#SpringBootApplication
#ComponentScan(basePackages="com.wmsdm.cloud")
public class WebclientMicroserviceServerApplication{
public static void main(String[] args) {
SpringApplication.run(WebclientMicroserviceServerApplication.class, args);
}
}
In application.properties I have following entries:
spring.application.name=WebclientMicroserviceServerApplication
eureka.client.serviceUrl.defaultZone:http://localhost:9091/eureka/
server.port=9993
eureka.instance.lease-renewal-interval-in-seconds=80
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
spring.mvc.static-path-pattern=/resources/**
I have a AccountController class as follows:
#Controller
public class AccountController {
#RequestMapping(value = "/account", method = RequestMethod.GET)
public String viewAccount() {
return "accountSummary";
}
}
I have the accountSummary.jsp page in my /WEB-INF/view path.Also in maven's pom.xml I have the following dependencies along with other dependencies:
<groupId>com.wmsdm.cloud</groupId>
<artifactId>webclient-microservice-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>webclient-microservice-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</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>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Inspite of these when I hit the URL:
http://localhost:9993/WebclientMicroserviceServerApplication/account
I get the error No mapping found for the requested uri
I suggest using spring-boot-starter-web instead of tomcat-embed-jasper. You can use multiple starters in your pom.xml.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
See spring.io for more info.
Make sure the context component scan base package is correct. If not specified specify it using regex.

"resources" folder not being created in WEB-INF directory in Spring Boot app

I have a Spring Boot application in development that I recently upgraded from Spring 3 to Spring 4 (spring-boot-starter-parent 1.3.5.RELEASE to 1.4.6.RELEASE).
I have, within my src/main/resources/templates directory, an html template (called 'test-template.html') which I have been using to create HTML email content, using Thymeleaf's TemplateEngine. (This was working fine prior to upgrading to version 4.)
The problem I have at present is that no "templates" directory is being created within WEB-INF on the server (or within the relevant directory within "target", upon building with Maven). As a result, I'm getting the following:
java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/templates/test-template.html]
The following is my configuration class for Thymeleaf. (I am using manual configuration since I'm using Thymeleaf only for email template processing, and thus don't want Spring Boot to configure a Thymeleaf view resolver. I get the same error with auto-configuration, however.)
#Configuration
public class ThymeleafConfig implements ApplicationContextAware {
private static final Logger logger = LoggerFactory.getLogger(ThymeleafConfig.class);
private ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setEnableSpringELCompiler(true);
engine.setTemplateResolver(templateResolver());
return engine;
}
private ITemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/WEB-INF/templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode(TemplateMode.HTML);
return resolver;
}
}
Here's the 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>myapp</artifactId>
<version>1.0.10-SNAPSHOT</version>
<packaging>war</packaging>
<name>myapp</name>
<description>blah blah</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<start-class>com.matchingchina.AcumenApplication</start-class>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
<version>3.2.1</version>
</dependency>
<!-- WEBJARS -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.6</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>font-awesome</artifactId>
<version>4.6.1</version>
</dependency>
</dependencies>
</project>
Change /WEB-INF/templates/ to classpath:/template/. On the classpath doesn't necessary mean on the web context path.

SpringBoot: Defining the main class in pom.xml file

I've generated a Spring Boot web application using Spring Initializer, embedded Tomcat, Thymeleaf template engine.Technologies used: Spring Boot 1.4.2.RELEASE, Spring 4.3.4.RELEASE, Thymeleaf 2.1.5.RELEASE, Tomcat Embed 8.5.6, Maven 3, Java 8.
I have this pom.xml file,
and I generate the war using this command
mvn clean package -DskipTests -Dspring.profiles.active=pebloc,war -DAPPKEY=pebloc
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bookcloud</groupId>
<artifactId>bookcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>bookcloud</name>
<description>Book Cloud </description>
<profiles>
<profile>
<id>jar</id>
<properties>
<spring.boot.mainclass>com.bookcloud.iot.BookCloudApplication</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>war</id>
<properties>
<spring.boot.mainclass>com.bookcloud.iot.BookCloudApplicationWar</spring.boot.mainclass>
</properties>
</profile>
</profiles>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>spring-boot-email-core</artifactId>
<version>0.5.1</version>
</dependency>
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>spring-boot-thymeleaf-email</artifactId>
<version>0.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.icegreen/greenmail -->
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.5.3</version>
<optional>true</optional>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- hot swapping, disable cache for template, enable live reload -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>${spring.boot.mainclass}</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
but I still got this error:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) on project bookcloud: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.bookcloud.iot.BookCloudApplication, com.bookcloud.iot.BookCloudApplicationWar] -> [Help 1]
BookCloudApplication.java
#Profile("!war")
#SpringBootApplication
#Import({SecurityConfig.class ,PersistenceConfig.class, ServiceConfig.class})
public class BookCloudApplication {
public static void main(String[] args) {
SpringApplication.run(BookCloudApplication.class, args);
}
}
BookCloudApplicationWar.java
#Profile("war")
#Import({SecurityConfig.class ,PersistenceConfig.class})
#SpringBootApplication
public class BookCloudApplicationWar extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(BookCloudApplicationWar.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(BookCloudApplicationWar.class, args);
}
}
Maven profiles has no relation to Spring profiles.
You have two Maven profiles: jar, war
You either use:
mvn clean package -Pjar
or
mvn clean package -Pwar
The error you are getting is while building the artifact because pom needs:
<start-class>your.app.entry.point.Main</start-class>
inside the properties element.
I would suggest to have only one start class regardless of the Maven profile.
something like:
pom.xml:
...
<modelVersion>4.0.0</modelVersion>
<groupId>com.asimio.cloud</groupId>
<artifactId>zuul-server</artifactId>
<version>0-SNAPSHOT</version>
<packaging>${packaging.type}</packaging>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<start-class>your.app.entry.point.Main</start-class>
<packaging.type>jar</packaging.type>
...
<profiles>
<profile>
<id>war</id>
<properties>
<packaging.type>war</packaging.type>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
...
</profile>
</profiles>
...
Main.java:
package your.app.entry.point;
...
#SpringBootApplication
#Import({SecurityConfig.class ,PersistenceConfig.class, ServiceConfig.class})
public class App extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(App.class);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
No need for Spring profiles. This artifact can now be built using either Maven profile and will run either using:
java -jar .....
when building this artifact as a jar
or
as a war file deployed to a servlet container when built using the -Pwar Maven profile.
Maven profiles and Spring profiles are two different concepts.
You should also run Maven using the war profile, in order for the spring.boot.mainclass property to be set with the correct matching value.
Add -Pwar to the command line in order to activate the war Maven profile.
mvn clean package -DskipTests -Pwar -Dspring.profiles.active=pebloc,war -DAPPKEY=pebloc

Spring Boot on Embedded Tomcat with JSTL still throws java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config

I have an application which leverages PrimeFaces, elastic search,Spring boot.
Following is it's 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.acn.hps.aops.demo</groupId>
<artifactId>PSCommandCenter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CommandCentre</name>
<description>Command Center Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.geocoder-java</groupId>
<artifactId>geocoder-java</artifactId>
<version>0.16</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<!-- Primefaces -->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.2</version>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>all-themes</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-runtime</artifactId>
<version>2.3.4</version>
</dependency>
<!-- JSF dependencies -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.8</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Elastic Search dependencies -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>primefaces</id>
<url>http://repository.primefaces.org</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
Following is its spring configuration,
#SpringBootApplication
public class CommandCentreApplication extends WebMvcConfigurerAdapter{
public static void main(String[] args) {
SpringApplication.run(CommandCentreApplication.class, args);
}
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/home.jsf");
}
#Bean
ServletRegistrationBean facesServletRegistration() {
return new ServletRegistrationBean() {
#Override
public void onStartup(ServletContext servletContext)
throws ServletException {
FacesInitializer facesInitializer = new FacesInitializer();
facesInitializer.onStartup(Collections.singleton(CommandCentreApplication.class),
servletContext);
servletContext.setInitParameter("primefaces.THEME", "redmond");
servletContext.setInitParameter("javax.faces.FACELETS_REFRESH_PERIOD", "0");
servletContext.setInitParameter("facelets.SKIP_COMMENTS", "true");
}
};
}
#Bean
ServletRegistrationBean pushServletRegistration(){
ServletRegistrationBean pushServlet = new ServletRegistrationBean(new PushServlet(), "/primepush/*");
pushServlet.addInitParameter("org.atmosphere.annotation.packages", "org.primefaces.push");
pushServlet.addInitParameter("org.atmosphere.cpr.packages", "com.acn.hps.aops.demo.map.service.channel");
pushServlet.setAsyncSupported(true);
pushServlet.setLoadOnStartup(0);
pushServlet.setOrder(Ordered.HIGHEST_PRECEDENCE);
return pushServlet;
}
}
and my elastic search's configuration,
#Bean
public Client client(){
TransportClient client= new TransportClient();
TransportAddress address = new InetSocketTransportAddress(PropertiesLoader.esIP, PropertiesLoader.esPort);
client.addTransportAddress(address);
return client;
}
This same application runs as expected when I execute it from my IDE (Eclipse) but when I execute the application from its jar then I am getting the following exception ,
java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config
at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_45].
I searched for this exception and I found that jstl's jar is added to avoid this error. But I already have jstl's jar in tomcat's jar. And also if this is the problem then I should be getting the same exception while executing from my IDE.
You can refer this github code which i used as outline for my application. I am facing the same issue in this also.
When using spring-boot-starter as a parent, running
mvn clean install
should produce a fat jar (that means all dependencies are copied into that jar).
More informations can be found here:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-build.html
Maybe this part in the spring-boot-starter plugin within the pom.xml
<configuration>
<executable>true</executable>
</configuration>
is messing with this. Try to remove that part and build the jar again.

Resources