Spring Boot 之 RESTfull API简单项目的快速搭建(一)
1.创建项目
2.创建 controller
IndexController.java
package com.roncoo.example.controller; 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 com.roncoo.example.bean.User; @RestController
@RequestMapping(value = "/index")
public class IndexController { @RequestMapping
public String index() {
return "hello world";
} @RequestMapping(value = "get")
public Map<String, String> get(@RequestParam String name) {
Map<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("value", "hello world"); return map;
} @RequestMapping(value = "find/{id}/{name}")
public User get(@PathVariable int id, @PathVariable String name) {
User u = new User();
u.setId(id);
u.setName(name);
u.setDate(new Date());
return u;
}
}
3.创建 bean -- 实体类 User
User.java
package com.roncoo.example.bean; 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;
} }
4.测试
SpringBootDemo21ApplicationTests.java
package com.roncoo.example; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import com.roncoo.example.controller.IndexController; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootDemo21ApplicationTests { private MockMvc mvc; @Before
public void befor() {
this.mvc = MockMvcBuilders.standaloneSetup(new IndexController()).build();
} @Test
public void contextLoads() throws Exception {
RequestBuilder req = get("/index");
mvc.perform(req).andExpect(status().isOk()).andExpect(content().string("hello world"));
} }
5.项目目录
Spring Boot 之 RESTfull API简单项目的快速搭建(一)的更多相关文章
- Spring Boot 之 RESTfull API简单项目的快速搭建(三)
1.运行打包后的项目 java -jar spring-boot-demo-2-1-0.0.1-SNAPSHOT.jar
- Spring Boot 之 RESTfull API简单项目的快速搭建(二)
1.打包 -- Maven build 2.问题 [WARNING] The requested profile "pom.xml" could not be activated ...
- 48. spring boot单元测试restfull API【从零开始学Spring Boot】
回顾并详细说明一下在在之前章节中的中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建 ...
- spring boot 实现RESTFull API
- Spring Boot入门系列(二十)快速打造Restful API 接口
spring boot入门系列文章已经写到第二十篇,前面我们讲了spring boot的基础入门的内容,也介绍了spring boot 整合mybatis,整合redis.整合Thymeleaf 模板 ...
- 使用Spring Boot和Gradle创建AngularJS项目
Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 ...
- Spring Boot微服务电商项目开发实战 --- 基础配置及搭建
根据SpringBoot实现分布式微服务项目近两年的开发经验,今天决定开始做SpringBoot实现分布式微服务项目的系列文章,帮助其他正在使用或计划使用SringBoot开发的小伙伴们.本次系列文章 ...
- 喜大普奔,两个开源的 Spring Boot + Vue 前后端分离项目可以在线体验了
折腾了一周的域名备案昨天终于搞定了. 松哥第一时间想到赶紧把微人事和 V 部落部署上去,我知道很多小伙伴已经等不及了. 1. 也曾经上过线 其实这两个项目当时刚做好的时候,我就把它们部署到服务器上了, ...
- 两个开源的 Spring Boot + Vue 前后端分离项目
折腾了一周的域名备案昨天终于搞定了. 松哥第一时间想到赶紧把微人事和 V 部落部署上去,我知道很多小伙伴已经等不及了. 1. 也曾经上过线 其实这两个项目当时刚做好的时候,我就把它们部署到服务器上了, ...
随机推荐
- MARQUEE 字符滚动条效果
<MARQUEE scrolldelay =“100” direction=“up " > 滚动文字或图像 </MARQUEE> 说明: – scrolldelay: ...
- PHP工程师笔试题
PHP工程师笔试题 提示:请将答案写在另外一张空白纸上,并在30分钟内完成. PHP 请写出include.require.include_once.require_noce的区别. include是 ...
- 使用PHP生成二维码图像
1.PHP生成二维码图像的类QRcode http://www.phper.org.cn/?post=128 QRcode是用于生成二维条形码的开放源码 (LGPL) 库.提供 API 创建条码图像. ...
- Oracle简单的备份和恢复-导入和导出-目录
ylbtech-Oracle:Oracle简单的备份和恢复-导入和导出-目录 Oracle安全运行离不开良好的备份和恢复机制,因为我们不是DBA.所以我们也就不过多的讲解DBA的备份和恢复.作为程序员 ...
- 附1 踩过的jedis的一些坑
1.java.lang.Long to java.lang.B]类型转换异常 解决方案:归还资源部分,使用jedis.close() 2.jedis数组越界异常 解决方案:版本太低,升到2.8.0
- 不得不知的ES6十大特性
ES6(ECMAScript2015)的出现,无疑给前端开发人员带来了新的惊喜,它包含了一些很棒的新特性,可以更加方便的实现很多复杂的操作,提高开发人员的效率. 本文主要针对ES6做一个简要介绍. 主 ...
- ES6 主要的新特性
本文基于lukehoban/es6features ,同时参考了大量博客资料,具体见文末引用. ES6(ECMAScript 6)是即将到来的新版本JavaScript语言的标准,代号harmony( ...
- 操作系统重点双语阅读 - 上下文切换 Context Switch
The context is represented in the PCB of the process. It includes the value of the CPU registers, th ...
- ssh tunnel
https://peppoj.net/2012/10/tunnel-http-traffic-encrypted-using-polipo-and-ssh/ --------------------- ...
- 如何在编辑器里添加CSS或JS代码
//编辑器里代码模式下的代码 <scripttype="text/javascript"> //my code.... </script> //编辑器里可视 ...