0
votes

I found a good example for generating odata from MySQL at this GitHub repository

I can run the project by using the command mvn spring-boot:run -P jar inside the eclipse. The only part that I changed in comparison to the original code is my connection details to my local MySQL inside the application.properties file:

# WEB SERVER 
server.port=9090

# MYSQL DATA SOURCE
spring.datasource.url = jdbc:mysql://localhost:3306/olingo
spring.datasource.username = admin
spring.datasource.password = 123456
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# HANA (CLOUD) DB
#spring.datasource.jndi-name = java:comp/env/jdbc/DefaultDB
#spring.jpa.hibernate.ddl-auto = update
#spring.datasource.testWhileIdle = true
#spring.datasource.validationQuery = SELECT 1

# HSQL DB
#spring.datasource.platform=hsqldb

# JPA / HIBERNATE
spring.jpa.show-sql = false
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# AZURE 
#logging.path = D://home//LogFiles

And here is the pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.penninkhof</groupId>
    <artifactId>odata</artifactId>
    <version>0.1.0</version>
    <packaging>${packaging.type}</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.7</java.version>
        <cxf.version>3.1.5</cxf.version>
        <olingo.version>2.0.6</olingo.version>
    </properties>

    <profiles>

        <!-- HANA Cloud Platform -->
        <profile>
            <id>hcp</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <packaging.type>war</packaging.type>
                <tomcat.version>7.0.59</tomcat.version>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                    <!-- Some exclusions are necessary to make Spring Boot run on HCP. -->
                    <exclusions>
                        <exclusion>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-tomcat</artifactId>
                        </exclusion>
                        <exclusion>
                            <groupId>org.apache.tomcat.embed</groupId>
                            <artifactId>tomcat-embed-el</artifactId>
                        </exclusion>
                        <exclusion>
                            <artifactId>logback-classic</artifactId>
                            <groupId>ch.qos.logback</groupId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-jpa</artifactId>
                    <!-- Without this exclusions, the HCP runtime will not be so happy -->
                    <exclusions>
                        <exclusion>
                            <artifactId>logback-classic</artifactId>
                            <groupId>ch.qos.logback</groupId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <dependency>
                    <groupId>org.apache.olingo</groupId>
                    <artifactId>olingo-odata2-jpa-processor-api</artifactId>
                    <version>${olingo.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.olingo</groupId>
                    <artifactId>olingo-odata2-jpa-processor-core</artifactId>
                    <version>${olingo.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                    <scope>provided</scope>
                </dependency>
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>javax.servlet-api</artifactId>
                    <version>3.0.1</version>
                    <scope>provided</scope>
                </dependency>
            </dependencies>
        </profile>

        <!-- Self-contained JAR -->
        <profile>
            <id>jar</id>
            <properties>
                <packaging.type>jar</packaging.type>
            </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-data-jpa</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.apache.olingo</groupId>
                    <artifactId>olingo-odata2-jpa-processor-api</artifactId>
                    <version>${olingo.version}</version>
                    <exclusions>
                        <exclusion>
                            <artifactId>javax.persistence</artifactId>
                            <groupId>org.eclipse.persistence</groupId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <dependency>
                    <groupId>org.apache.olingo</groupId>
                    <artifactId>olingo-odata2-jpa-processor-core</artifactId>
                    <version>${olingo.version}</version>
                    <exclusions>
                        <exclusion>
                            <artifactId>javax.persistence</artifactId>
                            <groupId>org.eclipse.persistence</groupId>
                        </exclusion>
                    </exclusions>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-resources-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-resources</id>
                                <phase>validate</phase>
                                <goals>
                                    <goal>copy-resources</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${basedir}/target/classes/static</outputDirectory>
                                    <resources>
                                        <resource>
                                            <directory>src/main/webapp</directory>
                                            <filtering>true</filtering>
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

    </profiles>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-api</artifactId>
            <version>${olingo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-core</artifactId>
            <version>${olingo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-api-annotation</artifactId>
            <version>${olingo.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>

When I try to run the project as Run on a Server while I am using Tomcat 9 It will show the following error:

SEVERE: Error during ServletContainerInitializer processing javax.servlet.ServletException: Failed to instantiate WebApplicationInitializer class at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:158) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5140) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374) at java.util.concurrent.FutureTask.run(Unknown Source) at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) at java.util.concurrent.AbstractExecutorService.submit(Unknown Source) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909) at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:841) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374) at java.util.concurrent.FutureTask.run(Unknown Source) at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) at java.util.concurrent.AbstractExecutorService.submit(Unknown Source) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909) at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.StandardService.startInternal(StandardService.java:421) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:930) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.startup.Catalina.start(Catalina.java:633) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:343) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:474) Caused by: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory at com.penninkhof.odata.Application.(Application.java:18) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:155) ... 29 more Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1365) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1188) ... 35 more

I can build the project with mvn clean install but when I deploy the war file inside the Tomcat it cannot run the app. It seems the same issue is the source of the problem!

Any help for solving this issue is appreciated.

2
General "how-to" : you can solve many exceptions by looking at their cause (e.g. Caused by in your stack trace). Here, you will solve your ServletException by solving a ClassNotFoundException about SLF4J, which is a pretty actionnable error message.GPI
Sorry I was new to maven. I will check it.MJBZA
Not an issue at all, just a trick, that will help you solve your issues faster, and (or) post a more accurate question and title here on SO if you can't :-) .GPI
Dear @GPI I added pom.xml. If you check the file the slf4j-api is included in the hcp profile. It seems the error is somewhere else. if I run the app with mvn spring-boot:run -P hcp then I see the following error: Unable to start ServletW ebServerApplicationContext due to missing ServletWebServerFactory bean. .MJBZA
If I remove the <scope>provided</scope> then application will be compiled but cannot be run as normal.MJBZA

2 Answers

1
votes

I think you didn't let spring know about some bean class. Look at your xml or configuration file for LoggerFactory In the exception stack above, it says java.lang.ClassNotFoundException occured. Spring can't find class files even if you have them in your java src package. If my answer doesn't solve your problem, you need to show your xml or source code to make sure what the problem is.

0
votes

I changed the pom.xml and removed the profiles, then I could build the project with mvn clean install which will result a war file that I can deploy in Tomcat.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.penninkhof</groupId>
    <artifactId>odata</artifactId>
    <version>0.1.0</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.7</java.version>
        <cxf.version>3.1.5</cxf.version>
        <olingo.version>2.0.6</olingo.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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-jpa-processor-core</artifactId>
            <version>${olingo.version}</version>
        </dependency>       
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-jpa-processor-api</artifactId>
            <version>${olingo.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-api</artifactId>
            <version>${olingo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-core</artifactId>
            <version>${olingo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-api-annotation</artifactId>
            <version>${olingo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/classes/static</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/webapp</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>

However when I deploy it in the Tomcat it can access the database for some hours and after some hours it cannot anymore fetch data from MySQL!

When we want to run the app inside the Eclipse with Run on Server option, it needs to access the app after a success run like this http://localhost:9090/<artifactName>/ i.e. http://localhost:9090/odata/ in this case!