01-构建一个springboot工程

构建过程

打开Idea-> new Project ->Spring Initializr ->填写group、artifact ->钩上web(开启web功能)->点下一步就行了。

目录结构入下:

├─.idea
│  └─inspectionProfiles
├─.mvn
│  └─wrapper
└─src
    ├─main
    │  ├─java
    │  │  └─com
    │  │      └─stellar
    │  │          └─springboot
    │  │              └─springbootfirstapplication #程序入口
    │  └─resources
    │      ├─static #静态资源
    │      └─templates #模板资源
    |      └─application.yml #配置文件
    └─test
        └─java
            └─com
                └─stellar
                    └─springboot
                        └─springbootfirstapplication
└─pom.xml

pom文件如下

?xml version="1.0" encoding="UTF-8"?>

    4.0.0

    com.stellar.springboot
    springboot-first-application
    0.0.1-SNAPSHOT
    jar

    springboot-first-application
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



spring-boot-starter-web的jar中包含了spring mvc相关的组件及默认配置,还有各种组件的自动配置,因此在此种不需要配置项目通过springbootfirstapplication入口可以直接启动。

添加controller访问

在启动入口做如下修改:

@RestController //配置返回JSON格式
@SpringBootApplication
public class SpringbootFirstApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootFirstApplication.class, args);
    }
    //添加一个controller方法
    @RequestMapping("/")
    public String helloTest(){
        return "This is the first springboot application";
    }
}

通过如可main方法启动服务,调用请求http://localhost:8080/返回结果如下:

This is the first springboot application
  • 此时tomcat使用的是内置的tomcat
  • web.xml和springmvcd 配置用的是默认配置

查看加载了哪些类

在入口类中添加如下代码,打印启动加载了哪些类:

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx){
        return args -> {
            System.out.println("Let's inspect the beans provided by Spring Boot:");
            //启动打印加载的类
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }
        };
    }

单元测试代码入下

方式一:

package com.stellar.springboot.springbootfirstapplication;

import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.net.MalformedURLException;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)//需要配置webEnviroment要不会报错
public class SpringbootFirstApplicationTests {

    @LocalServerPort
    private int port;

    private String baseUrl;

    @Autowired
    private TestRestTemplate testRestTemplate;

    @Before
    public void setUp() throws MalformedURLException {
        this.baseUrl="http://localhost:"+this.port+"/";
    }

    @Test
    public void testHello(){
        ResponseEntity responseEntity = this.testRestTemplate.getForEntity(this.baseUrl,String.class);
        Assert.assertThat(responseEntity.getBody(), Matchers.equalTo("This is the first springboot application"));
    }
}

方式二:

import org.hamcrest.Matchers;
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.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloTest {
    @Autowired
    private MockMvc mvc;

    @Test
    public void testHello() throws Exception {
        this.mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON)
        ).andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string(Matchers.equalTo("This is the first springboot application")));
    }

}

来源:http://blog.csdn.net/forezp/article/details/70341651

你可能感兴趣的:(01-构建一个springboot工程)