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. 也曾经上过线 其实这两个项目当时刚做好的时候,我就把它们部署到服务器上了, ...
随机推荐
- Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
问题: java连接不上redis. 异常信息: Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.ne ...
- spring-boot:run启动时,指定spring.profiles.active
Maven启动指定Profile通过-P,如mvn spring-boot:run -Ptest,但这是Maven的Profile. 如果要指定spring-boot的spring.profiles. ...
- Material Designer的低版本兼容实现(七)—— Rectange Button
矩形按钮是我们很常用的控件,让其拥有5.0动效必须自定义控件.本文讲解的控件是参考: https://github.com/shark0017/MaterialDesignLibrary 一.放入布局 ...
- ClipDrawable属性介绍
ClipDrawable代表从其它位图上截取一个"图片片段",XML中的根元素为<clip.../>,截取的方向由clipOrientation控制 <?xml ...
- Windows10系统.NET Framework 3.5离线安装方法
Win10技术预览版给用户们带来很多新功能的同时,也给用户们带来了不害臊的麻烦与问题.其中.NET Framework 4.5是系统预装的,但是在Win10技术预览版中的部分应用需要.NET Fram ...
- 论文分享|《Universal Language Model Fine-tuning for Text Classificatio》
https://www.sohu.com/a/233269391_395209 本周我们要分享的论文是<Universal Language Model Fine-tuning for Text ...
- XGBoost浅入浅出
http://wepon.me/ XGBoost风靡Kaggle.天池.DataCastle.Kesci等国内外数据竞赛平台,是比赛夺冠的必备大杀器.我在之前参加过的一些比赛中,着实领略了其威力,也取 ...
- 【R】函数-字符处理函数
- 阿里巴巴Java开发规约插件全球首发!(转)
https://mp.weixin.qq.com/s?__biz=MzI0NTE4NjA0OQ==&mid=2658355901&idx=1&sn=3169172bfc6819 ...
- Structured Streaming编程向导
简介 Structured Streaming is a scalable and fault-tolerant stream processing engine built on the Spark ...