70

自己动手搭建一个简易的SpringBoot环境 - 队长给我球。

 6 years ago
source link: https://www.cnblogs.com/JJJ1990/p/8384386.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

自己动手搭建一个简易的SpringBoot环境

什么是springboot?

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

1, 新建一个maven工程

先选择workspace

1063420-20180130110641781-1822836913.png

点击【next】

1063420-20180130111434796-169867536.png

直接默认,再点击【next】

1063420-20180130111545421-133617182.png

填写groupid等~然后【finish】,到这里整个新建工程结束。

2,引入相关的jar包

 1 <parent>
 2        <groupId>org.springframework.boot</groupId>
 3        <artifactId>spring-boot-starter-parent</artifactId>
 4        <version>1.5.8.RELEASE</version>
 5    </parent>
 6    <dependencies>
 7        <dependency>
 8            <groupId>org.springframework.boot</groupId>
 9            <artifactId>spring-boot-starter-web</artifactId>
10        </dependency>
11    </dependencies>

这里说明下看似我们只引用了2个jar包其实里面包含了很多东西,像spring-boot-starter-web 我们通过压缩包打开后

查看里面的pom文件可以看到如下所示的内容,它引用了很多jar像spring的web,还有json的jar包都包含在内了

 1     <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.boot</groupId>
 4             <artifactId>spring-boot-starter</artifactId>
 5         </dependency>
 6         <dependency>
 7             <groupId>org.springframework.boot</groupId>
 8             <artifactId>spring-boot-starter-tomcat</artifactId>
 9         </dependency>
10         <dependency>
11             <groupId>org.hibernate</groupId>
12             <artifactId>hibernate-validator</artifactId>
13         </dependency>
14         <dependency>
15             <groupId>com.fasterxml.jackson.core</groupId>
16             <artifactId>jackson-databind</artifactId>
17         </dependency>
18         <dependency>
19             <groupId>org.springframework</groupId>
20             <artifactId>spring-web</artifactId>
21         </dependency>
22         <dependency>
23             <groupId>org.springframework</groupId>
24             <artifactId>spring-webmvc</artifactId>
25         </dependency>
26     </dependencies>

3,编写程序入口类

 1 package com.springbooot2;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 /**
 7  * Hello world!
 8  *
 9  */
10 @SpringBootApplication
11 public class App 
12 {
13     
14     public static void main(String[] args) throws Exception {
15         SpringApplication.run(App.class, args);
16     }
17 }

这里说明下, @SpringBootApplication 就是为了让spring扫描识别,告诉他我是一个程序入口类。

4,编写请求响应类

 1 package com.springbooot2;
 2 
 3 
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.ResponseBody;
 7  
 8 @Controller
 9 public class FristBlood {
10     @RequestMapping("/FristBlood")
11     @ResponseBody
12     public String hello() {
13         return "dont worry,be happy!<br/><br/> <input type=\"submit\" value=\"ok\" />";
14     }
15 }

这里说明下

@Controller 请求处理控制器类。

@RequestMapping 熟悉spring的都应该不陌生,这是spring的东西,url映射。

@ResponseBody 响应方法,我们的响应信息都会被自动转化为json信息返回给前台页面

到这里整个代码就撸完了,比起我们之前搭建一个ssh或者ssm之类的框架简单了不少,如果我们有那种只需要发送一个邮件啊。或者简单的服务,用springboot可以说很方便了。

5,测试代码

启动程序,打开浏览器,输入:http://localhost:8080/FristBlood 

请求页面响应结果如下图

1063420-20180130113751609-890500808.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK