Achim Mertens avatar

Hamurabi Spring Boot Server

achimmertens

Published: 02 Feb 2022 › Updated: 02 Feb 2022Hamurabi Spring Boot Server

Hamurabi Spring Boot Server

Hello Java and game developers and interested others,

at the moment I am learning Java. My colleagues and me are in a bootcamp and have a recap-time this week.
I want to create a game, that runs on a server and maybe later can be attached to Hive. This game is called Hamurabi.

The first part of it is to create a Spring Boot Server, which can handle some data.
So I started from scratch and here is the very first minimal viable product, a server which accepts http-requests:

Setting up Spring Boot, JPA and H2

I configure the server at start.spring.io:
grafik.png

I downloaded the zipfile and opened it in IntelliJ. I added the dependencies for Jackson into pom.xml:

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>

I have created the following two files data-sql and schema.sql:

grafik.png

I have adjusted application.properties:

grafik.png

So, after starting HammurabiApplication.java our Spring Database is up and running:

grafik.png

I can log into the SQL-Console via http://8099:h2-console

grafik.png

We find our data, which was automatically created (with the right settings in appliocation.properties) with the following command:

grafik.png

Setting up the data connection between Repository and Service

Now lets bring these repository data data to the service side on REST Spring:
We create the following packages: configuration, controller, dto, entity, repository and services:

grafik.png

In the folder "dto" we create the dataclass "Userlogdto" with the following content:

grafik.png

Please also create a default constructor to avoid the error message:
Ensure that org.chary.dto.Userlogdto has a non-private no-argument constructor.

Now we copy this file and paste it into the "entity" folder. We rename it to "UserlogEntity". We add "@Entity" and "@Table(name="Userlog") before the beginning of the class Userlog. And the @Id has to be set:

grafik.png

Don't forget to set the default Constructor to avoid the error message:
No default constructor for entity:

grafik.png

Now we create the interface "UserlogRepository" in the package "Repository"and we extend it with the class JPARepository<UserlogEntity, String>:
grafik.png

Now we build the Rest-Controller class and add the annotations
@RestController
@CrossOrigin("*")
@RequestMapping("/userlog")

grafik.png

Before we continue in the controller, we create the UserlogService interface. In this we create the method template
public List getAllUserlogs();

grafik.png

Now we add this Interface into the controller:

grafik.png
Now let's create the Interface-implementation in the service package and make it public to Spring by adding the annotation @Service:

grafik.png

Modelmapper

To get the objects, that are in the entity-class into the dto-class, we use modelmapper. Therefore we add it into the pom.xml

        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.5</version>
        </dependency>

Then we add the following to the UserlogServiceImpl-class:

grafik.png
We need to make modelmapper public to Spring. For this we create in the configuration package the ModelMapperConfiguration class.

grafik.png

I mixed up account and username. So I refactored it. But now it works:

grafik.png

If you want to see the complete code, click here. There are just some names exchanged.


This is the very early beginning of my project. I hope I get the time and the knowledge to fulfil my dream to create, at least a small, game that uses HIVE to play it.

Regards, Achim

Leave Hamurabi Spring Boot Server to:

Written by

Love also your next but one and create more than you destroy.

Read more #java posts


Best Posts From Achim Mertens

We have not curated any of achimmertens's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Achim Mertens