構(gòu)建過(guò)程
打開(kāi)Idea-> new Project ->Spring Initializr ->填寫group塘装、artifact ->鉤上web(開(kāi)啟web功能)->點(diǎn)下一步就行了刨肃。
目錄結(jié)構(gòu)入下:
├─.idea
│ └─inspectionProfiles
├─.mvn
│ └─wrapper
└─src
├─main
│ ├─java
│ │ └─com
│ │ └─stellar
│ │ └─springboot
│ │ └─springbootfirstapplication #程序入口
│ └─resources
│ ├─static #靜態(tài)資源
│ └─templates #模板資源
| └─application.yml #配置文件
└─test
└─java
└─com
└─stellar
└─springboot
└─springbootfirstapplication
└─pom.xml
pom文件如下
?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stellar.springboot</groupId>
<artifactId>springboot-first-application</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-first-application</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring-boot-starter-web的jar中包含了spring mvc相關(guān)的組件及默認(rèn)配置,還有各種組件的自動(dòng)配置钱反,因此在此種不需要配置項(xiàng)目通過(guò)springbootfirstapplication入口可以直接啟動(dòng)址儒。
添加controller訪問(wèn)
在啟動(dòng)入口做如下修改:
@RestController //配置返回JSON格式
@SpringBootApplication
public class SpringbootFirstApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootFirstApplication.class, args);
}
//添加一個(gè)controller方法
@RequestMapping("/")
public String helloTest(){
return "This is the first springboot application";
}
}
通過(guò)如可main方法啟動(dòng)服務(wù)演训,調(diào)用請(qǐng)求http://localhost:8080/返回結(jié)果如下:
This is the first springboot application
- 此時(shí)tomcat使用的是內(nèi)置的tomcat
- web.xml和springmvcd 配置用的是默認(rèn)配置
查看加載了哪些類
在入口類中添加如下代碼,打印啟動(dòng)加載了哪些類:
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx){
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
//啟動(dòng)打印加載的類
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
單元測(cè)試代碼入下
方式一:
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要不會(huì)報(bào)錯(cuò)
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<String> 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")));
}
}
來(lái)源:http://blog.csdn.net/forezp/article/details/70341651