JavaRush /Java Blog /Random EN /Spring is not scary, or how to start a WEB server with Sp...
Павел
Level 11

Spring is not scary, or how to start a WEB server with Spring Boot in 5 minutes

Published in the Random EN group
Content:
  1. How to start a WEB server with Spring Boot in 5 minutes
  2. How to connect a database for a student project in 5 minutes
  3. How to populate an h2 database (and a little Hibernate)
  4. How to keep your project concise with Lombok
  5. We continue to fill the database
  6. How to ask a DB question
  7. Review of the topic about lambda expressions and streamAPI
  8. Mapping database responses
  9. A little about spring context
  10. DTO pattern
  11. REST-Controller
  12. Cookies & Headers
  13. Exception Handling in Spring Boot Controllers
  14. How to host a Spring Boot project on Heroku
First, let's create a Maven project . We write the following dependencies in the pom file:
<properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/><!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
<properties> are settings, specifically the Java level is indicated here. <parent> says that all dependencies will be downloaded with the version that matches the specified one (2.2.0.RELEASE). <dependencies> contains the same spring-boot-starter-web that will start. The version is not needed here, since it is specified in <parent> In the java package, you need to create a package: ru.java.rush . In the created package, create the Applications class and fill it with the following content:
package ru.java.rush;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Applications {

    public static void main(String[] args) {
        SpringApplication.run(Applications.class);
    }
}
Let's start the project using the green button. When the logs appear in the console:
2020-12-05 21:05:27.598  INFO 2546 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-12-05 21:05:27.601  INFO 2546 --- [           main] ru.java.java_rush.Applications           : Started Applications in 0.971 seconds (JVM running for 1.446)
This will mean that TomcatWebServer started on port 8080. Let's open a browser and type http://localhost:8080/ Since the server does not send any information, something like this will be displayed:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Dec 05 21:36:10 MSK 2020
There was an unexpected error (type=Not Found, status=404).
No message available
But the server is working: try to stop the application, and then refresh the browser page, and it will say that "Page not found." You've just written a Java application that hosts a web server! The same thing without Spring would take ten times more time, and a thousand nerves! In general, study Spring, don’t be afraid, gradually move towards your goal, and you will succeed! Let's now see how to quickly connect the training database here . The main goal of the series of articles is to show that you should not be afraid to try to learn the Spring framework and Spring boot, it is not as difficult as it might seem. However, without knowledge of JavaCore, and the basic concepts of OOP (class, interface, inheritance, etc.), your time will be wasted. If you don’t have this knowledge, don’t despair, 1 - 2 months of study will fix everything, after you master it, come back here. If you don’t know where to start, then take this: “Java 8. Beginner's Guide. Herbert Schildt". This series of articles will introduce you to the main modern practical topics regarding the Spring framework and Spring boot. The format of each article involves one small example (or an example spanning several articles), a small explanation and links to theory or other examples, and independent search for additional information on the topic is also strongly encouraged. It is expected that at the end of the cycle you will be able to create a small web application based on the Spring framework and Spring boot technologies. To understand what Spring is, read this simple and understandable article right now: Spring for the lazy. Fundamentals, basic concepts and examples with code. . If you are looking for more serious information on the topic, then check out the following list: Translated and not so easy to understand articles from Habr: What is the Spring Framework? From dependency injection to Web MVC Spring MVC: creating websites and RESTful services Translated documentation , I highly recommend checking out what’s there right now Books! First you need to get them; of course, you should read in small portions: Spring in action | Walls Craig (Spring 3) - I recommend starting with this book, reading only the first chapter (since the rest is already outdated), you can search the Internet. There is a partial translation of Spring in action floating around the Internet | Walls Craig (Spring 5) - you can try to use it as a basis, the presentation of the original book is simple and with examples. A good primary source of knowledge option is: Spring 5 for Professionals | Kozmina Yuliana, Harrop Rob - in Russian, you can search on the Internet The book is voluminous and academic, so that after reading two pages you will not be disappointed in it, you must understand this. Personally, I did this: I read some part of the book (sometimes more than once), and then went to look for examples on this topic on the Internet.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION