spring boot学习系列之一分钟快速搭建1

光年之外 – G.E.M.邓紫棋–:– / 03:55
(*+﹏+*)

为了简化spring应用的搭建和开发,遂诞生了Spring boot这个全新的框架。Spring boot简化了大量的XML配置和复杂的依赖管理,采用习惯大于约定,可以模块化方式导入依赖,提供了RESTful Web服务,为微服务提供了支持。Spring boot快速构建将成为一股风。

创建Maven工程

根据向导创建maven工程

springboot_maven_project

添加依赖

依赖和继承父工程,往pom.xml里添加依赖

  1. <parent>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter-parent</artifactId>
  4.     <version>1.5.4.RELEASE</version>
  5. </parent>

当然可以用依赖的关系来替换:

  1. <dependencyManagement>
  2.      <dependencies>
  3.         <dependency>
  4.             <!– Import dependency management from Spring Boot –>
  5.             <groupId>org.springframework.boot</groupId>
  6.             <artifactId>spring-boot-dependencies</artifactId>
  7.             <version>1.3.1.RELEASE</version>
  8.             <type>pom</type>
  9.             <scope>import</scope>
  10.         </dependency>
  11.     </dependencies>
  12. </dependencyManagement>

继续添加整合web依赖包的依赖:

  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>

maven 构建后,查看依赖层次关系

springboot_maven_dependency

代码

创建HelloWorldApp类,提供项目入口main函数和加入类注解@SpringBootApplication

  1. public static void main(String[] args) {
  2.     SpringApplication.run(HelloWorldApp.class, args);
  3. }

再加入restful 风格的控制层注解@RestController,试下

  1. @SpringBootApplication
  2. @RestController
  3. @RequestMapping(“/hello”)
  4. public class HelloWorldApp {
  5.     @RequestMapping(“/{id}”)
  6.     public Map<String,Object> view(@PathVariable(“id”) String id) {
  7.         Map<String, Object> hashMap = new HashMap<String,Object>();
  8.         hashMap.put(“id”“hello “ + id);
  9.         return hashMap;
  10.     }
  11.     public static void main(String[] args) {
  12.         SpringApplication.run(HelloWorldApp.class, args);
  13.     }
  14. }

确实可以看出还是springMVC的风格,只是spring boot 进行了整合。Main方法作为web项目的入口,说明spring boot 有把web项目打包成jar包的能力,添加maven插件:

  1. <!– Package as an executable JAR –>
  2.     <build>
  3.         <plugins>
  4.             <plugin>
  5.                 <groupId>org.springframework.boot</groupId>
  6.                 <artifactId>spring-boot-maven-plugin</artifactId>
  7.             </plugin>
  8.         </plugins>
  9.     </build>

项目启动

启动项目:

springboot_start_project

项目启动成功,默认是8080端口,访问URL:localhost:8080/hello/wenqy

springboot_access

controller中的路由映射方法返回值可以是返回对象,List,Map等等,springMVC会自动解析成json字符串。

利用 mvn package 命令 打包成jar,在target目录下可以找到生产的jar包

springboot_maven_package

用  jar tvf  springbootdemo-0.0.1-SNAPSHOT.jar 查看里面的内容

springboot_jar_tvf

然后,启动项目 java -jar target/springbootdemo-0.0.1-SNAPSHOT.jar

springboot_java_jar

启动成功后,继续访问刚才的路径,得到的结果是一样的。

加入测试模块的依赖,包括JUnit、TestNG、Mockito

  1. <dependency>
  2.        <groupId>org.springframework.boot</groupId>
  3.        <artifactId>spring-boot-starter-test</artifactId>
  4.        <scope>test</scope>
  5. </dependency>

Junit测试

编写junit单元测试

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest
  3. public class HelloWorldAppTest {
  4.     @Autowired
  5.     private WebApplicationContext wac;
  6.     private MockMvc mockMvc;
  7.     @Before
  8.     public void setup() {
  9.         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
  10.     }
  11.     @Test
  12.     public void getHelloWorld() throws Exception {
  13.         this.mockMvc.perform(MockMvcRequestBuilders.get(“/hello/wenqy”)
  14.                 .accept(MediaType.parseMediaType(“application/json;charset=UTF-8”)))
  15.         .andDo(MockMvcResultHandlers.print())
  16.         .andExpect(MockMvcResultMatchers.status().isOk())
  17.         //.andExpect(MockMvcResultMatchers.content().contentType(“application/json”))
  18.         .andExpect(MockMvcResultMatchers.status().isOk())
  19.         .andExpect(MockMvcResultMatchers.jsonPath(“id”).value(“hello wenqy”));
  20.     }
  21. }

测试打印情况:

springboot_junit_test

如果想自己利用现有代码快速搭建,可以到https://start.spring.io/ 下载demo,还支持gradle的项目。

参考:

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#Server-Side Tests

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#getting-started-first-application-code

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

5 + 9 = ?