【SpringBoot教程二】:RESTfull API簡單專案的快速搭建
阿新 • • 發佈:2019-02-18
今天 這麼遲了補一篇 SpringBootRESTfull API簡單專案的快速搭建
看第一篇文章。建立工程
編輯pom.xml
簡單解釋一下上面的配置<?xml version="1.0" encoding="UTF-8"?> <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>com.lyw</groupId> <artifactId>springboot02</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot02</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
首先看看Spring-boot-start-web包的依賴
可以看到 其實這個包就是一些 日誌包 還有內建Tomcat JSON包 SpringMVC/Web包 這也就差不多解釋了為什麼直接跑起來了
其實這個就是可以理解為簡單封裝了一下Spring 使得我們配置更加便捷。
spring-boot-devtools的引入,其依賴包的學習
這個包 包含一些Spring的核心包。。。
spring-boot-maven-plugin 最後還加入了 一個SpringBoot的Maven外掛
開始寫程式碼。。。
Springboot2Application.java
User.javapackage com.lyw; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Springboot02Application { public static void main(String[] args) { SpringApplication.run(Springboot02Application.class, args); } }
package com.lyw.bean; import java.util.Date; /** * Title: User Description: 實體類 Company: blog.csdn.net/lu1005287365/ * * @author L lulu */ public class User { private Integer id; private String name; private Date date; private char sex; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } }
UserControoler.java
package com.lyw.controller;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lyw.bean.User;
@RestController
@RequestMapping(value = "/user")
public class UserController {
@RequestMapping
public String index() {
return "Hello BeiJing";
}
@RequestMapping(value = "/getMap")
public Map<String, Object> getThisMap(String msg) {
Map<String, Object> map = new HashMap<>();
map.put("Name", "LYW");
map.put("Sex", "大老爺們");
map.put("Message", msg);
return map;
}
@RequestMapping(value = "/getUser/{name}/{sex}")
public User getUser(@PathVariable String name, @PathVariable char sex) {
User user = new User();
user.setId(12);
user.setName(name);
user.setDate(new Date());
user.setSex(sex);
return user;
}
}
執行專案:
直接執行main方法或者使用maven命令: spring-boot:run
上面就是執行的結果。。
解釋
@RestController註解:看看原始碼 其實就是 又封裝了一層 將@Controller和@RsponseBody兩個註解合在一起
這樣一來應該對SpringBoot 也有了初步的認識。。。並且會簡單的使用