springboot学习2
2024-09-01 11:08:28
项目导入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组织的项目。
最新文章
- 部署网站出现System.ServiceModel.Activation.HttpModule错误
- ListView简单使用
- 响应式布局 Bootstrap(01)
- FFmpeg - 音频解码过程
- 【freemaker】之循环,判断,对象取值
- WCF - 绑定
- hyperLink的定制
- 一个只有99行代码的JS流程框架(二)
- js中多维数组转一维
- PHP 页面跳转到另一个页面的多种方法方法总结
- A Basic Course in Partial Differential Equations
- Camp 前三日简单总结
- spring boot + easypoi两行代码excel导入导出
- awesomium_v1.6.6_sdk 百度云下载地址
- iOS 10的两个坑
- mysql建表规范及注意事项
- IIS部署wordpress4.7.4
- Python中的基本数据类型的区别
- 《无所不能的JavaScript编程系列:setTimeout 简笔》
- Java设计模式(15)备忘录模式(Memento模式)