springboot学习2
项目导入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的更多相关文章
- springboot 学习资源推荐
springboot 是什么?对于构建生产就绪的Spring应用程序有一个看法. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.(这是springboot的官方介绍) 我们为什么要学 ...
- Springboot学习记录1--概念介绍以及环境搭建
摘要:springboot学习记录,环境搭建: 官方文档地址:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/ht ...
- SpringBoot学习笔记
SpringBoot个人感觉比SpringMVC还要好用的一个框架,很多注解配置可以非常灵活的在代码中运用起来: springBoot学习笔记: .一.aop: 新建一个类HttpAspect,类上添 ...
- springboot学习(一)——helloworld
以下内容,如有问题,烦请指出,谢谢 springboot出来也很久了,以前零散地学习了不少,不过很长时间了都没有在实际中使用过了,忘了不少,因此要最近准备抽时间系统的学习积累下springboot,给 ...
- SpringBoot学习(3)-SpringBoot添加支持CORS跨域访问
SpringBoot学习(3)-SpringBoot添加支持CORS跨域访问 https://blog.csdn.net/yft_android/article/details/80307672
- Springboot学习07-数据源Druid
Springboot学习07-数据源Druid 关键字 Druid 前言 学习笔记 正文 1-Druid是什么 Druid是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池.插件框架和SQL解析器 ...
- Springboot学习06-Spring AOP封装接口自定义校验
Springboot学习06-Spring AOP封装接口自定义校验 关键字 BindingResult.Spring AOP.自定义注解.自定义异常处理.ConstraintValidator 前言 ...
- Springboot学习05-自定义错误页面完整分析
Springboot学习06-自定义错误页面完整分析 前言 接着上一篇博客,继续分析Springboot错误页面问题 正文 1-自定义浏览器错误页面(只要将自己的错误页面放在指定的路径下即可) 1-1 ...
- Springboot学习04-默认错误页面加载机制源码分析
Springboot学习04-默认错误页面加载机制源码分析 前沿 希望通过本文的学习,对错误页面的加载机制有这更神的理解 正文 1-Springboot错误页面展示 2-Springboot默认错误处 ...
- Springboot学习笔记(六)-配置化注入
前言 前面写过一个Springboot学习笔记(一)-线程池的简化及使用,发现有个缺陷,打个比方,我这个线程池写在一个公用服务中,各项参数都定死了,现在有两个服务要调用它,一个服务的线程数通常很多,而 ...
随机推荐
- 后盾网lavarel视频项目---lavarel用户认证实例
后盾网lavarel视频项目---lavarel用户认证实例 一.总结 一句话总结: 主要是用的Auth认证,所以配置是配置的auth(config/auth.php),控制器中调用也是用的Auth( ...
- de4dot 反混淆
de4dot .NET deobfuscator and unpacker. Description de4dot is an open source (GPLv3) .NET deobfuscato ...
- 系统句柄报too many files open
对于一般的应用来说(象Apache.系统进程)1024完全足够使用.但是如何象java等单进程处理大量请求的应用来说就有点捉襟见肘了.如果单个进程打开的文件句柄数量超过了系统定义的值,就会提到“too ...
- 阶段3 2.Spring_06.Spring的新注解_4 spring的新注解-Import
把Configuration的直接先注释掉 那么运行测试类的查询所有 并不影响我们的使用 不写同样可以执行的原因是因为这里把SpringConfiguration这个类作为方法传入进去了 新建 Spr ...
- LoadRunner 技巧之 脚本设计
LoadRunner 技巧之 脚本设计 在做性能测试时,我们可能会遇到各种不同的业务需求与用户行为,在一个系统或网站中,每个用户的操作都不完全一样.我们如何来模拟这此用户的行为?经验与能力有限,我这里 ...
- [转] javascript 正则表达式提取数字使用
简述: 测试了一下js的正则表达式函数match 用来从一个字符串里挑出非0开头的数字, 放入一个array里, 之后join(',')之后输出 知识点: 1. 用match函数, 提取一个字符串当中 ...
- 原生js之addEventListener,removeEventListener
使用addEventListener添加事件 <!DOCTYPE html> <html lang="en"> <head> <meta ...
- javaScript 实现倒计时 + 获取网页中的文字
一.倒计时 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...
- 报错:java.lang.NoClassDefFoundError: com/google/inject/Injector
使用testng report,导入jar包:reportng.jar和velocity-dep-1.4.jar后,执行脚本,报错如下: 缺少依赖的jar包:guice-4.0.jar 导入依赖的ja ...
- 解决某些软件无法在parallels desktop虚拟机下运行
步骤1.打开注册表,点开始菜单,点运行,输入regedit.exe后回车 步骤2.找到HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System 步骤3.找到右边的V ...