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
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.
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.
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;
}
}
@SpringBootApplication
public class OpenxavaSpringBootExampleApplication extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(OpenxavaSpringBootExampleApplication.class, args);
}
}
<packaging>war</packaging>
<dependencies>
...
<dependency>
<groupId>org.openxava</groupId>
<artifactId>openxava</artifactId>
<version>7.1.1</version>
</dependency>
</dependencies>
<build>
<finalName>openxavaspringbootexample</finalName>
</build>
Your project should now have these changes implemented.
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