SpringBoot IntelliJ创建简单的Restful接口
使用SpringBoot快速建服务,和NodeJS使用express几乎一模一样,主要分为以下:
1.添加和安装依赖 2.添加路由(即接口) 3.对路由事件进行处理
同样坑的地方就是,祖国的防火墙太过强大,必须要把maven换成国内镜像源,阿里云的速度就非常快。
1.通过spring initializr创建项目

2.填好工程名,一路next到底
3.新建controller的package,新建class "DemoServer",填入以下内容:
import java.util.Date;
import java.util.HashMap;
import java.util.Map; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.ResponseBody; @RestController
@RequestMapping(value = "/index")
public class NewServer {
@RequestMapping
public String index() {
return "spring boot server";
} // @RequestParam 简单类型的绑定,可以出来get和post
@RequestMapping(value = "/get")
public HashMap<String, Object> get(@RequestParam String name) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title", "hello world");
map.put("name", name);
return map;
}
@RequestMapping("/json")
@ResponseBody
public Map<String,Object> json(){
Map<String,Object> map = new HashMap<String,Object>();
map.put("name","Ryan");
map.put("age","18");
map.put("sex","man");
return map;
}
}
4.会提示缺少 org.springframework.web的依赖,在pom.xml中添加该依赖:
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
这里有个IntelliJ非常傻bi的bug,你不去修改pom.xml,他默认竟然不会安装里面的依赖。
所以你需要,先剪切<dependencies>标签里的内容,保存。然后重新复制进去,再保存,然后就会自动添加依赖了。。。真他妈简直了。。
如果IntelliJ安装依赖有问题,可以通过cd到工程目录,使用mvn install进行安装。
5.新建model的pakcage,写一个model类User
package com.example.model; /**
* Created by shenzw on 31/10/2016.
*/
import java.util.Date; public class User {
private int id;
private String name;
private Date date; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Date getDate() {
return date;
} public void setDate(Date date) {
this.date = date;
}
}
通过在DemoServer中添加路由,可以在路径中获取参数,然后拼装一个User对象,快速序列化成JSON返回
// @PathVariable 获得请求url中的动态参数
@RequestMapping(value = "/get/{id}/{name}")
public User getUser(@PathVariable int id, @PathVariable String name) {
User user = new User();
user.setId(id);
user.setName(name);
user.setDate(new Date());
return user;
}
6.点击运行,或选择DemoServer这个Class右键RUN
http://localhost:8080/index 可以访问到这个服务
http://localhost:8080/index/json 会返回json
http://localhost:8080/index/get?name=123 可以进行GET请求获取
http://localhost:8080/index/get/123/xiaoming 可以在路径中传递参数
demo:
https://github.com/rayshen/spring-boot-demo/tree/master/spring-boot-demo-2-1
SpringBoot IntelliJ创建简单的Restful接口的更多相关文章
- Intellij创建简单Springboot项目
Intellij创建简单Springboot项目 第一步:选择创建新项目——file-new-project 第二步:选择项目类型——Spring Initializr-next 第三步:输入项目信息 ...
- [转]简单识别 RESTful 接口
本文描述了识别一个接口是否真的是 RESTful 接口的基本方法.符合 REST 架构风格的接口,称为 RESTful 接口.本文不打算从架构风格的推导方面描述,而是从 HTTP 标准的方面 ...
- springboot结合jwt实现基于restful接口的身份认证
基于restful接口的身份认证,可以采用jwt的方式实现,想了解jwt,可以查询相关资料,这里不做介绍~ 下面直接看如何实现 1.首先添加jwt的jar包,pom.xml中添加依赖包: <de ...
- 使用springboot实现一个简单的restful crud——01、项目简介以及创建项目
前言 之前一段时间学习了一些springboot的一些基础使用方法和敲了一些例子,是时候写一个简单的crud来将之前学的东西做一个整合了 -- 一个员工列表的增删改查. 使用 restful api ...
- 利用WCF创建简单的RESTFul Service
1):用VS2013创建一个WCF的工程,如下图所示: 2):我们来看一下默认状态下的config文件内容,这里的内容我们会再后续的步骤中进行修改 <?xml version="1.0 ...
- cxf的使用及安全校验-02创建简单的客户端接口
上一篇文章中,我们已经讲了如果简单的创建一个webservice接口 http://www.cnblogs.com/snowstar123/p/3395568.html 现在我们创建一个简单客户端接口 ...
- 使用springboot实现一个简单的restful crud——03、前端页面、管理员登陆(注销)功能
前言 这一篇我们就先引入前端页面和相关的静态资源,再做一下管理员的登陆和注销的功能,为后续在页面上操作数据做一个基础. 前端页面 前端的页面是我从网上找的一个基于Bootstrap 的dashboar ...
- 使用springboot实现一个简单的restful crud——02、dao层单元测试,测试从数据库取数据
接着上一篇,上一篇我们创建了项目.创建了实体类,以及创建了数据库数据.这一篇就写一下Dao层,以及对Dao层进行单元测试,看下能否成功操作数据库数据. Dao EmpDao package com.j ...
- 使用webpy创建一个简单的restful风格的webservice应用
下载:wget http://webpy.org/static/web.py-0.38.tar.gz解压并进入web.py-0.38文件夹安装:easy_install web.py 这是一个如何使用 ...
随机推荐
- html5上传图片(二)一解决部分手机拍照上传图片转向问题
本以为解决跨域上传后没有问题了,不成想被测试找出一个问题,那就是在手机上拍照上传后图片会旋转.很头痛,不过没有办法,问题还是需要解决的.在查阅了一系列资料后我找到了相应的解决方案,利用exif.js获 ...
- no identity found Command /usr/bin/codesign failed with exit code 1 报错解决方法
stackoverflow 的解决方法是 xcode->preference->account->view detail -> refresh the provisioning ...
- Android Studio使用时源码到处报红色警告,运行时又没错
转载地址:http://www.07net01.com/program/2016/04/1452749.html [摘要:正在AS上开辟时,碰到那个题目,翻开全部的Java源文件,右边一起标赤色,找没 ...
- 【代码笔记】iOS-页面之间的跳转效果
一,工程图. 二,代码. RootViewController.m -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { ...
- CAS Client集群环境的Session问题及解决方案
[原创申明:文章为原创,欢迎非盈利性转载,但转载必须注明来源] 之前写过一篇文章,介绍单点登录的基本原理.这篇文章重点介绍开源单点登录系统CAS的登录和注销的实现方法.并结合实际工作中碰到的问题,探讨 ...
- SQL Server 2008 R2:快速清除日志文件的方法
本例,快速清理“students”数据库的日志,清理后日志文件不足1M. USE [master] GO ALTER DATABASE students SET RECOVERY SIMPLE WIT ...
- 前端编辑工具之VSCode
因为前段时间看了瞬息之间的一篇文章编辑器背后的程序观, 里面只提到了Visual studio. 我想想可能是因为非.Net开发者,所以不知道Visual sutdio code这个工具.来看看V ...
- 【js】初入AJAX
AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 JavaScript和 ...
- 基于pcDuino-V2的无线视频智能小车 - UBUNTU系统上的gtk编程
详细的代码已经上传到git网站:https://github.com/qq2216691777/pcduino_smartcar
- 使用Guid做主键和int做主键性能比较
使用Guid做主键和int做主键性能比较 在数据库的设计中我们常常用Guid或int来做主键,根据所学的知识一直感觉int做主键效率要高,但没有做仔细的测试无法 说明道理.碰巧今天在数据库的优化过程中 ...