来源:
http://blog.didispace.com/Spring-Boot%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/
快速运行一个简单的spring-boot就一下三步:
一、pom.xml中引入spring-boot的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.0.4.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
二、创建主入口:
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
三、随便写一个Controller:
@GetMapping 等于 @RequestMapping(method = RequestMethod.GET),不同的写法,用法一样;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
@GetMapping("/hello")
public String hello() {
return "hello";
}
}
-----------------------------------------------------------------------------------------------------------------------------------
以上步骤就可以搭建并运行一个简单的springboot的工程的。测试的方式有两种:
1.执行主入口的main方法,启动成功且控制台无报错,在浏览器中输入url,会显示“hello”,控制台日志如下,可以从控制台中看到启动的端口是8080:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.4.RELEASE)
2018-10-30 11:29:40.695 INFO 5676 --- [ main] com.demo.spring.boot.App : Starting App on cnpc1580 with PID 5676 (D:\workspace\boot\target\classes started by liwen.cui in D:\workspace\boot)
2018-10-30 11:29:40.695 INFO 5676 --- [ main] com.demo.spring.boot.App : No active profile set, falling back to default profiles: default
2018-10-30 11:29:40.737 INFO 5676 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@15bb6bea: startup date [Tue Oct 30 11:29:40 CST 2018]; root of context hierarchy
2018-10-30 11:29:41.765 INFO 5676 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-10-30 11:29:41.786 INFO 5676 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-10-30 11:29:41.786 INFO 5676 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.32
2018-10-30 11:29:41.803 INFO 5676 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\Apps\Java\jdk1.8.172\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Apps\TortoiseSVN\bin;D:\Apps\apache-maven-3.5.3\bin;D:\Apps\Java\jdk1.8.172\bin;;.]
2018-10-30 11:29:41.887 INFO 5676 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-10-30 11:29:41.887 INFO 5676 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1150 ms
2018-10-30 11:29:41.937 INFO 5676 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-10-30 11:29:41.947 INFO 5676 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-10-30 11:29:41.947 INFO 5676 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-10-30 11:29:41.947 INFO 5676 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-10-30 11:29:41.947 INFO 5676 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-10-30 11:29:42.027 INFO 5676 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-30 11:29:42.137 INFO 5676 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@15bb6bea: startup date [Tue Oct 30 11:29:40 CST 2018]; root of context hierarchy
2018-10-30 11:29:42.187 INFO 5676 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public java.lang.String com.demo.spring.boot.controller.Controller.hello()
2018-10-30 11:29:42.197 INFO 5676 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-10-30 11:29:42.197 INFO 5676 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-10-30 11:29:42.207 INFO 5676 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-30 11:29:42.207 INFO 5676 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-10-30 11:29:42.317 INFO 5676 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-10-30 11:29:42.357 INFO 5676 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-10-30 11:29:42.357 INFO 5676 --- [ main] com.demo.spring.boot.App : Started App in 1.892 seconds (JVM running for 2.311)
2.通过单元测试验证:
package com.demo.spring.boot.controller;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
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;
//让status、content、equalTo函数可用需要导入下面三个内容
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)
//1.4之后的版本去掉了 @SpringApplicationConfiguration,换为@SpringBootTest
@SpringBootTest(classes=MockServletContext.class)
@WebAppConfiguration
public class ControllerTest {
private static MockMvc mvc;
@BeforeClass
public static void Before(){
mvc = MockMvcBuilders.standaloneSetup(new Controller()).build();
}
@Test
public void getHello() throws Exception {
System.out.println(mvc==null);
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(equalTo("hello")));
}
}