SpringBoot学习1之快速入门
2017-05-10 16:25
615 查看
SpringBoot学习1之快速入门
微信公众号:IT程序猿进化史
环境准备
Java环境一个趁手的IDE(本文使用Eclipse)
Maven环境(可以自己安装或者使用IDE内置的maven)
创建一个简单的Spring-boot应用
首先创建一个maven项目创建好的项目结构如图所示
并在其pom.xml中引入如下所示:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 | <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&g 4000 t; <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> |
将App.java文件改写成如下所示
1234567891011121314151617 | 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@RestControllerpublic class App { @RequestMapping("/") public String greeting() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(App.class, args); }} |
运行成功后,访问浏览器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 注解是用来处理请求地址映射的注解,可以用在类或方法上,如果用在类上,则表示所有该类中的响应方法都是以该地址作为父路径。123456789101112 | @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} |
1234 | @RequestMapping("/hello/{id}")public String hello(@PathVariable("id") int id) { return return “Hello :” + id;} |
在实际应用中,不同的Http方法,在处理过程中可能也要进行分别处理,@RequestMapping也提供了相应的解决办法
12345678 | @RequestMapping(value = "/hello", method = RequestMethod.GET)public String helloGet() { return "hello get";}@RequestMapping(value = "/hello", method = RequestMethod.POST)public String helloPost() { return "hello post";} |
模板渲染
当使用@Controller注解某个类时,某个URL路由方法中的返回值字符串,将不再直接返回到前端进行显示,而是将寻找名为该字符串的模板进行渲染12345678 | @Controllerpublic class App { @RequestMapping("/hello/{id}") public String hello(@PathVariable("id") String id, Model model) { model.addAttribute("id", id); return "hello" }} |
在这里我们使用thymeleaf模板引擎进行渲染。
首先在pom.xml中引入依赖
1234 | <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency> |
12345678910 | <!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是<p>标签的属性)。
微信公众号:IT程序猿进化史
详情请访问我的博客: 点击打开链接
相关文章推荐
- spring boot框架学习1-认识spring boot和快速入门
- springboot学习(二) Spring Boot 快速入门
- spring boot框架学习1-认识spring boot和快速入门
- Spring Boot 基础知识学习(一)——快速入门
- spring boot框架学习1-认识spring boot和快速入门
- Spring Boot官方文档学习——Spring Boot快速入门
- Spring boot学习教程之快速入门篇
- Spring学习笔记(3)----------Spring快速入门
- Spring Boot快速入门之Hello World
- (Spring Boot框架)快速入门
- Spring Boot 快速入门
- spring-boot快速入门
- 使用 Spring Boot 快速构建 Spring 框架应用---学习笔记
- Spring Boot 快速入门
- Spring Boot 快速入门
- Spring Boot快速入门
- Spring Boot 快速入门
- Spring Boot 快速入门
- SpringBoot初识和快速入门(一)
- Spring Boot 快速入门