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简单项目的快速搭建(一)的更多相关文章

  1. Spring Boot 之 RESTfull API简单项目的快速搭建(三)

    1.运行打包后的项目 java -jar spring-boot-demo-2-1-0.0.1-SNAPSHOT.jar

  2. Spring Boot 之 RESTfull API简单项目的快速搭建(二)

    1.打包 -- Maven build 2.问题 [WARNING] The requested profile "pom.xml" could not be activated ...

  3. 48. spring boot单元测试restfull API【从零开始学Spring Boot】

    回顾并详细说明一下在在之前章节中的中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建 ...

  4. spring boot 实现RESTFull API

  5. Spring Boot入门系列(二十)快速打造Restful API 接口

    spring boot入门系列文章已经写到第二十篇,前面我们讲了spring boot的基础入门的内容,也介绍了spring boot 整合mybatis,整合redis.整合Thymeleaf 模板 ...

  6. 使用Spring Boot和Gradle创建AngularJS项目

    Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 ...

  7. Spring Boot微服务电商项目开发实战 --- 基础配置及搭建

    根据SpringBoot实现分布式微服务项目近两年的开发经验,今天决定开始做SpringBoot实现分布式微服务项目的系列文章,帮助其他正在使用或计划使用SringBoot开发的小伙伴们.本次系列文章 ...

  8. 喜大普奔,两个开源的 Spring Boot + Vue 前后端分离项目可以在线体验了

    折腾了一周的域名备案昨天终于搞定了. 松哥第一时间想到赶紧把微人事和 V 部落部署上去,我知道很多小伙伴已经等不及了. 1. 也曾经上过线 其实这两个项目当时刚做好的时候,我就把它们部署到服务器上了, ...

  9. 两个开源的 Spring Boot + Vue 前后端分离项目

    折腾了一周的域名备案昨天终于搞定了. 松哥第一时间想到赶紧把微人事和 V 部落部署上去,我知道很多小伙伴已经等不及了. 1. 也曾经上过线 其实这两个项目当时刚做好的时候,我就把它们部署到服务器上了, ...

随机推荐

  1. C#学习笔记:预处理指令

    C#和C/C++一样,也支持预处理指令,下面我们来看看C#中的预处理指令. #region 代码折叠功能,配合#endregion使用,如下: 点击后如下: 条件预处理 条件预处理可以根据给出的条件决 ...

  2. log4j1 修改FileAppender解决当天的文件没有日期后缀

    直接上代码: /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license ...

  3. 替换NSUserDefaults的方案

    替换NSUserDefaults的方案 效果 源码 https://github.com/YouXianMing/iOS-Utilities // // BaseValueStorageManager ...

  4. 《Python自然语言处理》

    <Python自然语言处理> 基本信息 作者: (美)Steven Bird    Ewan Klein    Edward Loper 出版社:人民邮电出版社 ISBN:97871153 ...

  5. Visual Studio Code 配置 gcc

    作者:谭九鼎链接:https://www.zhihu.com/question/30315894/answer/154979413来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  6. Chapter 4 -- Throwables

    TODO: rewrite with more examples Guava's Throwables utility can frequently simplify dealing with exc ...

  7. 日历控件My97DatePicker WdatePicker屏蔽 onchange的解决方法

    http://www.cnblogs.com/wan-feng/archive/2013/12/13/3473439.html 受下面文章的启发,使用DatePicker自带的年月日相关的change ...

  8. XML和JSON优缺点

    <1>.XML的优点 A.格式统一,符合标准: B.容易与其他系统进行远程交互,数据共享比较方便.<2>.XML的缺点 A.XML文件庞大,文件格式复杂,传输占带宽: B.服务 ...

  9. ASP.NET MVC:UrlHelper.cs

    ylbtech-funcation-Utility: ASP.NET MVC:UrlHelper.cs 充当表示 ASP.NET Razor 页的类的基类. 1.UrlHelper 类返回顶部 1-1 ...

  10. 聊一聊Spring中的线程安全性

    Spring作为一个IOC/DI容器,帮助我们管理了许许多多的“bean”.但其实,Spring并没有保证这些对象的线程安全,需要由开发者自己编写解决线程安全问题的代码. Spring对每个bean提 ...