SpringBoot实战(一)Hello World

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

1.首先介绍极简的helloworld工程

1).打开Eclipse,新建Maven工程,修改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.enn</groupId>
	<artifactId>SpringBoot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-boot-sample-mysql</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.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-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</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)、编制Application.java存于myFirstProject\src\main\java\com\example\myFirstProject下

[java]  view plain  copy
  1. package com.example.myFirstProject;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5.   
  6.   
  7. @SpringBootApplication  
  8. public class Application {  
  9.   
  10.     public static void main(String[] args) {  
  11.         SpringApplication.run(Application.class, args);  
  12.     }  
  13. }  
3)、编制Example.java,存于myFirstProject\src\main\java\com\example\myFirstProject下

[java]  view plain  copy
  1. package com.example.myFirstProject;  
  2.   
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
  4. import org.springframework.web.bind.annotation.PathVariable;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import org.springframework.web.bind.annotation.RestController;  
  7.   
  8. @RestController  
  9. @EnableAutoConfiguration  
  10. public class Example {  
  11.       
  12.     @RequestMapping("/")  
  13.     String home() {  
  14.         return "Hello World!";  
  15.     }  
  16.       
  17.     @RequestMapping("/hello/{myName}")  
  18.     String index(@PathVariable String myName) {  
  19.         return "Hello "+myName+"!!!";  
  20.     }  
  21. }  

4).再次点击“OK”按钮,在Eclipse的Console中开始打印如图5


5)、打开浏览器,输入http://localhost:8080,显示如图6

6)、在浏览器中,输入http://localhost:8080/hello/SpringBoot

2、使用官网样例创建工程

一、在浏览器中打开http://start.spring.io/,如图

Artifact中输入spring-boot-sample-helloworld,点击“Switch to the full version.”,勾选"web",然后点击“ Generate Project alt +”按钮,把文件保存到本地某个位置

二、下载文件导入eclips

1、解压下载的文件到某个文件夹;

2、在eclips中导入工程file->import->Import Existing Maven Projects-->Select Maven projects-->finish

3、在eclips中运行工程,正确则入图

三、添加HelloController

1、在包com.example中点击右键,选择new->class,再在Name中输入HelloController

2、在类上面条件声明@RestController

3、增加方法

[java]  view plain  copy
  1. @RequestMapping("/")  
  2.     public String helloworld(){  
  3.         return "Hello world!";  
  4.     }  
  5.       
  6.     @RequestMapping("/hello/{name}")  
  7.     public String hellName(@PathVariable String Name){  
  8.         return "Hello "+Name;  
  9.     }  

4、保存Ctrl+S

5、自动添加引用 Ctrl+SHift+O

6、整个HelloController文件如下

[java]  view plain  copy
  1. package com.example;  
  2.   
  3. import org.springframework.web.bind.annotation.PathVariable;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RestController;  
  6.   
  7. @RestController  
  8. public class HelloController {  
  9.   
  10.     @RequestMapping("/")  
  11.     public String helloworld(){  
  12.         return "Hello world!";  
  13.     }  
  14.       
  15.     @RequestMapping("/hello/{name}")  
  16.     public String hellName(@PathVariable String name){  
  17.         return "Hello "+name;  
  18.     }  
  19. }  
7、启动测试

在浏览器中依次输入

http://localhost:8080/

http://localhost:8080/hello/上帝




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值