赵走x博客
首页
书籍
软件
工具
古诗词
搜索
登录
3、Getting Started
2、Spring Quickstart Guide
1、简介
2、Spring Quickstart Guide
资源编号:76568
Java
Spring Boot官方文档
热度:33
来源:https://spring.io/quickstart
# Step 1: Start a new Spring Boot project Use [start.spring.io](https://start.spring.io/) to create a “web” project. In the “Dependencies” dialog search for and add the “web” dependency as shown in the screenshot. Hit the “Generate” button, download the zip, and unpack it into a folder on your computer. The Spring Initializr (start.spring.io) being asked to generate a simple Spring Boot ‘Web’ project. Hitting the ‘generate’ button will create an archive归档文件 of a project which you can unpack and use to get started. ![图片.png](https://img.handsomemark.com/2020/04/30/d80dc6de-8aad-11ea-8aed-0242ac120007.png) Projects created by start.spring.io contain Spring Boot, a framework that makes Spring ready to work inside your app, but without much code or configuration required. Spring Boot is the quickest and most popular大众的 way to start Spring projects. # Step 2: Add your code Open up the project in your IDE and locate the DemoApplication.java file in the src/main/java/com/example/demo folder. Now change the contents of the file by adding the extra method and annotations 注释 shown in the code below. You can copy and paste the code or just type it. ``` package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @GetMapping("/hello") public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { return String.format("Hello %s!", name); } } ``` This is all the code required to create a simple “Hello World” web service in Spring Boot. The hello() method we’ve added is designed 设计好的to take a String parameter called name, and then combine 组合this parameter with the word "Hello" in the code. This means that if you set your name to “Amy” in the request, the response would be “Hello Amy”. The @RestController annotation tells Spring that this code describes 描述 an endpoint that should be made available over the web 在web上提供. The @GetMapping(“/hello”) tells Spring to use our hello() method to answer requests that get sent to the http://localhost:8080/hello address. Finally, the @RequestParam is telling Spring to expect 期望 a name value in the request, but if it’s not there, it will use the word “World” by default. # Step 3: Try it Let’s build and run the program. Open a command line (or terminal) and navigate to the folder where you have the project files. We can build and run the application by issuing the following command: ### MacOS/Linux: ``` ./mvnw spring-boot:run ``` ### Windows: ``` mvnw spring-boot:run ``` You should see some output that looks very similar to this: ![图片.png](https://img.handsomemark.com/2020/04/30/91c292a8-8aae-11ea-be01-0242ac120007.png) The last couple 一对 of lines here tell us that Spring has started. Spring Boot’s embedded 嵌入式Apache Tomcat server is acting as a webserver and is listening for requests on localhost port 8080. Open your browser and in the address bar at the top enter http://localhost:8080/hello. You should get a nice friendly response like this: The browser screen displaying the default ‘hello world’ output from our application. ![图片.png](https://img.handsomemark.com/2020/04/30/bc839ee2-8aae-11ea-80da-0242ac120007.png)