Giter Site home page Giter Site logo

java-spring-boot-api's Introduction

Java Spring Boot Demo

Intro

In case you wanna specialize on backend development, Java is a natural choice. It is still widely used in companies.

There are many more programming language options for backend development. This video here nicely summarizes the Pro and Cons of using Java instead of other prominent web development languages like PHP, Python, C# or Node (even though from 2019, it is still very accurate): https://www.youtube.com/watch?v=mCOSgYilwNs.

However: Before getting into Java, you will have to say "YES" to the so called "Object Oriented Programming" (OOP). In case you have no idea what that is, getting right into the Spring framework is probably not a good first step. It makes sense to train it a bit in the language you are currently using (e.g. in JavaScript you can do OOP to), then trying out your first Java program (without a framework) using some classes calling each other and get a little bit used to Java terminology. Only afterwards it makes sense to then dive into the Spring experience.

For Backend API Development the Framework "Spring" is by far the most commonly used. But Spring is a super complex multi purpose framework. So a pre-configured version of Spring exists, called "Spring Boot" which gives you a lot of basic libraries and configurations out of the box, to e.g. start quickly with creating a RESTful API with operations against a database of your choice.

In order to checkout the fundamentals of the Spring Framework you might checkout this helpful high level videos first:

Prerequisites

Setup

Create configuration / environment

In Spring Boot environment variable configuration is not done with .env files like e.g. in the Node or PHP world.

Instead so called "properties" files are used.

You have a default "application.properties" file, with relevant config settings. It is located in the folder src/main/resources of a Spring Boot App.

Ideally this file loads its values from the ENVIRONMENT.

As an Example - the application.properties from this app:

spring.profiles.active=local

# in case no port is set in environment => use default 5000
server.port=${PORT:5000}

spring.datasource.url=${DATABASE_URL:""}
spring.datasource.username=${DATABASE_USERNAME:""}
spring.datasource.password=${DATABASE_PASSWORD:""}

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update


jwt.secret=${JWT_SECRET:"myHolySecret"}

The Environment variables are the one inside the ${...} blocks (you can compare it to process.env.YOUR_ENV_VAR in Node)

This way the app will be easily deployable to production, grabbing all its config like database from the Server Environment / Config vars.

But in order to setup your environment for LOCAL DEVELOPMENT, you have two choices:

  • set the same environment variables on your system (not recommended - it would get shared with other apps)
  • set the local environment by a separate properties file that will overwrite (!) the settings from application.properties. That local environment file must get added to .gitignore to keep it out of Git

So we will choose the second option.

Create local properties / environment file

Create a file "application-local.properties" inside Folder src/main/resources.

This file pretty much has the same purpose as your .env file from other languages. Provide an Environment Config for local development.

Add the following config variables and replace the values with the details from your database.

server.port=5000

spring.datasource.url=jdbc:postgresql://localhost:5432/your_database
spring.datasource.username=postgres
spring.datasource.password=yourDbPw

jwt.secret=myLocalSecretio

Loading the local environment file

Now how to load that file instead of application.properties?

In order that Spring Boot loads the given file on startup, you need to launch the app with a so called "profile".

"Profile" is the term Spring Boot uses for "Environment". So don't get confused here ;)

Profiles are used to serve different ENVIRONMENTS with different configurations, e.g. a different Database connection.

You can read more on profiles here: https://www.baeldung.com/spring-profiles

Depending on the profile that you run your app with, a matching properties file (if it exists) with the config values for that environment will get loaded automatically (and will overwrite the settings in application.properties!)

The configuration file just must contain the profile name.

Examples:

  • Profile "local" => Configuration filename: application-local.properties
  • Profile "development" => Configuration filename: application-development.properties

So the profile name determines the file name that Spring will look for and will then autodetect and load the file.

In your application.properties the "local" profile is already pre-configured to be picked on startup:

spring.profiles.active=local

So you just need to provide the file "application-local.properties" in src/main/resources and your env for local development should get loaded.

Spring will automatically OVERWRITE all settings in application.properties with the ones in your local properties file!

Production environment

For production you could create another profile, e.g. "prod" or "production".

But actually you do not need that, because on production your "application.properties" file will kick in and load the configuration from the environment. So you just need to setup all environment variables on your server and it should work out of the box.

In production, e.g. when you deploy to Heroku, Spring Boot will try to lookup an application-local.properties first.

But if it does not find it, it will default to loading the values from application.properties. Which loads the configuration from the environment / config vars you set on the server.

So please: Keep the application-local.properties out of git to prevent accidental deployment! (it is already added to .gitignore file in this project, so usually nothing to do here)

Running the app

If you have configured your VScode with the Spring Boot Extension, you can easily navigate to the main entry file /src/main/java/com/example/springbootdemo/DemoApplication.java and open it.

A little "Play" icon should appear at the top right of your file.

Click that and check if the app starts up!

Alternatively you can also launch the app from the terminal:

./mvnw spring-boot:run

Under the hood now the build tool "Maven" will launch, install and bind all necessary dependencies and then starts your app. Hopefully successfully :)

Deployment

You can deploy a Spring boot app for free e.g. on Heroku: https://devcenter.heroku.com/articles/deploying-spring-boot-apps-to-heroku

In order to deploy this app, you do not need to run the spring-boot CLI, because you already have the app created. So you can skip that part

Also please setup the Postgres Addon in your created Heroku App like shown in the Heroku Devcenter article above! This way you get a Postgres Database out of the box

In case you already have your own SQL database: Please provide the Environment / Config Vars in your Heroku Dashboard of your App => Tab Settings:

DATABASE_URL=yourDbUrl
DATABASE_USERNAME=yourDbUser
DATABASE_PASSWORD=yourDbPw

Heroku will make these variables available and Spring Boot will loadthose into you application.properties file on startup.

And that is it.

Usually you do not need to configure anything extra on Heroku. It should run out of the box.

Enjoy!

Resources / Learning

In order to understand the big picture of Spring, this video series - even though a bit dated - is still excellent for all general concepts:

In order to understand how you work with databases with Spring Boot there is a lot of jargon out there (JDBC, JPA, Hibernate, Spring Boot Data JPA). This video here explains how it all works together nicely:

And here a great article that shows how to develop a Spring Boot REST API with Database CRUD (Create / Read / Update / Delete):

Authentication using JWT:

java-spring-boot-api's People

Contributors

losrobbos avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.