How view-file mapping works on Spring - spring

I don't understand how Spring maps view to files (e.g. "Homepage" view to "Home.jsp"). On Websphere you would find it in an XML file. On Spring I see that you map end-points to view on controllers in a Java class, but I cannot actually find how I can choose to map a specific view to a specific file.
Thank you
EDIT 1:
It seems like my first problem is that JSP support is not enabled. This is my 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>org.springframework</groupId>
<artifactId>gs-serving-web-content</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
If I add:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
Then I get an error on my pom.xml:
The managed version is 8.5.27 The artifact is managed in org.springframework.boot:spring-boot-dependencies:1.5.10.RELEASE
The file reported is org.springframework.boot:spring-boot-dependencies:1.5.10.RELEASE.pom and the line report is:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>${tomcat.version}</version>
</dependency>
EDIT 2:
Please consider that, even if I set my spring.mvc.view.suffix: .html, I get error 500 when I try to access http://localhost:8080/greeting. Here it is my console error:
2018-03-11 21:24:40.923 ERROR 4892 --- [nio-8080-exec-5] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-5] Exception processing template "greeting": Error resolving template "greeting", template might not exist or might not be accessible by any of the configured Template Resolvers
2018-03-11 21:24:40.924 ERROR 4892 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "greeting", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "greeting", template might not exist or might not be accessible by any of the configured Template Resolvers

You need to provide two things to Spring's ViewResolver if you want it to find your view files:
prefix - path to the folder where all searching begins.
suffix - extension of your views.
You can do this via xml:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Via application.properties:
spring.mvc.view.prefix: /WEB-INF/pages/
spring.mvc.view.suffix: .jsp
Via Java Config:
#Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
So if you have file /WEB-INF/jsp/home.jsp Spring can find the file only having hello string.
If you have two hello.jsp in different folders it won't be a problem because you need to explicitly tell in what folder your view is reside(for example, you will specify return "/foo/hello" or return "/bar/hello")

Related

Error Deploying Spring Boot App using 2.6.2 on IBM Websphere 8.5.5.20

