• 项目导入eclipse

先检测是否安装有gradle插件

然后点击  finish 按钮

  • hello world实例

    • Application.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

SpringBootApplication的说明

    • HelloController.java

先创建包controller,再在包下创建Java类HelloController

package com.example.demo.controler;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//注解的解释参考下面
@RestController
public class HelloControler {  //映射路径定义为hello
    @RequestMapping("/hello")
    public String hello() {

        return "hello world!";
    }
}
@Open Declaration   org.springframework.web.bind.annotation.RestController
@Controller
@ResponseBody
@Target(value={TYPE})
@Retention(value=RUNTIME)
@Documented

A convenience annotation that is itself annotated with @Controller and @ResponseBody. 

Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default. 

NOTE: @RestController is processed if an appropriate HandlerMapping-HandlerAdapter pair is configured such as the RequestMappingHandlerMapping-RequestMappingHandlerAdapterpair which are the default in the MVC Java config and the MVC namespace.
Since:4.0Author:Rossen StoyanchevSam Brannen
    • HelloControlerTest.java  (测试用例)

代码可以拷贝DemoApplicationTests.java里的

package com.example.demo.controler;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
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.request.MockMvcRequestBuilders;

import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest//配置注入MockMvc
@AutoConfigureMockMvc
public class HelloControlerTest {
  

  //因为使用的时MVC框架
    @Autowired
    private MockMvc mockmvc;
    @Test
    public void testHello() throws Exception {     //构建MVC的请求,get方法,接受的媒体类型(JSON),断言预期返回(),期望返回内容(字符串和“hello world!”匹配)
        mockmvc.perform(MockMvcRequestBuilders.get("/hello").accept(org.springframework.http.MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andExpect(content().string(equalTo("hello world!")));
    }

}
    • 测试

在HelloControlerTest.java 中,右键 => Run as  => JUnit Test

    ------------6月22日-------------

    经过研究个人感觉还是maven更好用一些,一直都在用IDEA下maven组织的项目。

springboot学习2的更多相关文章

  1. springboot 学习资源推荐

    springboot 是什么?对于构建生产就绪的Spring应用程序有一个看法. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.(这是springboot的官方介绍) 我们为什么要学 ...

  2. Springboot学习记录1--概念介绍以及环境搭建

    摘要:springboot学习记录,环境搭建: 官方文档地址:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/ht ...

  3. SpringBoot学习笔记

    SpringBoot个人感觉比SpringMVC还要好用的一个框架,很多注解配置可以非常灵活的在代码中运用起来: springBoot学习笔记: .一.aop: 新建一个类HttpAspect,类上添 ...

  4. springboot学习(一)——helloworld

    以下内容,如有问题,烦请指出,谢谢 springboot出来也很久了,以前零散地学习了不少,不过很长时间了都没有在实际中使用过了,忘了不少,因此要最近准备抽时间系统的学习积累下springboot,给 ...

  5. SpringBoot学习(3)-SpringBoot添加支持CORS跨域访问

    SpringBoot学习(3)-SpringBoot添加支持CORS跨域访问 https://blog.csdn.net/yft_android/article/details/80307672

  6. Springboot学习07-数据源Druid

    Springboot学习07-数据源Druid 关键字 Druid 前言 学习笔记 正文 1-Druid是什么 Druid是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池.插件框架和SQL解析器 ...

  7. Springboot学习06-Spring AOP封装接口自定义校验

    Springboot学习06-Spring AOP封装接口自定义校验 关键字 BindingResult.Spring AOP.自定义注解.自定义异常处理.ConstraintValidator 前言 ...

  8. Springboot学习05-自定义错误页面完整分析

    Springboot学习06-自定义错误页面完整分析 前言 接着上一篇博客,继续分析Springboot错误页面问题 正文 1-自定义浏览器错误页面(只要将自己的错误页面放在指定的路径下即可) 1-1 ...

  9. Springboot学习04-默认错误页面加载机制源码分析

    Springboot学习04-默认错误页面加载机制源码分析 前沿 希望通过本文的学习,对错误页面的加载机制有这更神的理解 正文 1-Springboot错误页面展示 2-Springboot默认错误处 ...

  10. Springboot学习笔记(六)-配置化注入

    前言 前面写过一个Springboot学习笔记(一)-线程池的简化及使用,发现有个缺陷,打个比方,我这个线程池写在一个公用服务中,各项参数都定死了,现在有两个服务要调用它,一个服务的线程数通常很多,而 ...

随机推荐

  1. AES加密算法在Linux下出现随机加密结果

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  2. 设置Apache监听多个端口

    1.在配置文件httpd.conf中Listen多个端口 Listen localhost:8033    Listen localhost:8083 ....... 2.在配置文件夹下的extra文 ...

  3. 查询redis中没有设置过期时间的key

    #!/bin/sh ## 该脚本用来查询redis集群中,哪些key是没有设置过期时间,对应只需要修改redis的其中一个实例的 host和port ## 脚本会自动识别出该集群的所有实例,并查出对应 ...

  4. typescript 函数(定义、参数、重载)

    代码: // 本节内容 // 1.函数的定义 // 2.参数(可选参数/默认参数/剩余参数) // 3.方法的重载 // js // function add(x,y){ // return x+y ...

  5. leetcode-easy-others-268 Missing Number

    mycode   80.25% class Solution(object): def missingNumber(self, nums): """ :type nums ...

  6. leetcode-easy-math-204 Count Primes-NO

    mycode     time limited class Solution(object): def countPrimes(self, n): """ :type n ...

  7. 初学vue 在做项目时遇到的问题与解决办法(使用element组件)(二)

    表格每行里都有按钮 <el-table-column prop="option" label="操作" align="center" ...

  8. assert断言用法

    使用assert断言是学习python一个非常好的习惯,python assert 断言句语格式及用法很简单.在没完善一个程序之前,我们不知道程序在哪里会出错,与其让它在运行最崩溃,不如在出现错误条件 ...

  9. IFB

    本文翻译自Linux官方IFB文档 IFB(中介功能块设备)是IMQ(中介队列设备)的继任者,IMQ从来没有被集成过,IFB拥有IMQ的优点,在SMP上更加清晰明了,并且代码量缩减了非常多,旧的中介设 ...

  10. Android 夜间模式的实现

    package com.loaderman.daynightdemo; import android.os.Bundle; import android.support.v7.app.AppCompa ...