diff --git a/README.md b/README.md index 3749ea5..cebead5 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Spring Boot 使用的各种示例,以最简单、最实用为标准,此开 - [spring-boot-scheduler](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-scheduler):Spring Boot 3.0 定时任务 scheduler 使用示例 - [spring-boot-mail](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mail):Spring Boot 3.0 邮件发送使用示例 - [spring-boot-mongodb](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mongodb):Spring Boot 3.0 MongoDB 增删改查示例 多数据源使用案例 - +- [spring-boot-package](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-package):Spring Boot 3.0 单元测试、集成测试、打 Jar/War 包、定制启动参数使用案例 diff --git a/spring-boot-package/spring-boot-package-war/pom.xml b/spring-boot-package/spring-boot-package-war/pom.xml new file mode 100644 index 0000000..2082500 --- /dev/null +++ b/spring-boot-package/spring-boot-package-war/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + + com.neo + spring-boot-package-war + 0.0.1-SNAPSHOT + war + + spring-boot-package-war + Demo project for Spring Boot package war + + + org.springframework.boot + spring-boot-starter-parent + 3.0.0 + + + + + UTF-8 + 17 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-test + + + org.junit.vintage + junit-vintage-engine + test + + + org.hamcrest + hamcrest-core + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + provided + + + org.springframework.boot + spring-boot-devtools + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-boot-package/spring-boot-package-war/src/main/java/com/neo/Application.java b/spring-boot-package/spring-boot-package-war/src/main/java/com/neo/Application.java new file mode 100644 index 0000000..6cda50c --- /dev/null +++ b/spring-boot-package/spring-boot-package-war/src/main/java/com/neo/Application.java @@ -0,0 +1,12 @@ +package com.neo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-boot-package/spring-boot-package-war/src/main/java/com/neo/ServletInitializer.java b/spring-boot-package/spring-boot-package-war/src/main/java/com/neo/ServletInitializer.java new file mode 100644 index 0000000..8becd38 --- /dev/null +++ b/spring-boot-package/spring-boot-package-war/src/main/java/com/neo/ServletInitializer.java @@ -0,0 +1,14 @@ +package com.neo; + +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +/** + * Created by summer on 2017/5/8. + */ +public class ServletInitializer extends SpringBootServletInitializer { + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(Application.class); + } +} diff --git a/spring-boot-package/spring-boot-package-war/src/main/java/com/neo/controller/HelloWorldController.java b/spring-boot-package/spring-boot-package-war/src/main/java/com/neo/controller/HelloWorldController.java new file mode 100644 index 0000000..7d0256b --- /dev/null +++ b/spring-boot-package/spring-boot-package-war/src/main/java/com/neo/controller/HelloWorldController.java @@ -0,0 +1,13 @@ +package com.neo.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloWorldController { + + @RequestMapping("/hello") + public String index() { + return "Hello World"; + } +} \ No newline at end of file diff --git a/spring-boot-package/spring-boot-package-war/src/main/resources/application.properties b/spring-boot-package/spring-boot-package-war/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/spring-boot-package/spring-boot-package-war/src/test/java/com/neo/ApplicationTests.java b/spring-boot-package/spring-boot-package-war/src/test/java/com/neo/ApplicationTests.java new file mode 100644 index 0000000..457f6d8 --- /dev/null +++ b/spring-boot-package/spring-boot-package-war/src/test/java/com/neo/ApplicationTests.java @@ -0,0 +1,14 @@ +package com.neo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +public class ApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-boot-package/spring-boot-package-war/src/test/java/com/neo/controller/HelloTests.java b/spring-boot-package/spring-boot-package-war/src/test/java/com/neo/controller/HelloTests.java new file mode 100644 index 0000000..582eade --- /dev/null +++ b/spring-boot-package/spring-boot-package-war/src/test/java/com/neo/controller/HelloTests.java @@ -0,0 +1,36 @@ +package com.neo.controller; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.http.MediaType; +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) +@WebAppConfiguration +public class HelloTests { + + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build(); + } + + @Test + public void getHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("Hello World"))); + } + +} \ No newline at end of file diff --git a/spring-boot-package/spring-boot-package-war/src/test/java/com/neo/controller/HelloWorldControlerTests.java b/spring-boot-package/spring-boot-package-war/src/test/java/com/neo/controller/HelloWorldControlerTests.java new file mode 100644 index 0000000..4645772 --- /dev/null +++ b/spring-boot-package/spring-boot-package-war/src/test/java/com/neo/controller/HelloWorldControlerTests.java @@ -0,0 +1,35 @@ +package com.neo.controller; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +public class HelloWorldControlerTests { + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build(); + } + + @Test + public void getHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andDo(MockMvcResultHandlers.print()) + .andReturn(); + } + +} \ No newline at end of file diff --git a/spring-boot-package/spring-boot-package/pom.xml b/spring-boot-package/spring-boot-package/pom.xml new file mode 100644 index 0000000..0b26e52 --- /dev/null +++ b/spring-boot-package/spring-boot-package/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + com.neo + spring-boot-package + 1.0.0 + jar + + spring-boot-package + Demo project for Spring Boot package war + + + org.springframework.boot + spring-boot-starter-parent + 3.0.0 + + + + + UTF-8 + 17 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + + + org.junit.vintage + junit-vintage-engine + test + + + org.hamcrest + hamcrest-core + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-boot-package/spring-boot-package/src/main/java/com/neo/PackageApplication.java b/spring-boot-package/spring-boot-package/src/main/java/com/neo/PackageApplication.java new file mode 100644 index 0000000..6611164 --- /dev/null +++ b/spring-boot-package/spring-boot-package/src/main/java/com/neo/PackageApplication.java @@ -0,0 +1,19 @@ +package com.neo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +@SpringBootApplication +public class PackageApplication extends SpringBootServletInitializer { + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(PackageApplication.class); + } + + public static void main(String[] args) { + SpringApplication.run(PackageApplication.class, args); + } +} diff --git a/spring-boot-package/spring-boot-package/src/main/java/com/neo/controller/HelloController.java b/spring-boot-package/spring-boot-package/src/main/java/com/neo/controller/HelloController.java new file mode 100644 index 0000000..5e93f0d --- /dev/null +++ b/spring-boot-package/spring-boot-package/src/main/java/com/neo/controller/HelloController.java @@ -0,0 +1,13 @@ +package com.neo.controller; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloController { + + @RequestMapping("/hello") + public String index() { + return "Hello World"; + } +} \ No newline at end of file diff --git a/spring-boot-package/spring-boot-package/src/main/resources/application-dev.properties b/spring-boot-package/spring-boot-package/src/main/resources/application-dev.properties new file mode 100644 index 0000000..c83a6aa --- /dev/null +++ b/spring-boot-package/spring-boot-package/src/main/resources/application-dev.properties @@ -0,0 +1,2 @@ +info.app.name=spring-boot-test +info.app.version= 1.0.0 \ No newline at end of file diff --git a/spring-boot-package/spring-boot-package/src/main/resources/application-pro.properties b/spring-boot-package/spring-boot-package/src/main/resources/application-pro.properties new file mode 100644 index 0000000..5bc6c5c --- /dev/null +++ b/spring-boot-package/spring-boot-package/src/main/resources/application-pro.properties @@ -0,0 +1,2 @@ +info.app.name=spring-boot-pro +info.app.version= 1.0.0 \ No newline at end of file diff --git a/spring-boot-package/spring-boot-package/src/main/resources/application-test.properties b/spring-boot-package/spring-boot-package/src/main/resources/application-test.properties new file mode 100644 index 0000000..995c6d2 --- /dev/null +++ b/spring-boot-package/spring-boot-package/src/main/resources/application-test.properties @@ -0,0 +1,2 @@ +info.app.name=spring-boot-uat +info.app.version= 1.0.0 \ No newline at end of file diff --git a/spring-boot-package/spring-boot-package/src/main/resources/application.properties b/spring-boot-package/spring-boot-package/src/main/resources/application.properties new file mode 100644 index 0000000..bba9dfd --- /dev/null +++ b/spring-boot-package/spring-boot-package/src/main/resources/application.properties @@ -0,0 +1,26 @@ +# \u9879\u76EEcontextPath\uFF0C\u4E00\u822C\u4E0D\u914D\u7F6E +#server.servlet.context-path=/myspringboot +# \u9519\u8BEF\u9875\uFF0C\u6307\u5B9A\u53D1\u751F\u9519\u8BEF\u65F6\uFF0C\u8DF3\u8F6C\u7684URL\u3002 +server.error.path=/error +# \u670D\u52A1\u7AEF\u53E3 +server.port=8080 +# session\u6700\u5927\u8D85\u65F6\u65F6\u95F4(\u5206\u949F)\uFF0C\u9ED8\u8BA4\u4E3A30 +server.session-timeout=60 +# \u8BE5\u670D\u52A1\u7ED1\u5B9AIP\u5730\u5740\uFF0C\u542F\u52A8\u670D\u52A1\u5668\u65F6\u5982\u672C\u673A\u4E0D\u662F\u8BE5IP\u5730\u5740\u5219\u629B\u51FA\u5F02\u5E38\u542F\u52A8\u5931\u8D25\uFF0C\u53EA\u6709\u7279\u6B8A\u9700\u6C42\u7684\u60C5\u51B5\u4E0B\u624D\u914D\u7F6E +#server.address=192.168.0.6 + +# tomcat \u6700\u5927\u7EBF\u7A0B\u6570\uFF0C\u9ED8\u8BA4\u4E3A200 +server.tomcat.max-threads=600 +# tomcat\u7684URI\u7F16\u7801 +server.tomcat.uri-encoding=UTF-8 +# \u5B58\u653ETomcat\u7684\u65E5\u5FD7\u3001Dump\u7B49\u6587\u4EF6\u7684\u4E34\u65F6\u6587\u4EF6\u5939\uFF0C\u9ED8\u8BA4\u4E3A\u7CFB\u7EDF\u7684tmp\u6587\u4EF6\u5939 +server.tomcat.basedir=/tmp/log +# \u6253\u5F00Tomcat\u7684Access\u65E5\u5FD7\uFF0C\u5E76\u53EF\u4EE5\u8BBE\u7F6E\u65E5\u5FD7\u683C\u5F0F +#server.tomcat.access-log-enabled=true +#server.tomcat.access-log-pattern= +# accesslog\u76EE\u5F55\uFF0C\u9ED8\u8BA4\u5728basedir/logs +#server.tomcat.accesslog.directory= +# \u65E5\u5FD7\u6587\u4EF6\u76EE\u5F55 +logging.path=/tmp/log +# \u65E5\u5FD7\u6587\u4EF6\u540D\u79F0\uFF0C\u9ED8\u8BA4\u4E3Aspring.log +logging.file=myapp.log \ No newline at end of file diff --git a/spring-boot-package/spring-boot-package/src/test/java/com/neo/PackageApplicationTests.java b/spring-boot-package/spring-boot-package/src/test/java/com/neo/PackageApplicationTests.java new file mode 100644 index 0000000..ff737b9 --- /dev/null +++ b/spring-boot-package/spring-boot-package/src/test/java/com/neo/PackageApplicationTests.java @@ -0,0 +1,16 @@ +package com.neo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class PackageApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-boot-package/spring-boot-package/src/test/java/com/neo/controller/HelloTests.java b/spring-boot-package/spring-boot-package/src/test/java/com/neo/controller/HelloTests.java new file mode 100644 index 0000000..1495439 --- /dev/null +++ b/spring-boot-package/spring-boot-package/src/test/java/com/neo/controller/HelloTests.java @@ -0,0 +1,36 @@ +package com.neo.controller; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.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(SpringRunner.class) +@SpringBootTest +public class HelloTests { + + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build(); + } + + @Test + public void getHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("Hello World"))); + } + +} \ No newline at end of file diff --git a/spring-boot-package/spring-boot-package/src/test/java/com/neo/controller/HelloWorldControlerTests.java b/spring-boot-package/spring-boot-package/src/test/java/com/neo/controller/HelloWorldControlerTests.java new file mode 100644 index 0000000..d8b01a5 --- /dev/null +++ b/spring-boot-package/spring-boot-package/src/test/java/com/neo/controller/HelloWorldControlerTests.java @@ -0,0 +1,34 @@ +package com.neo.controller; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +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.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class HelloWorldControlerTests { + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build(); + } + + @Test + public void getHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andDo(MockMvcResultHandlers.print()) + .andReturn(); + } + +} \ No newline at end of file