Eclipse + Spring boot development tutorial

I. Preface

This article is for reference https://www.cnblogs.com/lsdb/p/9783435.html Learn to organize your study notes.
Configuration information:
Eclipse IDE for Enterprise Java Developers(Version: 2019-09 R (4.13.0))
Java 1.8

2, Install the Spring plug-in

Open Eclipse, select Help - > Eclipse marketplace, enter spring in Find, click Go to search, and select Install Spring Tools 4

After checking all, click Confirm

Select accept, click Finish, and when the progress bar in the lower right corner of Eclipse reaches 100% (it needs to wait for a while), the restart option will pop up. Click restart

3, New Spring Boot project

  1. Open the menu file - > New - project and select Spring Starter Project
  2. Fill in the project information and click Next
  3. Select Spring Web under Web, click Next, and then click Finish

  4. The project directory structure after creation is as follows:

4, Implement REST requests

  1. Create package: com.example.demo.model and create a new class: Test
package com.example.demo.model;
 
public class Test {
 
        private final long id;
        private final String content;
 
        public Test(long id, String content) {
                this.id = id;
                this.content = content;
        }
 
        public long getId() {
                return id;
        }
 
        public String getContent() {
                return content;
        }
}
  1. Create package: com.example.demo.control, new class: TestController
package com.example.demo.controller;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.model.Test;

@RestController
public class TestController {

	private static final String template = "Hello, %s!";
	private final AtomicLong counter = new AtomicLong();

	@RequestMapping("/test")
	public Test dealTest(@RequestParam(value = "name", defaultValue = "World") String name) {
		return new Test(counter.incrementAndGet(), String.format(template, name));
	}

	@RequestMapping("/test1")
	public Test dealTest1(@RequestParam(value = "name", defaultValue = "World") String name) {
		return new Test(counter.incrementAndGet(), String.format(template, name));
	}
}

When finished, the directory structure should look like this:

  1. Function

Select demo1, right-click run as - > spring boot app, and you can see that the Console window outputs the log information

Enter in browser http://localhost:8080/test , see the following results

  1. Implement hot deployment

In the current project, if we make any changes to the project file, we need to manually re run it if we want to make it effective. For example, if we change the requested repair from test to modify, we need to manually re run it if we want the change to take effect.

It's very good if the system can be re run after modification and saving. This is called hot deployment, and spring boot can realize hot deployment through simple configuration.

For the first generation, open the pom.xml file in the root directory of the project, and add the following contents in the < dependencies > < dependencies > tab:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>


If you check Build Automatically under the menu Project, you can automatically rebuild the Project as soon as there is a modification. When you need to pay attention to it, you need to close the existing running Project after the modification, and then restart Eclipse.

Published 4 original articles, won praise 0, visited 35
Private letter follow

Tags: Spring Eclipse Java REST

Posted on Tue, 21 Jan 2020 05:49:20 -0800 by xander85