Spring Boot IntelliJ IDEA 例子(Hello Word!)

    随着软件技术的快速发展,身为一名程序猿需要学习新知识,今天get了spring boot框架并做了总结。(Hello Word!)

Spring的基础就不再这里介绍了,可以参考( 初识Spring Boot框架Spring Boot 入门之基础Spring boot学习笔记

下面我用idea写的两个栗子

栗子1、构建项目(初识Spring Boot框架)-》编码-》运行 http://localhost:8080(默认端口8080)

package com.example.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController 
@SpringBootApplication
public class DemoApplication {

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

   @RequestMapping(value = "/",produces = "text/plain;charset=UTF-8")
   String index(){
      return "Hello Spring Boot!";
   }
}

栗子2、构建项目(初识Spring Boot框架)-》编码-》运行 http://localhost:8080/hello(默认端口8080)

package com.example.hello;

import com.example.web.HelloController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by Rhett on 18/3/22.
 */
@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args){
        SpringApplication.run(HelloController.class, args);
    }
}
package com.example.web;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Rhett on 18/3/22.
 */
@EnableAutoConfiguration
@RestController
public class HelloController {
    @RequestMapping("hello")
    String index(){
        return "Hello Word!";
    }
}

栗子3、测试

package com.example.demo;

import com.example.web.HelloController;

import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.boot.test.SpringApplicationConfiguration;

import org.springframework.http.MediaType;

import org.springframework.mock.web.MockServletContext;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import org.springframework.test.context.web.WebAppConfiguration;

import org.springframework.test.web.servlet.MockMvc;

import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import org.springframework.test.web.servlet.setup.MockMvcBuilders;

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(SpringJUnit4ClassRunner.class)

@SpringApplicationConfiguration(classes = MockServletContext.class)

@WebAppConfiguration

public class DemoApplicationTests {

private MockMvc mvc;

@Before

public void setUp() throws Exception {

mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();//初始化MockMvc对象

}

@Test

public void getHello() throws Exception {

mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))//执行请求  

.andExpect(status().isOk())//验证状态码

.andExpect(content().string(equalTo("Hello World!")))

.andReturn().getResponse().getContentAsString();//将相应的数据转换为字符串

}

}

注意点:1、如需修改端口:application.properites

server.port=8081

2、栗子2会遇到的问题:spring boot启动报Unable to start EmbeddedWebApplicationContext解决方案

3、栗子3可能报的错:

java.lang.AssertionError: Response content
Expected: "Hello Word"
     but: was "Hello Word!"
原因及解决的方法:HelloController类返回的结果和 .andExpect(content().string(equalTo( "Hello World" )))里返回的结果不一致造成。

 
  

猜你喜欢

转载自blog.csdn.net/ScarletLina/article/details/79656099