`
jinnianshilongnian
  • 浏览: 21424367 次
  • 性别: Icon_minigender_1
博客专栏
5c8dac6a-21dc-3466-8abb-057664ab39c7
跟我学spring3
浏览量:2403176
D659df3e-4ad7-3b12-8b9a-1e94abd75ac3
Spring杂谈
浏览量:2996176
43989fe4-8b6b-3109-aaec-379d27dd4090
跟开涛学SpringMVC...
浏览量:5630413
1df97887-a9e1-3328-b6da-091f51f886a1
Servlet3.1规范翻...
浏览量:257187
4f347843-a078-36c1-977f-797c7fc123fc
springmvc杂谈
浏览量:1592662
22722232-95c1-34f2-b8e1-d059493d3d98
hibernate杂谈
浏览量:248795
45b32b6f-7468-3077-be40-00a5853c9a48
跟我学Shiro
浏览量:5845661
Group-logo
跟我学Nginx+Lua开...
浏览量:697478
5041f67a-12b2-30ba-814d-b55f466529d5
亿级流量网站架构核心技术
浏览量:779851
社区版块
存档分类
最新评论

Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

阅读更多

扫一扫,关注我的公众号 

 

我的新书 购买地址

 

之前有一篇《5分钟构建spring web mvc REST风格HelloWorld》介绍了普通方式开发spring web mvc web service。接下来看看使用spring boot如何快速构建一个。

 

Spring Boot使我们更容易去创建基于Spring的独立和产品级的可以”即时运行“的应用和服务。支持约定大于配置,目的是尽可能快地构建和运行Spring应用。

 

之前我们创建基于Spring的项目需要考虑添加哪些Spring依赖和第三方的依赖。使用Spring Boot后,我们可以以最小化的依赖开始spring应用。大多数Spring Boot应用需要很少的配置即可运行,比如我们可以创建独立独立大Java应用,然后通过java -jar运行启动或者传统的WAR部署。其也提供了命令行工具来直接运行Spring脚本(如groovy脚本)。也就是说Spring Boot让Spring应用从配置到运行变的更加简单,但不对Spring本身提供增强(即额外的功能)。

 

目的:

让所有Spring开发变得更快,且让更多的人更快的进行Spring入门体验,提供“starter” POM来简化我们的Maven配置(也就是说使用Spring Boot只有配合maven/gradle等这种依赖管理工具才能发挥它的能力),不像以前,构建一个springmvc项目需要进行好多配置等

开箱即用,快速开始需求开发而不被其他方面影响(如果可能会自动配置Spring)

 

提供一些非功能性的常见的大型项目类特性(如内嵌服务器、安全、度量、健康检查、外部化配置),如可以直接地内嵌Tomcat/Jetty(不需要单独去部署war包)

绝无代码生成,且无需XML配置

 

我的构建环境

JDK 7

Maven 3

Servlet3容器 

 

创建项目

首先使用Maven创建一个普通Maven应用即可,不必是web的。

 

添加Spring Boot相关POM配置

在pom.xml中添加如下配置

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>0.5.0.BUILD-SNAPSHOT</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable JAR -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <!-- Allow access to Spring milestones and snapshots -->
    <!-- (you don't need this if you are using anything after 0.5.0.RELEASE) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>

继承spring-boot-starter-parent后我们可以继承一些默认的依赖,这样就无需添加一堆相应的依赖,把依赖配置最小化;spring-boot-starter-web提供了对web的支持,spring-boot-maven-plugin提供了直接运行项目的插件,我们可以直接mvn spring-boot:run运行。

 

实体

package com.sishuok.entity;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 13-12-22
 * <p>Version: 1.0
 */
public class User {
    private Long id;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (id != null ? !id.equals(user.id) : user.id != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }
}

 

控制器

package com.sishuok.controller;

import com.sishuok.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 13-12-22
 * <p>Version: 1.0
 */
//@EnableAutoConfiguration
@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/{id}")
    public User view(@PathVariable("id") Long id) {
        User user = new User();
        user.setId(id);
        user.setName("zhang");
        return user;
    }

    //public static void main(String[] args) {
    //    SpringApplication.run(UserController.class);
    //}

}

 

运行  

第一种方式

通过在UserController中加上@EnableAutoConfiguration开启自动配置,然后通过SpringApplication.run(UserController.class);运行这个控制器;这种方式只运行一个控制器比较方便;

第二种方式

通过@Configuration+@ComponentScan开启注解扫描并自动注册相应的注解Bean

package com.sishuok;

import com.sishuok.controller.UserController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 13-12-22
 * <p>Version: 1.0
 */
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

到此,一个基本的REST风格的web应用就构建完成了。

 

地址栏输入http://localhost:8080/user/1即可看到json结果。

 

 

如果大家查看其依赖,会发现自动添加了需要相应的依赖(不管你用/不用),但是开发一个应用确实变得非常快速,对于想学习/体验Spring的新手,快速建立项目模型等可以考虑用这种方式。当然如果不想依赖这么多的jar包,可以去掉parent,然后自己添加依赖。 

 

欢迎加入spring群134755960进行交流。

 

参考

https://github.com/spring-projects/spring-boot

 

28
5
分享到:
评论
30 楼 wangyudong 2017-11-27  
由Spring Boot实现的微服务需要有比较好的工具去测试RESTful API,很多REST Client是不支持自动化测试RESTful API,也不支持自动生成API文档.
之前习惯用一款名字为 WisdomTool REST Client,支持自动化测试RESTful API,输出精美的测试报告,并且自动生成精美的RESTful API文档。
轻量级的工具,功能却很精悍哦!

https://github.com/wisdomtool/rest-client

Most of REST Client tools do not support automated testing.

Once used a tool called WisdomTool REST Client supports automated testing, output exquisite report, and automatically generating RESTful API document.

Lightweight tool with very powerful features!

https://github.com/wisdomtool/rest-client
29 楼 Liuxianglin 2017-11-08  
数据库去哪了?
28 楼 a3618392 2017-04-10  
还不错,我也推荐一个SpringBoot干货系列的博客:
这个博主写的干货教程很棒,很适合新手脱坑
http://tengj.top
27 楼 恋无涯 2017-01-22  
呵呵6666 写道
spring boot基础学习系列文章:http://www.roncoo.com/article/detail/124661
广告打的蛮远的,有点烂
26 楼 w846492130_1 2016-12-30  
为啥我总觉得 spring boot  是 nodejs+soa 使用 tcp/ip 通信的变种呢
25 楼 清风飞扬1988 2016-10-28  
点击链接加入群【Spring Boot Cloud  交流】:http://jq.qq.com/?_wv=1027&k=40umhEo
24 楼 呵呵6666 2016-10-11  
spring boot基础学习系列文章:http://www.roncoo.com/article/detail/124661
23 楼 flystarfly 2016-08-24  
zyj_daan 写道
为什么我运行main函数,eclipse就直接要结束进程,什么错误信息都没有?先mvn install了的

我也是install成功了的,但是还是没法运行main函数
22 楼 zyj_daan 2016-08-10  
为什么我运行main函数,eclipse就直接要结束进程,什么错误信息都没有?先mvn install了的
21 楼 zkw0710 2016-08-09  
例子非常好,spring-boot中的tomcat怎样才能绑定域名
20 楼 oaoutoaout 2016-03-24  
田梦桦 写道
体验下零配置的感觉。

看看playframework吧,让你真正体验到什么是飞一般的感觉!
19 楼 liuwuhen 2016-03-14  
赞一个!
18 楼 田梦桦 2015-12-29  
体验下零配置的感觉。
17 楼 lihuanjin 2015-08-27  
16 楼 cactu6 2015-08-21  
入门的好教程,这篇写得也不错,推荐给大家:http://tianmaying.com/tutorial/spring-mvc-quickstart
15 楼 rox 2015-07-23  
涛&哥的秀发呢?
14 楼 ruyi0127 2015-07-01  
涛。哥群进不去
13 楼 javaHacker2010 2015-05-19  
能否把controller和app分开不同的包?
12 楼 string2020 2015-03-13  
@RestController   和 @Controller   有什么区别?
11 楼 yanguoyingliu 2014-12-25  
前面弄错了,先用maven install下载所需jar,然后在运行main函数。

相关推荐

Global site tag (gtag.js) - Google Analytics