A few days ago, I was having a conversation with a colleague who is working on a web project using Spring Boot and Hibernate.
几天前,我和一个正在使用Springboot 和hibernate 开发项目的同事进行交谈。
I was amazed that someone could still be using Java technologies for web application these days, when there are a lot of web frameworks out there that are easier to work with, providing nearly the same performance.
我很惊讶,有人仍然使用java技术做web应用,并且坚持了这么多天,因为有很多web框架可以选择,提供了机会相同的性能。
I myself started my web development in JavaServer Pages (JSP), before switching to PHP over five years ago due to the flexibility and little time required to craft a functional application in PHP, compared to JSP.
我开始我的网站开发的时候使用了jsp语言,五年前是切换到了PHP语言,因为它相比于jsp,PHP在编写功能性应用程序上更加灵活并且所需要时间很短。
Nonetheless, this colleague of mine is more concerned about enterprise acceptability, standard, integration, and security.
尽管如此,我的这位同事更关心企业接受程度,标准,集成,安全等方面。
So, I decided to take a look at Spring Boot to see what it has to offer. While it seemed quite confusing at the beginning, it is not as difficult as I thought.
所以,我决定看一下springboot 看它能提供什么。虽然一开始看起来很难以理解,但是实际上并不像我想的那么难。
One thing I don’t like about it is the annotations. Thankfully, I don’t need to memorize all of it, I just need to know their functions and have the annotations cheats sheet on my desktop.
有一件事我不喜欢它的注解,谢天谢地,我并不需要全部记住他们,我仅仅需要去了解他们的功能,并且在我的桌面上放了注解秘籍簿。
To cut the story short, having looked at the documentation and some sample codes online, I was able to build a REST API, even though it is not as fast as I could have done in Laravel.
简单来说,看这个文档,并且在线查阅样例diam,我就可以去创建一个rest 挨批,尽管并没有我在Laravel框架中那么快。
In this tutorial, I’m going to show you how you can build a REST API in Spring Boot.
在这个教程中,我将会为你展示如何在SpringBoot中去构建一个rest api.
What We’ll Be Building
我们要构建什么
We will be building a REST API for blog posts. It allows one to search for a blog, fetch all blogs, get a single blog, create a blog, update, and delete an existing blog.
我们将要在这篇博客中构建一个rest api 。一个允许被用户搜索的博客,获取所有的博客,获取单个博客,创建、更新删除一个已经存在的博客。
Requirements
要求
- Good understanding of Java programming language.
- Basic knowledge of Maven.
- 对Java程序语言有很好的理解。
- 对maven的基础知识有所了解。
Tools Needed
需要的工具
- Java Development Kit (JDK) 1.7+
- IntelliJ IDEA, NetBeans, Eclipse, or Spring Tool Suite (I will be using IntelliJ IDEA).
- jdk 1.7以上
- xxxx开发工具的名字(我在这里将使用IDEA这个工具)
Getting Started
开始
There are at least three ways of creating a Spring Boot application (i.e. Spring Boot initializer, command-line tool (CLI), and Maven with IDE). I will only cover how to create the application using Maven.
Open IntelliJ IDEA and click “Create New Project”.
有至少三种方式去创建一个springboot的应用。(Springboot initializer, CLI, IDEA中的maven)。我将只接受如何去使用maven去创建这个应用。
打开IDEA工具并且点击创建
Select “Maven Project”.
选择 maven项目
Fill in the required information.
GroupId
is your unique organizational name (in most cases, people use their company’s reverse domain name such as “com.mycompany”), ArtifactId
is the unique name of the project, and Version
is the version number of the project.
填写所需信息
GroupIds是你组织的唯一名字(大多数情况下,他们去使用他们的公司的反向域名,例如com.mycompany),ArticactId是项目的唯一名字,Version是项目的版本号。
After completing the setup, you should have an empty project with a Maven config file pom.xml
.
在完成这些设置之后,你应该有一个空的项目,并且带有一个pom.xml的maven的配置文件。
As Maven is a dependency manager, you can add all the project dependencies to it.
For now, all we need is Spring Boot. Copy and paste the code below into the pom.xml
file
maven 是一个依赖管理者,你可以向它添加所有的项目依赖。
现在呢,我们所需要的只是一个SpringBoot,复制并且黏贴如下代码到pom.xml文件中去。
- The
<parent>
tag tells Maven to inherit the properties ofspring-boot-starter-parent
, such as port number, configs, etc. All these are provided by Spring. - 这<parent>标签告诉maven 继承
spring-boot-starter-parent所有属性,例如端口号,配置等,这些都是由Spring提供的。
- The
<dependencies>
tag contains all the project dependencies. For now, we only have one dependency,spring-boot-starter-web
. In a complete application, there could be more, e.g. MySQL, socket, JSON library, etc. - 这个<dependencies>标签包含了这个项目中的所有依赖,现在我们仅有一个依赖,spring-boot-starter-web。在一个完成的应用中,可能会有更多,例如MySQL, socket, json 的库。等等。
- The
<build>
contains build plugins, such asspring-maven-plugin
. - 这个<build> 包含了构建的插件,例如spring-maven-plugin插件。
Update the Maven repository to download the dependencies.
更新maven仓储去下载这些依赖。
We have now finished the configurations. Let’s start coding
现在我们完成了配置,让我们开始编码吧
The Main Class
主方法类
For any Java application to run, you need to have at least one “Main class”. Create a class and name it MainApplicationClass.java
.
对于任何Java应用的运行,你都需要去有至少一个main类 ,创建一个类并且将它命名为MainApplicationClass.java
.
Make sure your class is inside a package, or it might not run. Also, take note of my package name (me.salisuwy
) which might be different from yours.
确保你的类在包中,否则它可能没法运行起来。另外,记下我的包名(me.salisuwy),有可能跟你的不一样。
Add the @SpringBootApplication
annotation to the class to make it a Spring Boot application. You can now run the application.
在类中加入注解@SpringBootApplication
,让他成为一个Springboot的应用。现在你可以运行这个应用了。
Congratulations, you’ve created a Spring Boot application.
恭喜,你已经创建了一个Springboot的应用了。
Hey, wait a minute. Why is localhost:8080
showing an error? Well, that is because we don’t have a controller to handle our HTTP requests.
嘿,等一下,为什么localhost:8080显示错误呢?好吧,这是因为我们没有controller去处理我们的HTTP请求。
Controller, the C in MVC
controller,就是MVC中的c
The controller handles all incoming HTTP requests from the user and returns an appropriate response. In some languages, route files map the HTTP requests to the appropriate controller. Let’s create a Controller.
这个controller处理了所有用户发来的HTTP的请求,并且返回了一个适当的响应。在一些语言中路由文件映射HTTP请求到对应的controller中,让我们创建一个Controller控制器。
Create BlogController.java
.
创建BlogController.java类。
@RestController
annotation tells Spring that this class is a controller.- @RestController 注解告诉Spring这个类是一个控制器
@RequestMapping(“/”)
annotation means that any request (GET
,POST
,PUT
, etc.) to the rootURL(/)
will be handled by theindex()
method. The response is of typeString
.- @RequestMapping("/") 注解意味着所有的请求(get,post, put, 等)去访问根目录的URL(/)都会被这index()方法所处理。响应体是String字符类型的。
Other variants of the @RequestMapping
annotation are @GetMapping
, @PostMapping
, etc. for handling GET
and POST
requests, respectively.
其他的@RequestMapping 注解的变形写法是@GetMapping, @PostMapping, 等,分别用来处理get和post请求。