I'm trying to Deploying a spring boot app using 2.6.2 on IBM Websphere 8.5.5.20 (Using java 8)
To test out the Issue I have tried to deploy a simple Hello World restcontroller and facing the issues noted below. I'm able to deploy the application using Spring Boot 1.5 on Websphere 8.5.5.20, but facing difficulty when moving to spring-boot 2.6.2.
Please note I have tried with the javax.servlet and jakarta.servlet (version as per spring boot dependencies). I have followed this Link [https://stackoverflow.com/questions/48156126/websphere-8-5-with-spring-5], Deploy Spring Boot 2.x apps on WebSphere 8.5.5
I get the following error based on how I have the classloader for my app by setting it from
Enterprise Applications > simple-boot > Manage Modules > simple-boot.war
Parent Last
Error:
[1/7/22 16:39:57:407 EST] 00000001 ContainerHelp E WSVR0501E: Error creating component com.ibm.ws.runtime.component.CompositionUnitMgrImpl#c412fa58
com.ibm.ws.exception.RuntimeWarning: com.ibm.ws.exception.RuntimeWarning: com.ibm.ws.webcontainer.exception.WebAppNotLoadedException: Failed to load webapp: Failed to load webapp: javax.servlet.ServletContainerInitializer: Provider ch.qos.logback.classic.servlet.LogbackServletContainerInitializer not a subtype
Parent First
Error:
[1/7/22 16:42:17:173 EST] 00000047 SystemOut O 2022-01-07 16:42:17.173 WARN 31546 --- [ver.startup : 1] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultValidator' defined in class path resource [org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax/validation/Configuration.getDefaultParameterNameProvider()Ljavax/validation/ParameterNameProvider; (loaded from file:/xxxxxxx/was/INSTANCE1/plugins/javax.j2ee.validation.jar by org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader#25be5b0d) called from class org.springframework.validation.beanvalidation.LocalValidatorFactoryBean (loaded from file:xxxx/simple-boot.ear/simple-boot.war/WEB-INF/lib/spring-context-5.3.14.jar by
com.ibm.ws.classloader.CompoundClassLoader#24768f5e[war:simple-boot/simple-boot.war]
My pom.xml
Note: In this pom.xml I have javax.servlet using 3.1.0 for testing but as noted above tried with default version as per the spring boot dependencies also with same result.
<?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.6.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.my.testApp</groupId>
<artifactId>simple-webapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>simple-boot</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-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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<exclusions>
<exclusion>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>simple-boot</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Based on the error message, the parent-last version looks like it's failing because there is a copy of the Servlet API packaged in your application. This is not a usable configuration, because the container will link to its own copy of the API, and as a result it wouldn't be able to successfully cast application artifacts that link to their own API instance. There are some APIs for which you can bring your own version in the app, but Servlet is not among them.
For the parent-first failure, it looks like Spring is expecting a newer version of the javax.validation package than is provided by the server - it's calling a method that doesn't exist in the server's version. WebSphere 8.5.5 implements Java EE 6, which includes Bean Validation 1.0, and that method wasn't added until Bean Validation 1.1. You'll need a version of Spring that works with Java EE 6 or is configurable to avoid higher-version dependencies. It might also be possible to resolve this by running parent-last and including a separate implementation along with the API (obviously, removing the Servlet API as referenced in the previous paragraph), although I'm not familiar enough with the Bean Validation API to know whether that would cause a conflict with the server runtime in the same way as Servlet.

Problem extending Spring WebFlux application with reactive DB client: conflicting Maven dependencies?

I have a small working Spring WebFlux app made basing on this code:
https://github.com/chang-chao/spring-webflux-reactive-jdbc-sample
As far as I've understood this is some kind of mix between purely reactive programming and usual blocking relational databases.
Now I have a task to add reactive DB client to my app. I stared with this guide:
https://spring.io/guides/gs/accessing-data-r2dbc/
But as soon as I added following dependencies to my pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
<version>0.8.4.RELEASE</version>
<scope>runtime</scope>
</dependency>
my WORKING app failed to start saying it couldn't find autowired repository bean. This error disappeared as soon as I've removed two dependencies above.
Initial full pom.xml of WORKING app:
<?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.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.freelance</groupId>
<artifactId>studentlist</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>student-list</name>
<description>Spring WebFlux application for managing students</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.3.4.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<version>3.3.10.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I suppose some dependencies are conflicting with each other. Is there a way to actually use these dependencies with all my other dependencies so I'll be able to follow that guide?
I don't know if app's Java code is needed for this question, and if it is, which pieces. For now I'll just add application.properties:
spring.h2.console.enabled=true
spring.h2.console.path=/h2_console
spring.datasource.url=jdbc:h2:~/studentlist
spring.datasource.platform=h2
spring.datasource.initialization-mode=always
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.generate-ddl=true
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto = update
spring.jpa.show-sql=true
logging.level.org.springframework=warn
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=trace
logging.level.org.hibernate.SQL=warn
logging.level.io.netty=warn
spring.datasource.maximum-pool-size=100
Adding either of these two dependencies, spring-boot-starter-data-r2dbc or r2dbc-h2, without the second one is enough to cause this error:
2020-10-20 15:31:26.008 WARN 11580 --- [ main] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'studentService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentService': Unsatisfied dependency expressed through field 'studentRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.freelance.studentlist.repository.StudentRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
2020-10-20 15:31:26.123 ERROR 11580 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field studentRepository in com.freelance.studentlist.service.StudentServiceImpl required a bean of type 'com.freelance.studentlist.repository.StudentRepository' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.freelance.studentlist.repository.StudentRepository' in your configuration.
Process finished with exit code 1
I removed JPA dependency and I'm currently trying to make things work with just r2dbc.
I suppose my application.properties would no longer be valid. Could you please help me with changing it? I use this code fragment from the guide:
public class R2DBCConfiguration extends AbstractR2dbcConfiguration {
#Bean
public H2ConnectionFactory connectionFactory() {
return new H2ConnectionFactory(
H2ConnectionConfiguration.builder()
.url("jdbc:h2:~/studentlist;DB_CLOSE_DELAY=-1;TRACE_LEVEL_FILE=4")
.username("sa")
.build());
}
}
I strongly suspect that such url is not valid for r2dbc. What would be valid substitution for jdbc:h2:~/studentlist URL in case of r2dbc H2 (not in-memory, just local DB)?
How should I change this block of code in application.properties ? URL in particular!
**spring.datasource.url=jdbc:h2:~/studentlist**
spring.datasource.platform=h2
spring.datasource.initialization-mode=always
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
Please help if you know! Can't google an appropriate example for now...
The spring.datasource is for the traditional Jdbc data source, to configure an R2dbc connection, use spring.r2dbc prefix instead in Spring Boot application.properties.
Check my Spring R2dbc example, if you are new to Spring Data R2dbc, the docs in Readme.md.

Spring boot with thymeleaf could not resolve view with name

I'm setting a spring boot project with thymeleaf but my view isn't displaying and getting the following error in the browser.
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri May 24 00:44:22 GST 2019
There was an unexpected error (type=Not Found, status=404).
No message available
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
Controller:
#Controller
public class MainController {
#GetMapping("/")
public String homePage() {
return "home";
}
}
application.properties:
spring.thymeleaf.cache=false
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.application.name=Bootstrap Spring Boot
I had added thymeleaf feature in my html file, but when it wasn't working I just made it simple html file. My home.html is:
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>Hello !</h1>
<p>
Welcome to Our App</span>
</p>
</body>
</html>
It's a very simple and basic controller. I am not seeing any exception stack trace in the console.
When you see this error , there are many possible reasons so try the following :
Make sure that you folder structure is correct. When the maven folder structure is not followed , spring might have difficulties discovering endpoints etc.
Make sure that you keep your main application.java a root package above other classes since spring boot annotation only scan the classes below your main class package.
For example :
com
+- APP
+- MainApplication.java <--- your main class should be here, above your other classes
|
+- model
| +- model.java
+- controller
+- Controller.java
Make sure that your resources are placed in the correct directory.
By default, Spring Boot serves static content from a directory called
/static (or /public or /resources or /META-INF/resources) in the
classpath or from the root of the ServletContext.
When you use one of these templating engines with the default
configuration, your templates are picked up automatically from
src/main/resources/templates.
Official doc : https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html
Since, you have provided the thymeleaf configuration in the application.yml, you can put your thymeleaf templates in the /templates folder of your resources folder and spring should take it up.
After a couple of days of effort, I resolved this issue by downgrading spring boot version from 2.1.5 to
<version>2.0.6.RELEASE</version>
l have a similar problem .l resolved it by renaming the home.html page to index.html and spring seems to resolve it automatically.Works like magic

Camel not starting in spring

I can run my Camel application using the camel-maven-plugin within maven (mvn camel:run). The camel-context.xml file is read and my routes start correctly.
My issues come about when I try to execute these camel routes on spring. When spring starts, I do not see any logs from Camel like I did when running the camel plugin directly. I also dont have any evidence that anything camel related has started. What is my configuration missing to successfully run the application? I am currently attempting to run this via an embedded tomcat instance (see mvn profile below). I would imagine that there is something unique that I need to do in order to get spring to find the camel context.
Thanks for any and all help!
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>
<parent>
<groupId>---</groupId>
<artifactId>---</artifactId>
<version>1.0.6-SNAPSHOT</version>
</parent>
<artifactId>---</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>${packaging.type}</packaging>
<properties>
<jacoco.minimum.code.coverage>0.8</jacoco.minimum.code.coverage>
<packaging.type>war</packaging.type>
<failOnMissingWebXml>false</failOnMissingWebXml>
<org.apache.camel.version>2.16.0</org.apache.camel.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-csv</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.19.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-aws</artifactId>
<version>2.19.2</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>2.19.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
<version>2.19.2</version>
</plugin>
</plugins>
</build>
<profiles>
<!-- Default build profile for generating war -->
<profile>
<id>war</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<packaging.type>war</packaging.type>
<log.dir>${catalina.base}/logs</log.dir>
<!-- updates bootstrap.properties -->
<config.override.path>file:${catalina.base}/conf</config.override.path>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptor>/src/main/resources/deployablecontent.xml</descriptor>
<tarLongFileMode>posix</tarLongFileMode>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<!-- Build profile for stand-alone java application with embedded Tomcat
Container -->
<profile>
<id>embedded</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<packaging.type>jar</packaging.type>
<log.dir>logs</log.dir>
<!-- updates bootstrap.properties -->
<config.override.path>./conf</config.override.path>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
<version>2.19.2</version>
</plugin>
</plugins>
</build>
</profile>
</profiles>
My Routebuilder class:
public class PriorityCodeSourcesUpdaterRouteBuilder extends RouteBuilder {
private Endpoint incomingEndpoint;
private Endpoint outgoingEndpoint;
#Override
public void configure() throws Exception {
from(incomingEndpoint)
.process((exchange) -> {
System.out.println("new file received");
})
.to(outgoingEndpoint);
}
/**
* Set the incoming endpoint from the spring config file.
* #param incomingEndpoint incoming endpoint
*/
public void setIncomingEndpoint(final Endpoint incomingEndpoint) {
this.incomingEndpoint = incomingEndpoint;
}
/**
* Set the outgoing endpoint from the spring config file.
* #param outgoingEndpoint outgoing endpoint
*/
public void setOutgoingEndpoint(final Endpoint outgoingEndpoint) {
this.outgoingEndpoint = outgoingEndpoint;
}
}
My camel-context.xml that lives in resources/META-INF/spring/:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:camel="http://camel.apache.org/schema/spring" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<camel:camelContext id="camel">
<camel:routeBuilder ref="PriorityCodeSourcesUpdaterRouteBuilder"/>
<camel:endpoint id="incomingEndpoint" uri="">
<camel:property key="accessKey" value=""/>
<camel:property key="secretKey" value="RAW()"/>
<camel:property key="region" value=""/>
<camel:property key="deleteAfterRead" value="false"/>
</camel:endpoint>
<camel:endpoint id="outgoingEndpoint" uri="file://#{systemProperties['java.io.tmpdir']}">
<camel:property key="fileName" value="deadBeefName"/>
<camel:property key="readLock" value="markerFile "/>
<!-- We need a customer idempotentKey because all files sent to this endpoint have the same fileName. -->
<!-- This will prevent camel from thinking that it has already consumed the file. -->
<!--<camel:property key="idempotentKey" value="3"/>-->
</camel:endpoint>
</camel:camelContext>
<bean id="PriorityCodeSourcesUpdaterRouteBuilder" class=".....PriorityCodeSourcesUpdaterRouteBuilder">
<property name="incomingEndpoint" ref="incomingEndpoint" />
<property name="outgoingEndpoint" ref="outgoingEndpoint" />
</bean>
TL;DR:
Try adding the camel-spring-boot-starter dependency to your POM file, mark your route with #Component annotation and add a #SpringBootApplication class to start your Spring Context tied up with Camel.
Reading through your files I'm guessing that you are using Spring Boot, right?
If that so, it's nice to have the following dependencies in your POM:
<dependencyManagement>
<dependencies>
<!-- Spring Boot BOM -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Camel BOM -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-dependencies</artifactId>
<version>${camel.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
This are the BOM (Bills of Materials) from both Spring Boot and Camel. This way all your required dependencies will be resolved nicely, plus avoiding the need to describe camel component's versions all the time.
It's a requirement to have the camel-context.xml file? If it isn't you, could define everything on your RouteBuilder class and put a #SpringBootApplication annotation class on your classpath.
From the docs:
Spring Boot component provides auto-configuration for Apache Camel. Our opinionated auto-configuration of the Camel context auto-detects Camel routes available in the Spring context and registers the key Camel utilities (like producer template, consumer template and the type converter) as beans.
camel-spring-boot jar comes with the spring.factories file, so as soon as you add that dependency into your classpath, Spring Boot will automatically auto-configure Camel for you.
The problem I think you're facing is that there's no "glue" between your Spring Context and the Camel Context (camel-context.xml file). Add the #Component annotation to your RouteBuilder and the following dependecy:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
Then these routes will be started automatically. To keep the main thread blocked so that Camel stays up, either include the spring-boot-starter-web dependency, or add camel.springboot.main-run-controller=true to your application.properties or application.yml file.
There's more information and examples in the docs.
Cheers!
As requested, below is a simplified version of Camel-Spring setup that we've been using in our Camel projects. Spring Boot is used (the goodies it offers are just too good to ignore, IMO) and we're also using web starter. Since you're using WAR packaging anyway, this shouldn't be an issue to you.
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">
<artifactId>stackoverflow</artifactId>
<groupId>sandbox</groupId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.19.2</version>
</dependency>
</dependencies>
</project>
src/main/java/stackoverflow/CamelRoute.java (the route definition, discovered automatically on startup, by the virtue of being placed in the package path of the #SpringBootApplication class - the TestApp class below):
package stackoverflow;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
#Component
public class CamelRoute extends RouteBuilder {
#Value("${message}")
private String message;
// you can also have dependencies #Autowired here
#Override
public void configure() {
from("direct:test").process(exchange -> {
exchange.getIn().setBody(message);
});
}
}
src/main/resources/application.properties (to illustrate how can configuration values be passed on to your route definitions):
message=Test
src/main/resources/log4j.properties (mostly so that you would see your route started in the logs):
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %c:%L - %m%n
src/main/java/stackoverflow/TestApp.java:
package stackoverflow;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class TestApp {
public static void main(String[] args) {
SpringApplication.run(TestApp.class, args);
}
}
The TestApp class starts the application, and keeps running until stopped (it starts an embedded Tomcat server). The routes are discovered and started on app startup.
This setup prefers Java Configuration over XML, but if you still prefer the XML, you can import your configuration using #ImportResource annotation on your TestApp. You will also be able to autowire the XML-configured classes into your route definitions.
Let me know if you have any questions about this setup.

Spring Batch Admin (2.0.0.M1): error creating bean batchMBeanExporter

I am trying to run spring batch admin from spring boot application.
I am using this excellent example from Michael (#mminella). This application works fine.
However, I need similar application using maven; I went ahead and created one with same files, jobs, configs and dependencies. But I ended up in following exception.
Only difference I see is the version of spring-batch-core. With maven it's version is 3.0.6 while with gradle (Michael's application) has 3.0.4.
Not sure what's going on when I use maven; really appreciate your help. Please let me know if any additional details required.
Looking forward for your inputs.
Thank you,
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchMBeanExporter' defined in URL [jar:file:/C:/Users/Admin/.m2/repository/org/springframework/batch/spring-batch-admin-manager/2.0.0.BUILD-SNAPSHOT/spring-batch-admin-manager-2.0.0.BUILD-SNAPSHOT.jar!/META-INF/spring/batch/bootstrap/manager/jmx-context.xml]: Cannot create inner bean 'org.springframework.aop.framework.ProxyFactoryBean#7807ac2c' of type [org.springframework.aop.framework.ProxyFactoryBean] while setting bean property 'jobService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.framework.ProxyFactoryBean#7807ac2c': Post-processing of FactoryBean's object failed; nested exception is java.lang.IllegalStateException: #Scheduled method 'removeInactiveExecutions' found on bean target class 'SimpleJobService' but not found in any interface(s) for a dynamic proxy. Either pull the method up to a declared interface or switch to subclass (CGLIB) proxies by setting proxy-target-class/proxyTargetClass to 'true'.
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>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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-batch-admin.version>2.0.0.BUILD-SNAPSHOT</spring-batch-admin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-admin-manager</artifactId>
<version>${spring-batch-admin.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-admin-domain</artifactId>
<version>${spring-batch-admin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>sqljdbc4</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots-local</id>
<name>Spring Maven Snapshot Repository</name>
<url>https://repo.spring.io/libs-snapshot-local/</url>
</repository>
</repositories>
</project>
Spring Boot config
#SpringBootApplication(exclude = {HypermediaAutoConfiguration.class, MultipartAutoConfiguration.class})
#EnableBatchAdmin
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
In order to avoid, JobController null pointer exception, put below
server.servletPath=/*
Best regards,
Jigar.
I finally found a way to make the annotation #EnableBatchAdmin in a maven project.
This allow you to fix the following error.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'batchMBeanExporter' defined in URL [jar:file:/C:/Users/Admin/.m2/repository/org/springframework/batch/spring-batch-admin-manager/2.0.0.BUILD-SNAPSHOT/spring-batch-admin-manager-2.0.0.BUILD-SNAPSHOT.jar!/META-INF/spring/batch/bootstrap/manager/jmx-context.xml]: Cannot create inner bean 'org.springframework.aop.framework.ProxyFactoryBean#7807ac2c' of type [org.springframework.aop.framework.ProxyFactoryBean] while setting bean property 'jobService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.framework.ProxyFactoryBean#7807ac2c': Post-processing of FactoryBean's object failed; nested exception is java.lang.IllegalStateException: #Scheduled method 'removeInactiveExecutions' found on bean target class 'SimpleJobService' but not found in any interface(s) for a dynamic proxy. Either pull the method up to a declared interface or switch to subclass (CGLIB) proxies by setting proxy-target-class/proxyTargetClass to 'true'.
The solution was found in this issue https://github.com/spring-projects/spring-batch-admin-samples/issues/12
and is to override the jmx-context.xml file by the following
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:mbean-server id="mbeanServer"/>
<context:mbean-export default-domain="spring.application" server="mbeanServer"/>
<!-- Override the `batchMBeanExporter` and set the `proxyTargetClass` property to true on the `jobService` proxy.
This is a workaround for the related https://jira.spring.io/browse/BATCHADM-126 -->
<bean id="batchMBeanExporter" class="org.springframework.batch.admin.jmx.BatchMBeanExporter">
<property name="server" ref="mbeanServer"/>
<property name="jobService">
<bean class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetName" value="jobService"/>
<property name="proxyTargetClass" value="true"/>
</bean>
</property>
<property name="defaultDomain" value="spring.application"/>
</bean>
by placing this code in your own jmx.context.xml file in main/resources/META-INF/spring/batch/override
Finally, I was able to run the spring batch with spring batch admin; though with this project. Still not able to use # EnableBatchAdmin with maven build. Many thanks to mminella and #vesperaba.
Though I am facing another issue; when I run the project with 2.0.0.M1 from repository, it works fine. However, when I build project locally after resolving [JobController null pointer exception] (https://github.com/spring-projects/spring-batch-admin/pull/42);
I have to include "spring-batch-admin-resources" in POM explicitly;
while this was not the case when I run the application using
repository (libs-snapshot-local) 2.0.0.M1
build.
When I include "spring-batch-admin-resources" application is not
able to start saying that "cvc-datatype-valid.1.2.1:
'/job-configuration' is not a valid value for 'NCName'."
Any comments will be appreciated. Thank You.

Resources