Skip to content

Latest commit

 

History

History

springboot-web-h2

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Getting Started

Create user using curl

	curl -XPOST -H 'Content-Type: application/json' -d '{ "firstName" : "Bill" }' http://localhost:8101/api/users

Retrieve Users

	http://localhost:8101/api/users

Project to use H2 database and JPA

We can setup project initially with JPA or enhance on an existing project to support JPA. To do so, add the starter-data-jpa in pom.xml

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-jpa</artifactId>
	</dependency>

This will allow us to focus on application and leave all the database details to JPA.

Tomcat

Project will load Tomcat and show in logs like for example: Starting Servlet engine: [Apache Tomcat/9.0.52] and serve pages with specified port 8101

resources/application.properties

server.port=8101

H2 dependency

Project is setup to run on H2 database and we can see dependency in pom.xml

	<dependency>
		<groupId>com.h2database</groupId>
		<artifactId>h2</artifactId>
		<scope>runtime</scope>
	</dependency>

	spring.datasource.url=jdbc:h2:mem:testdb
	spring.datasource.driverClassName=org.h2.Driver
	spring.datasource.username=sa
	spring.datasource.password=password
	spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

Thyme dependency

Now, let us add a front end support using Thymeleaf for which we need below section in pom.xml

	<dependency> 
			<groupId>org.springframework.boot</groupId> 
			<artifactId>spring-boot-starter-thymeleaf</artifactId> 
	</dependency>

This allows us to use Thymeleaf tags in html pages that are served. Start by configuring properties required in application.properties

There are examples that show how to invoke the Controller that has a function that returns back a thymeleaf template.

Reference links in index.html: In below example we can call the month calculator url that is mentioned in the mapping of that controller.

<tr>
	<td><a th:href="@{mtCalc}">Month Calculator </td>
	<td>Calculate new Month based on days added. </td>
</tr>

Following is to show Form and template: resources/templates/datecalculator.html

Form in page where action is specified as the Mapping "/dtCalc" defined with the DateController

<form id="frmDt" method="get" action="dtCalc">

</form>

DateController.java

Within the Controller we can find the mapping defined with "/dtCalc" which is referenced in the template.

@GetMapping("/dtCalc")
public String greeting(@RequestParam(name="curDt", required=false, defaultValue="2022-01-01") String name, 
		@RequestParam(name="addDays", required=false, defaultValue="30") String addDays,
		Model model) {

Reference Documentation

For further reference, please consider the following sections:

Guides

The following guides illustrate how to use some features concretely: