Reworked to use the Spring Guides' Spring Boot web application example
This commit is contained in:
parent
33984c4c0d
commit
3385b72fb5
14 changed files with 143 additions and 72 deletions
1
.idea/misc.xml
generated
1
.idea/misc.xml
generated
|
@ -1,4 +1,3 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="PDMPlugin">
|
<component name="PDMPlugin">
|
||||||
<option name="skipTestSources" value="false" />
|
<option name="skipTestSources" value="false" />
|
||||||
|
|
9
.idea/runConfigurations/Run Tests.xml
generated
9
.idea/runConfigurations/Run Tests.xml
generated
|
@ -1,9 +0,0 @@
|
||||||
<component name="ProjectRunConfigurationManager">
|
|
||||||
<configuration default="false" name="Run Tests" type="Application" factoryName="Application" nameIsGenerated="true">
|
|
||||||
<option name="MAIN_CLASS_NAME" value="com.example.demo.DemoApplicationTest" />
|
|
||||||
<module name="app" />
|
|
||||||
<method v="2">
|
|
||||||
<option name="Make" enabled="true" />
|
|
||||||
</method>
|
|
||||||
</configuration>
|
|
||||||
</component>
|
|
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
|
@ -5,7 +5,7 @@
|
||||||
"type": "java",
|
"type": "java",
|
||||||
"name": "Run Tests",
|
"name": "Run Tests",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mainClass": "com.example.demo.DemoApplicationTest"
|
"mainClass": "com.example.springboot.DemoApplicationTests"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
2
bld
2
bld
|
@ -1,2 +1,2 @@
|
||||||
#!/usr/bin/env sh
|
#!/usr/bin/env sh
|
||||||
java -jar "$(dirname "$0")/lib/bld/bld-wrapper.jar" "$0" --build com.example.demo.DemoApplicationBuild "$@"
|
java -jar "$(dirname "$0")/lib/bld/bld-wrapper.jar" "$0" --build com.example.springboot.ApplicationBuild "$@"
|
2
bld.bat
2
bld.bat
|
@ -1,4 +1,4 @@
|
||||||
@echo off
|
@echo off
|
||||||
set DIRNAME=%~dp0
|
set DIRNAME=%~dp0
|
||||||
if "%DIRNAME%" == "" set DIRNAME=.
|
if "%DIRNAME%" == "" set DIRNAME=.
|
||||||
java -jar "%DIRNAME%/lib/bld/bld-wrapper.jar" "%0" --build com.example.demo.DemoApplicationBuild %*
|
java -jar "%DIRNAME%/lib/bld/bld-wrapper.jar" "%0" --build com.example.springboot.ApplicationBuild %*
|
|
@ -1,34 +0,0 @@
|
||||||
package com.example.demo;
|
|
||||||
|
|
||||||
import rife.bld.BaseProject;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static rife.bld.dependencies.Repository.*;
|
|
||||||
import static rife.bld.dependencies.Scope.compile;
|
|
||||||
import static rife.bld.dependencies.Scope.test;
|
|
||||||
|
|
||||||
public class DemoApplicationBuild extends BaseProject {
|
|
||||||
public DemoApplicationBuild() {
|
|
||||||
pkg = "com.example.demo";
|
|
||||||
name = "DemoApplication";
|
|
||||||
mainClass = "com.example.demo.DemoApplication";
|
|
||||||
version = version(0,1,0);
|
|
||||||
|
|
||||||
repositories = List.of(MAVEN_CENTRAL);
|
|
||||||
|
|
||||||
scope(compile)
|
|
||||||
.include(dependency("org.springframework.boot", "spring-boot-starter",
|
|
||||||
version(3,1,5)));
|
|
||||||
scope(test)
|
|
||||||
.include(dependency("org.springframework.boot", "spring-boot-starter-test",
|
|
||||||
version(3,1,5)))
|
|
||||||
.include(dependency("org.junit.jupiter", "junit-jupiter",
|
|
||||||
version(5,10,0)));
|
|
||||||
|
|
||||||
testOperation().mainClass("com.example.demo.DemoApplicationTest");
|
|
||||||
}
|
|
||||||
public static void main(String[] args) {
|
|
||||||
new DemoApplicationBuild().start(args);
|
|
||||||
}
|
|
||||||
}
|
|
37
src/bld/java/com/example/springboot/ApplicationBuild.java
Normal file
37
src/bld/java/com/example/springboot/ApplicationBuild.java
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
package com.example.springboot;
|
||||||
|
|
||||||
|
import rife.bld.BaseProject;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static rife.bld.dependencies.Repository.MAVEN_CENTRAL;
|
||||||
|
import static rife.bld.dependencies.Scope.compile;
|
||||||
|
import static rife.bld.dependencies.Scope.test;
|
||||||
|
|
||||||
|
public class ApplicationBuild extends BaseProject {
|
||||||
|
public ApplicationBuild() {
|
||||||
|
pkg = "com.example.demo";
|
||||||
|
name = "DemoApplication";
|
||||||
|
mainClass = "com.example.springboot.Application";
|
||||||
|
version = version(0, 1, 0);
|
||||||
|
|
||||||
|
repositories = List.of(MAVEN_CENTRAL);
|
||||||
|
|
||||||
|
scope(compile)
|
||||||
|
.include(dependency("org.springframework.boot", "spring-boot-starter",
|
||||||
|
version(3, 1, 5)))
|
||||||
|
.include(dependency("org.springframework.boot", "spring-boot-starter-actuator",
|
||||||
|
version(3, 1, 5)))
|
||||||
|
.include(dependency("org.springframework.boot", "spring-boot-starter-web",
|
||||||
|
version(3, 1, 5)));
|
||||||
|
scope(test)
|
||||||
|
.include(dependency("org.springframework.boot", "spring-boot-starter-test",
|
||||||
|
version(3, 1, 5)));
|
||||||
|
|
||||||
|
testOperation().mainClass("com.example.springboot.HelloControllerIT");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new ApplicationBuild().start(args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,11 +0,0 @@
|
||||||
package com.example.demo;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
public class DemoApplication {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
SpringApplication.run(DemoApplication.class, args);
|
|
||||||
}
|
|
||||||
}
|
|
33
src/main/java/com/example/springboot/Application.java
Normal file
33
src/main/java/com/example/springboot/Application.java
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
package com.example.springboot;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.springframework.boot.CommandLineRunner;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
14
src/main/java/com/example/springboot/HelloController.java
Normal file
14
src/main/java/com/example/springboot/HelloController.java
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package com.example.springboot;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class HelloController {
|
||||||
|
|
||||||
|
@GetMapping("/")
|
||||||
|
public String index() {
|
||||||
|
return "Greetings from Spring Boot!";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1 +0,0 @@
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
package com.example.demo;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
|
|
||||||
@SpringBootTest
|
|
||||||
class DemoApplicationTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void contextLoads() {
|
|
||||||
}
|
|
||||||
}
|
|
27
src/test/java/com/example/springboot/HelloControllerIT.java
Normal file
27
src/test/java/com/example/springboot/HelloControllerIT.java
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package com.example.springboot;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||||
|
public class HelloControllerIT {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TestRestTemplate template;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getHello() throws Exception {
|
||||||
|
ResponseEntity<String> response = template.getForEntity("/", String.class);
|
||||||
|
assertThat(response.getBody()).isEqualTo("Greetings from Spring Boot!");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
new HelloControllerIT().getHello();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.example.springboot;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
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.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
public class HelloControllerTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mvc;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getHello() throws Exception {
|
||||||
|
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().string(equalTo("Greetings from Spring Boot!")));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue