openxava / documentation / Spring Boot

Spring Boot is a tool that makes web application and microservices development with Spring Framework faster and easier.
The idea of integrating OpenXava into a simple Spring Boot application with Rest services and JPA is to leverage the automatically generated view by OpenXava. We will work with our sample project and import it into Spring Tool Suite 4 for Eclipse. You will also need OpenXava Studio and MySQL (we will use MySQL 8 in this example). Finally, we will deploy the application on a Tomcat 9 server.

Import and configure the project in Spring Tool Suite 4

Download the project as a zip file then unzip it.

Select Import projects from Package Explorer view or File > Import and select Existing Maven Projects.

Browse to the folder where you unzipped the project and select it.

Open the application.properties file located in src/main/resources and adapt it to your MySQL database by providing URL, username, password, and dialect. Make sure your MySQL database is up and running.

Manually create some records in your MySQL database:

INSERT INTO `yourdatabase`.`tutorials` (`id`, `description`, `published`, `title`) 
VALUES ('1', 'description 1', 0, 'title 1');

Verify that the services are working correctly http://localhost:8080/api/tutorials

Create and configure project in OpenXava

What we need to do now is copy the configuration files generated by OpenXava when a new project is created to Spring Boot project, so the project must have the same configurations as the Spring Boot project.

Create a new OpenXava project using OpenXava Studio.

getting-started_en020.png

Project name: openxava-spring-boot-example

Group id: com.example

Add the MySQL8 dialect in the persistence.xml file located in src/main/resources/META-INF.

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>

Configure the context.xml file in the src/main/webapp/META-INF folder. Comment out the HSQLDB resource and use the MySQL resource instead. Configure the database connection using your URL, username, and password.

Replace application.properties and copy files to the Spring Boot project

Now, let's go back to the project in Spring Tool Suite 4 and proceed to integrate OpenXava into a Spring Boot application. There are several ways to do this, and we will use one of them. First, you need to replace the application.properties file and configure it manually by creating a @Configuration class.
To do this, create a class named AppConfig in the com.example.openxava.spring.boot.example package. Inside the dataSource() method, define the database connection configurations (URL, username, password), entities, and repositories to be scanned, for example:
package com.example.openxava.spring.boot.example;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableJpaRepositories("com.example.openxava.spring.boot.example.repository")
@EnableTransactionManagement
public class AppConfig {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/yourdatabase?useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("");
        return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(dataSource());
        entityManagerFactory.setPackagesToScan("com.example.openxava.spring.boot.example.model");
        entityManagerFactory.setPersistenceUnitName("com.example.openxava.spring.boot.example.model");
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabase(Database.MYSQL);
        vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL8Dialect");
        entityManagerFactory.setJpaVendorAdapter(vendorAdapter);
        entityManagerFactory.setJpaProperties(hibernateProperties());
        return entityManagerFactory;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL8Dialect");
        return properties;
    }

}
Remove the application.properties file and ensure that the services continue to function correctly by accessing  http://localhost:8080/api/tutorials
Add extends SpringBootServletInitializer in the OpenxavaSpringBootExampleApplication class located in the com.example.openxava.spring.boot.example package.
@SpringBootApplication
public class OpenxavaSpringBootExampleApplication extends SpringBootServletInitializer {

	public static void main(String[] args) throws Exception {
		SpringApplication.run(OpenxavaSpringBootExampleApplication.class, args);

	}
}
Now, configure the pom.xml file by adding packaging war, OpenXava dependency (you can use the latest available version, from 7.1.1). The finalName is optional, but we will use it in this example.
<packaging>war</packaging>

<dependencies> ... <dependency> <groupId>org.openxava</groupId> <artifactId>openxava</artifactId> <version>7.1.1</version> </dependency> </dependencies>

<build> <finalName>openxavaspringbootexample</finalName> </build>
Finally, copy the following files and folders from the OpenXava project to the Spring Boot project under the same path:

Your project should now have these changes implemented.

Run application

Before running the application, make sure to set up JAVA_HOME and perform the following steps:

Copy the openxavaspringbootexample.war file from the target folder to the webapp folder of your installed Tomcat 9. Then, run startup.bat from the bin folder.

Open your browser and access the following URL to open the OpenXava application (login with admin/admin if you haven't done the optional step):
http://localhost:8080/openxavaspringbootexample

To take advantage of the views generated by OpenXava with the project entities, you can now perform CRUD operations.

Verify that the Spring Boot project services are working by accessing: http://localhost:8080/openxavaspringbootexample/api/tutorials

You will notice that instead of returning JSON, it is returning XML. This is due to a bug in the JasperReports library that comes with OpenXava. We will temporarily resolve this issue in this example by excluding a dependency from that library. However, it is not recommended to do this in a production environment.

<dependency>
    <groupId>org.openxava</groupId>
    <artifactId>openxava</artifactId>
    <version>7.1.1</version>
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </exclusion>
    </exclusions>
</dependency>

If you want to remove "openxavaspringbootexample" from the URL, simply rename the  springbootdatajpa.war file to ROOT.war in the webapps folder. With this change, you can access the OpenXava application using http://localhost:8080 and the services as before using http://localhost:8080/api/tutorials