Development environment:
IED environment: Eclipse
JDK version: 1.8
maven version: 3.3.9
1. Create a spring boot mcv web application
Open Eclipse and create a new Maven project
Select quickstart template
Complete the creation of the Maven project
Refer to the official example of spring: http://spring.io/guides/gs/testing-web/
Add maven dependency in pom.xml
<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.github.carter659</groupId> <artifactId>spring01</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring01</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependency> </dependencies> <build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Add a controller file "HomeController.java"
package com.github.carter659.spring01;import org.springframework.steretype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class HomeController { @RequestMapping("/") public @ResponseBody String index() { return "Hello, this is the first spring boot application"; }}Modify App.java file
package com.github.carter659.spring01;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class App { public static void main(String[] args) { SpringApplication.run(App.class, args); }}In the App.java file, right-click Run As to run the java program
When the running result is displayed in the console
Enter "http://localhost:8080/" in the browser to access the first spring boot application
2. How do we unit test spring boot?
Adding dependencies for unit tests in pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency>
Create a new test class "HttpRequestTest.java" in src/test/java
package com.github.carter659.spring01;import static org.assertj.core.api.Assertions.assertThat;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.context.SpringBootTest.WebEnvironment;import org.springframework.boot.test.web.client.TestRestTemplate;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)public class HttpRequestTest { @LocalServerPort private int port; @Autowired private TestRestTemplate restTemplate; @Test public void greetingShouldReturnDefaultMessage() throws Exception { assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/", String.class)).contains("Hello, this is the first spring boot application"); }}And run unit tests
Green appears to indicate the assertion is successful
3. How do we deploy spring boot?
We will follow these steps:
1. Download maven
Download maven's bin package on maven's official website: http://maven.apache.org/download.cgi
2. Configure environment variables:
Here is the Program Files (x86) directory that unzips maven to the d disk
Enter: MAVEN_HOME -->D:/Program Files (x86)/maven
Additional to Path: ;%MAVEN_HOME%/bin;
Enter the "mvn -v" command in the CDM window to test whether maven is installed successfully
The 3.3.9 version is displayed here
3. Packing
Enter the "mvn package" command in the directory where the program is located (same level as pom.xml):
"BUILD SUCCESS" appears, which means the packaging is successful
The packaged jar file will appear in the tagget directory
4. Run
Enter the command "java -jar target/spring01-0.0.1-SNAPSHOT.jar" in CMD
The program is deployed at this time. Did you find that the spring boot program is not only very simple to develop and test, but also very easy to deploy?
Code download: https://github.com/carter659/spring-boot-01.git
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.