SpringBoot学习1之快速入门

SpringBoot学习1之快速入门

https://darylliu.github.io/archives/c3b398db.html

微信公众号:IT程序猿进化史

 

环境准备

 

Java环境
一个趁手的IDE(本文使用Eclipse)
Maven环境(能够本身安装或者使用IDE内置的maven)html

建立一个简单的Spring-boot应用

首先建立一个maven项目前端

建立好的项目结构如图所示java

并在其pom.xml中引入以下所示:git

 

1github

2web

3spring

4apache

5json

6浏览器

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>SpringBoot</groupId>

<artifactId>SpringBoot</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>SpringBoot</name>

<url>http://maven.apache.org</url>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.2.5.RELEASE</version>

<relativePath/>

</parent>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

从新构建项目,右键项目->Maven->Update Project

App.java文件改写成以下所示

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

 

package SpringBoot.SpringBoot;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

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

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

@SpringBootApplication

@RestController

public class App {

@RequestMapping("/")

public String greeting() {

return "Hello World!";

}

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}

右键运行该项目(做为一个普通的java application运行,这也是springboot的魅力所在),亦或者输入命令:mvn spring-boot:run 运行

运行成功后,访问浏览器http://localhost:8080/ 便可看到返回的结果

程序的入口是:SpringApplication.run(App.class, args); 其中SpringApplication能够暂时看成一个主类来看,这里先不细说,Spring Boot会判断出这是一个web应用,并启动一个内嵌的Servlet容器,通常默认是Tomcat。

注解说明

@Controller

@Controller 注解的类会被看成一个Controller,专门用来处理不一样请求不一样的URL,从而有针对性的返回内容。

@RestController

@RestController 注解整合了@Controller@ResponseBody。使用了这个注解的类会被看成一个Controller,而Controller中的方法没法返回jsp页面,视图解析器将不起做用,好比返回 return”success”;原本是应该返回到success.jsp页面的,可是如今只会返回一个字符串

上文中的@RestController若是替换成@Controller则会提示找不到名为“Hello World!”的页面。

因此通常不要用@RestController注解修饰某个类,通常在实际使用时,能够选择使用@Controller注解类,在该类的方法中有选择的使用@ResponseBody,这样的话,当某个方法须要返回的是一个视图的时候就不须要添加@ResponseBody,当须要返回的是一个json数据或者Xml等时,能够选择添加@ResponseBody

@RequestMapping

@RequestMapping 注解是用来处理请求地址映射的注解,能够用在类或方法上,若是用在类上,则表示全部该类中的响应方法都是以该地址做为父路径。

 

1

2

3

4

5

6

7

8

9

10

11

12

 

@RestController

@RequestMapping("/root")

public class App {

@RequestMapping("/")

public String hello() {

return " Hello World1!";

}//该方法的访问路径是http://localhost:8080/root

@RequestMapping("/hello2")

public String hello2() {

return "Hello World2!";

}//该方法的访问路径是http://localhost:8080/root/hello2

}

在实际开发中,若是对于不一样Id的用户进行不一样的处理,经常须要在URL中加入变量以进行区分,@RequestMapping也提供相似的方法

 

1

2

3

4

 

@RequestMapping("/hello/{id}")

public String hello(@PathVariable("id") int id) {

return return “Hello :” + id;

}

其中{id}就是一个占位符,能够替换成实际的id,当请求路由到该方法进行处理时,该方法根据注解@PathVariable取出名为”id”的值,并赋予给 int id,这样你在方法中就能够将id做为一个局部变量进行使用

在实际应用中,不一样的Http方法,在处理过程当中可能也要进行分别处理,@RequestMapping也提供了相应的解决办法

 

1

2

3

4

5

6

7

8

 

@RequestMapping(value = "/hello", method = RequestMethod.GET)

public String helloGet() {

return "hello get";

}

@RequestMapping(value = "/hello", method = RequestMethod.POST)

public String helloPost() {

return "hello post";

}

即便两个方法处理的URL是同样的,可是因为访问的http方法不同,因此依然会路由到不一样的方法分别进行处理

模板渲染

当使用@Controller注解某个类时,某个URL路由方法中的返回值字符串,将再也不直接返回到前端进行显示,而是将寻找名为该字符串的模板进行渲染

 

1

2

3

4

5

6

7

8

 

@Controller

public class App {

@RequestMapping("/hello/{id}")

public String hello(@PathVariable("id") String id, Model model) {

model.addAttribute("id", id);

return "hello"

}

}

上述例子中将寻找名为hello的模板进行渲染。

在这里咱们使用thymeleaf模板引擎进行渲染。

首先在pom.xml中引入依赖

 

1

2

3

4

 

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

接下来建立资源文件夹(source folder) src/main/resources,并在该目录下建立普通文件夹templates,并在该文件夹下建立模板文件hello.html

 

1

2

3

4

5

6

7

8

9

10

 

<!DOCTYPE HTML>

<html xmlns:th="http://www.thymeleaf.org">

<head>

<title>Hello SomeOne</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

</head>

<body>

<p th:text="'Hello, ' + ${id} + '!'" />

</body>

</html>

th:text=”‘Hello, ‘ + ${id} + ‘!’” 也就是将咱们以前在@Controller方法里添加至Model的属性id进行渲染,并放入<p>

标签中(由于th:text是<p>标签的属性)。

 

 

微信公众号:IT程序猿进化史

 

 

 

详情请访问个人博客: 点击打开连接