最近在学习springboot,因此记录下学习过程中一些小例子,防止以后忘记可以,可以随时复习一下。由于工作中使用的开发工具是eclipse,因此使用eclipse开发。
1.创建pom文件
<?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.huan.springboot</groupId> <artifactId>springboot_01_start</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_01_start</name> <description>Spring Boote入门示例</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.创建一个实体类
public class User { private String username; private String password; private int age; private Date birth; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } }
3.创建一个控制层
@RestController @RequestMapping("user") public class UserController { @RequestMapping(value = "get/{id}", method = RequestMethod.GET) public User getUser(@PathVariable("id") int age) { User user = new User(); user.setAge(age); user.setUsername("测试"); user.setBirth(new Date()); user.setPassword("123456"); return user; } @RequestMapping(value = "/del/{id}", method = RequestMethod.DELETE) public String delete(@PathVariable("id") int age) { return "delete" + age; } @RequestMapping(value = "/add", method = RequestMethod.POST) public User add(User user) { return user; } }
4.启动springboot应用程序
@SpringBootApplication @ComponentScan("com.springboot") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
5.启动应用程序
/** * springboot 程序的启动入口 * * @描述 * @作者 huan */ @SpringBootApplication @ComponentScan(basePackages = { "com.huan.springboot" }) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
6.测试:
注意:
1、@SpringBootApplication是一个组合注解,默认会扫描它所在的包以及子包,而我的controller不在它默认的子包下,因此需要加载@ComponentScan注解配置
2、@RestController也是一个组合注解,在此注解修饰的类下,它下方所有的方法都是以json的格式返回。
3、默认情况下不需要自己进行配置,springboot会帮我们进行自动配置,若有不符合的地方也是可以进行修改的。
4、程序启动的默认端口是8080,上下文路径是 /
5、开发工具可以使用
idea : 默认支持springboot插件
sts : spring 的一个集成eclipse的开发工具,也是可以开发springboot程序
eclipse : 需要安装一个插件(springsource-tool-suite-3.8.2.RELEASE-e4.5.2-updatesite.zip),且eclipse的版本需要>=4.5.2
进入springboot的官网,使用 https://start.spring.io/ 创建项目。
相关推荐
Springboot入门到精通.pdf Springboot入门到精通.pdf
springboot入门,springboot-demo.
springboot入门成功案例,帮组快速了解springboot,初学者快速入门的极佳选择。
SpringBoot入门,包括配置,JBDC,事务,JPA,Mybatis,NoSql,跨域访问,日志,监控
springboot入门实例 博客地址:https://blog.csdn.net/u010476739/article/details/98313834
虽然 Spring 的组件代码是轻量级的,但它的配置却是重量级的。一开 始,Spring 用 XML 配置,而且是很多 XML 配 置。Spring 2.5 引入了基于注解的组件扫描,这消除了大量针对应用 程序自身组件的显式 XML 配置。...
springboot入门代码;快速应用spring boot依赖和配置spring boot插件
SpringBoot入门基础以及应用
SpringBoot!SpringBoot!SpringBoot!SpringBoot!SpringBoot!SpringBoot!
主要介绍了在idea下创建springboot项目的过程以及入门知识
Spring boot目前分为两大版本系列,1.x系列和2.x系列,目前Spring Boot最新正式版为2.1.2.RELEASE(2019.01) 如果是使用eclipse,推荐安装Spring Tool Suite (STS)插件 ...SringBoot入门案例
25、SpringBoot入门及原理(狂神)
SpringBoot入门
SpringBoot入门级项目, 结合我写的文章很容易理解,希望能与大家一起学习
SpringBoot入门学习笔记,根据B站霍格沃兹公开课视频整理,如有错误,望指正。内容包括:springboot的优势,创建springboot项目的方法及其项目结构,项目小实例源码。
SpringBoot入门_实战 SpringBoot整合项目 入门案例
SpringBoot入门基础.ppt
Springboot入门到精通(超详细文档)。 一篇文章带你快速了解!
看B站视频项目总结的系列文档,仅供个人学习使用