1.运行环境

开发工具:intellij idea

JDK版本:1.8

项目管理工具:Maven 3.2.5

2.Maven Plugin管理

pom.xml配置代码:

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>spring-boot-helloWorld</groupId>
<artifactId>spring-boot-helloWorld</artifactId>
<version>1.0-SNAPSHOT</version> <!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent> <dependencies>
<!-- Spring Boot web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot test依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

3.Application启动类编写

 package com.goku.demo;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; /**
* Created by nbfujx on 2017/11/20.
*/
// Spring Boot 应用的标识
@SpringBootApplication
@ServletComponentScan
public class DemoApplication { public static void main(String[] args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(DemoApplication.class,args);
}
}

4.ExampleController控制器编写

 package com.goku.demo.controller;

 import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* Created by nbfujx on 2017-11-20.
*/
@RestController
public class ExampleController { @RequestMapping("/")
public String helloWorld()
{
return "helloWorld";
} @RequestMapping("/{str}")
public String helloWorld(@PathVariable String str)
{
return "hello"+ str;
}
}

5.使用MockMvc对Controller进行测试

添加相关单元测试

 package test.com.goku.demo.controller;

 import com.goku.demo.DemoApplication;
import com.goku.demo.controller.ExampleController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.junit.Assert.*; /**
* Created by nbfujx on 2017-11-20.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class)//这里的Application是springboot的启动类名。
@WebAppConfiguration
public class ExampleControllerTest { @Autowired
private WebApplicationContext context;
private MockMvc mvc; @Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.webAppContextSetup(context).build();//建议使用这种
} @Test
public void helloWorld() throws Exception {
String responseString = mvc.perform(get("/") //请求的url,请求的方法是get
.contentType(MediaType.APPLICATION_JSON) //数据的格式
.param("pcode","root") //添加参数
).andExpect(status().isOk()) //返回的状态是200
.andDo(print()) //打印出请求和相应的内容
.andReturn().getResponse().getContentAsString(); //将相应的数据转换为字符串
System.out.println("--------返回的json = " + responseString);
} @Test
public void helloWorld1() throws Exception {
String responseString = mvc.perform(get("/str") //请求的url,请求的方法是get
.contentType(MediaType.APPLICATION_JSON) //数据的格式
.param("pcode","root") //添加参数
).andExpect(status().isOk()) //返回的状态是200
.andDo(print()) //打印出请求和相应的内容
.andReturn().getResponse().getContentAsString(); //将相应的数据转换为字符串
System.out.println("--------返回的json = " + responseString);
} }

6.在页面上运行

http://localhost:8080/

http://localhost:8080/str

7.GITHUB地址

https://github.com/nbfujx/springBoot-learn-demo/tree/master/spring-boot-helloWorld

spring-boot的helloWorld详解的更多相关文章

  1. Spring Boot 之 HelloWorld详解

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “以前是人放狗看家,现在是狗牵着人散步” — 随笔 一.Spring Boot 自述 世界上最好 ...

  2. Spring Boot 集成 FreeMarker 详解案例(十五)

    一.Springboot 那些事 SpringBoot 很方便的集成 FreeMarker ,DAO 数据库操作层依旧用的是 Mybatis,本文将会一步一步到来如何集成 FreeMarker 以及配 ...

  3. Spring Boot 自定义日志详解

    本节内容基于 Spring Boot 2.0. 你所需具备的基础 什么是 Spring Boot? Spring Boot 核心配置文件详解 Spring Boot 开启的 2 种方式 Spring ...

  4. Spring Boot 之 Redis详解

    Redis是目前业界使用最广泛的内存数据存储. Redis支持丰富的数据结构,同时支持数据持久化. Redis还提供一些类数据库的特性,比如事务,HA,主从库. REmote DIctionary S ...

  5. Spring Boot启动流程详解(一)

    环境 本文基于Spring Boot版本1.3.3, 使用了spring-boot-starter-web. 配置完成后,编写了代码如下: @SpringBootApplication public ...

  6. spring boot容器启动详解

    目录 一.前言 二.容器启动 三.总结 =======正文分割线====== 一.前言 spring cloud大行其道的当下,如果不了解基本原理那么是很纠结的(看见的都是约定大于配置,但是原理呢?为 ...

  7. spring boot application.properties详解

    附上最新文档地址:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-propertie ...

  8. Spring Boot Tomcat配置详解

    参数配置容器 server.xx开头的是所有servlet容器通用的配置,server.tomcat.xx开头的是tomcat特有的参数,其它类似. 所有参数绑定配置类:org.springframe ...

  9. Spring Boot属性配置文件详解

    相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁 ...

  10. Spring Boot启动流程详解

    注:本文转自http://zhaox.github.io/java/2016/03/22/spring-boot-start-flow 环境 本文基于Spring Boot版本1.3.3, 使用了sp ...

随机推荐

  1. R语言rvest包网络爬虫

    R语言网络爬虫初学者指南(使用rvest包) 钱亦欣 发表于 今年 06-04 14:50   5228 阅读   作者 SAURAV KAUSHIK 译者 钱亦欣 引言 网上的数据和信息无穷无尽,如 ...

  2. QQ空间分享网址

    现在大部分网站都在每个界面设计了分享这个功能,但还是有的网页没有(比如 B 站只能分享具体的视频).在原来的 QQ 空间分享的地方已经找不到法自己创建分享.上网一搜有分享的接口,可这个接口是给开发者用 ...

  3. Using Xmanager to connect to remote CentOS 7 via XDMCP

    Gnome in CentOS 7 tries to use local hardware acceleration and this becomes a problem when trying to ...

  4. 5.1properties属性

    需求: 将数据库连接参数单独配置在db.properties文件中,只需在SqlMapconfig.xml中加载db.properties的属性值. 在SqlMapconfig.xml中就不需要对数据 ...

  5. CodeChef Count Substrings

    Count Substrings   Problem code: CSUB   Submit All Submissions   All submissions for this problem ar ...

  6. MapReduce数据格式化------<一>

    引言: 我们知道:在MapReduce程序的Map阶段,需要有数据输入,而由于数据往往大小不规则,所以在数据输入Mapper之前,需要根据数据的特点和业务逻辑对数据进行格式化.这一步的格式化被称为:I ...

  7. wangEditor 文本编辑器

    参考:https://www.cnblogs.com/Scholars/p/8968838.html 下载:http://www.wangeditor.com/ 前端代码: <script ty ...

  8. 攻防世界--csaw2013reversing2

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/3f35642056324371b913687e770e97e6.exe 1.准备 打开 ...

  9. UI库colorui的使用————小程序

    UI库colorui的使用----小程序 把colorui文件放到你的小程序中 包含文件: icon.wxss+main.wxss+components(文件夹里有icon和一些组件)+animati ...

  10. jq中的ajax传参

        一.   jq中的Ajax传参有两种           1.通过url地址来传参    2.通过data来传递参数 1. url来传递参数 function GetQuery(id) { | ...