• spring-boot的helloWorld详解


    1.运行环境

    开发工具:intellij idea

    JDK版本:1.8

    项目管理工具:Maven 3.2.5

    2.Maven Plugin管理

    pom.xml配置代码:

     1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     3   <modelVersion>4.0.0</modelVersion>
     4 
     5   <groupId>spring-boot-helloWorld</groupId>
     6   <artifactId>spring-boot-helloWorld</artifactId>
     7   <version>1.0-SNAPSHOT</version>
     8 
     9   <!-- Spring Boot 启动父依赖 -->
    10   <parent>
    11     <groupId>org.springframework.boot</groupId>
    12     <artifactId>spring-boot-starter-parent</artifactId>
    13     <version>1.3.3.RELEASE</version>
    14   </parent>
    15 
    16   <dependencies>
    17     <!-- Spring Boot web依赖 -->
    18     <dependency>
    19       <groupId>org.springframework.boot</groupId>
    20       <artifactId>spring-boot-starter-web</artifactId>
    21     </dependency>
    22     <!-- Spring Boot test依赖 -->
    23     <dependency>
    24       <groupId>org.springframework.boot</groupId>
    25       <artifactId>spring-boot-starter-test</artifactId>
    26       <scope>test</scope>
    27     </dependency>
    28   </dependencies>
    29 </project>
    View Code

    3.Application启动类编写

     1 package com.goku.demo;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 import org.springframework.boot.web.servlet.ServletComponentScan;
     6 
     7 /**
     8  * Created by nbfujx on 2017/11/20.
     9  */
    10 // Spring Boot 应用的标识
    11 @SpringBootApplication
    12 @ServletComponentScan
    13 public class DemoApplication {
    14 
    15     public static void main(String[] args) {
    16         // 程序启动入口
    17         // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
    18         SpringApplication.run(DemoApplication.class,args);
    19     }
    20 }
    View Code

    4.ExampleController控制器编写

     1 package com.goku.demo.controller;
     2 
     3 import org.springframework.web.bind.annotation.PathVariable;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 import org.springframework.web.bind.annotation.RestController;
     6 
     7 /**
     8  * Created by nbfujx on 2017-11-20.
     9  */
    10 @RestController
    11 public class ExampleController {
    12 
    13     @RequestMapping("/")
    14     public String helloWorld()
    15     {
    16         return "helloWorld";
    17     }
    18 
    19     @RequestMapping("/{str}")
    20     public String helloWorld(@PathVariable  String str)
    21     {
    22         return "hello"+ str;
    23     }
    24 }
    View Code

    5.使用MockMvc对Controller进行测试

    添加相关单元测试

     1 package test.com.goku.demo.controller;
     2 
     3 import com.goku.demo.DemoApplication;
     4 import com.goku.demo.controller.ExampleController;
     5 import org.junit.Before;
     6 import org.junit.Test;
     7 import org.junit.runner.RunWith;
     8 import org.springframework.beans.factory.annotation.Autowired;
     9 import org.springframework.boot.test.context.SpringBootTest;
    10 import org.springframework.http.MediaType;
    11 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    12 import org.springframework.test.context.web.WebAppConfiguration;
    13 import org.springframework.test.web.servlet.MockMvc;
    14 import org.springframework.test.web.servlet.RequestBuilder;
    15 import org.springframework.test.web.servlet.ResultActions;
    16 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
    17 import org.springframework.web.context.WebApplicationContext;
    18 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
    19 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
    20 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
    21 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    22 
    23 import static org.junit.Assert.*;
    24 
    25 /**
    26  * Created by nbfujx on 2017-11-20.
    27  */
    28 @RunWith(SpringJUnit4ClassRunner.class)
    29 @SpringBootTest(classes = DemoApplication.class)//这里的Application是springboot的启动类名。
    30 @WebAppConfiguration
    31 public class ExampleControllerTest {
    32 
    33     @Autowired
    34     private WebApplicationContext context;
    35     private MockMvc mvc;
    36 
    37     @Before
    38     public void setUp() throws Exception {
    39         mvc = MockMvcBuilders.webAppContextSetup(context).build();//建议使用这种
    40     }
    41 
    42     @Test
    43     public void helloWorld() throws Exception {
    44         String responseString = mvc.perform(get("/")    //请求的url,请求的方法是get
    45                 .contentType(MediaType.APPLICATION_JSON)  //数据的格式
    46                 .param("pcode","root")         //添加参数
    47         ).andExpect(status().isOk())    //返回的状态是200
    48                 .andDo(print())         //打印出请求和相应的内容
    49                 .andReturn().getResponse().getContentAsString();   //将相应的数据转换为字符串
    50         System.out.println("--------返回的json = " + responseString);
    51     }
    52 
    53     @Test
    54     public void helloWorld1() throws Exception {
    55         String responseString = mvc.perform(get("/str")    //请求的url,请求的方法是get
    56                 .contentType(MediaType.APPLICATION_JSON)  //数据的格式
    57                 .param("pcode","root")         //添加参数
    58         ).andExpect(status().isOk())    //返回的状态是200
    59                 .andDo(print())         //打印出请求和相应的内容
    60                 .andReturn().getResponse().getContentAsString();   //将相应的数据转换为字符串
    61         System.out.println("--------返回的json = " + responseString);
    62     }
    63 
    64 }
    View Code

    6.在页面上运行

    http://localhost:8080/

    http://localhost:8080/str

    7.GITHUB地址

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

  • 相关阅读:
    新公司面试记
    棋手与棋子
    有线通or ADSL?
    好久没去上海动物园了
    15=60
    技术和销售
    安能辨龟是雄雌巴西龟的雌雄辨别方法
    推荐两首好歌
    母亲节祝福天下所有的母亲
    农夫山泉的源头千岛湖游记
  • 原文地址:https://www.cnblogs.com/nbfujx/p/7865787.html
Copyright © 2020-2023  润新知