Spring Boot Hello World (restful Interface) Examples


Spring Boot Integration Tutorial


This article, we will write a hello world web application, with spring boot sole function is to access the interface return hello world string. The rear end of the front separation trend, where direct writing using java, few pages are generally written using java rear end of the interface, with the front end of the write page JS (available vue / react frame, etc.), so we choose interface mode write hello world example.

Create a project

Open Eclipse, if you have not set up a development environment, can refer spring boot build a development environment (Eclipse) to build, select the menu: File -> New -> Project...to bring up the "New Project" dialog box, select Spring Starter Project, as shown below, click Next:

image

Modify some input information, click Next:

image

The following dialog box is used to automatically generate configuration-dependent (pom.xml), == In the dialog box, check the Web (not miss) ==, click FinishCreate a new project

image

Barring unforeseen circumstances, the project will create success.

Project Directory Structure

image

Project directory Description:

  • src - the source directory
    • main - Code
      • java - Java code directory
      • resources - resources directory profiles, etc.
    • test - test code
  • target - the compiled output directory

pom.xml file in the root directory of the project is dependent maven package configuration file.

Update dependencies

New projects, the proposed update dependencies. As shown in the left side of Eclipse directory window, the mouse pointer to the project folder, right-click pop-up menu, choose: Maven -> Update project...pop-up dialog box check our project, to begin the update.

image

To view the updated schedule can be viewed in the progress window below the Eclipse interface:

image

write the code

Once the update is complete dependencies, add HelloController.java file. Expand the src directory, point to the directory hello right-click pop-up menu, choose: New -> Fileadd HelloController.java file

image

Add code in HelloController.java in:

package com.qikegu.hello;

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

@RestController
public class HelloController {
    
    @RequestMapping(value="/hello", method=RequestMethod.GET)
    public String hello() {
            
        return "Hello World!";
    }
}

Code has two notes to explain:

  • RestController this annotation indicates that the Control class provides an interface Restful
  • This annotation map RequestMapping request url, meaning here is: is the Get method, url path / hello

Note: To further understand these two notes Restful and what is the interface, you can view the relevant information.

run

Eclipse left side of the screen to bring up the context menu item, choose: Run As -> Spring Boot Appto run the program:

image

Open the browser and go to: http: // localhost: 8080 / hello (spring boot is the default port 8080)

image

to sum up

The complete code

Guess you like

Origin www.cnblogs.com/haibianren/p/11684534.html