Move articles from spring-core-6 module to other spring-boot modules (#13722)
* Move articles from spring-core-6 module to other spring-boot modules * JAVA-19559 Move articles from spring-core-6 module to other spring-boot-3 modules * JAVA-19559 Minor spacing change * JAVA-19559 Revert Minor spacing change
This commit is contained in:
@@ -5,3 +5,4 @@
|
||||
- [Singleton Design Pattern vs Singleton Beans in Spring Boot](https://www.baeldung.com/spring-boot-singleton-vs-beans)
|
||||
- [Migrate Application From Spring Boot 2 to Spring Boot 3](https://www.baeldung.com/spring-boot-3-migration)
|
||||
- [Using Java Records with JPA](https://www.baeldung.com/spring-jpa-java-records)
|
||||
- [HTTP Interface in Spring 6](https://www.baeldung.com/spring-6-http-interface)
|
||||
|
||||
@@ -32,6 +32,20 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mock-server</groupId>
|
||||
<artifactId>mockserver-netty</artifactId>
|
||||
<version>${mockserver.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mock-server</groupId>
|
||||
<artifactId>mockserver-client-java</artifactId>
|
||||
<version>${mockserver.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
@@ -125,7 +139,7 @@
|
||||
<springdoc.version>2.0.0</springdoc.version>
|
||||
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
|
||||
<start-class>com.baeldung.sample.TodoApplication</start-class>
|
||||
|
||||
<mockserver.version>5.14.0</mockserver.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.baeldung.httpinterface;
|
||||
|
||||
public record Book(long id, String title, String author, int year) {}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.httpinterface;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
|
||||
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
||||
|
||||
@Component
|
||||
public class BooksClient {
|
||||
|
||||
private final BooksService booksService;
|
||||
|
||||
public BooksClient(WebClient webClient) {
|
||||
HttpServiceProxyFactory httpServiceProxyFactory =
|
||||
HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient))
|
||||
.build();
|
||||
booksService = httpServiceProxyFactory.createClient(BooksService.class);
|
||||
}
|
||||
|
||||
public BooksService getBooksService() {
|
||||
return booksService;
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.httpinterface;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.service.annotation.DeleteExchange;
|
||||
import org.springframework.web.service.annotation.GetExchange;
|
||||
import org.springframework.web.service.annotation.PostExchange;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
interface BooksService {
|
||||
|
||||
@GetExchange("/books")
|
||||
List<Book> getBooks();
|
||||
|
||||
@GetExchange("/books/{id}")
|
||||
Book getBook(@PathVariable("id") long id);
|
||||
|
||||
@PostExchange("/books")
|
||||
Book saveBook(@RequestBody Book book);
|
||||
|
||||
@DeleteExchange("/books/{id}")
|
||||
ResponseEntity<Void> deleteBook(@PathVariable("id") long id);
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.httpinterface;
|
||||
|
||||
public class MyServiceException extends RuntimeException {
|
||||
|
||||
MyServiceException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,3 +10,5 @@
|
||||
- [Load Spring Boot Properties From a JSON File](https://www.baeldung.com/spring-boot-json-properties)
|
||||
- [IntelliJ – Cannot Resolve Spring Boot Configuration Properties Error](https://www.baeldung.com/intellij-resolve-spring-boot-configuration-properties)
|
||||
- [Log Properties in a Spring Boot Application](https://www.baeldung.com/spring-boot-log-properties)
|
||||
- [Using Environment Variables in Spring Boot’s application.properties](https://www.baeldung.com/spring-boot-properties-env-variables)
|
||||
- More articles: [[<-- prev]](../spring-boot-properties-2)
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.envvariables;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "baeldung")
|
||||
public class BaeldungProperties {
|
||||
|
||||
private String presentation;
|
||||
|
||||
public String getPresentation() {
|
||||
return presentation;
|
||||
}
|
||||
|
||||
public void setPresentation(String presentation) {
|
||||
this.presentation = presentation;
|
||||
}
|
||||
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.envvariables;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class MyController {
|
||||
|
||||
@Value("${environment.name}")
|
||||
private String environmentName;
|
||||
|
||||
@Value("${java.home.and.environment}")
|
||||
private String javaHomeAndEnvironmentName;
|
||||
|
||||
@Value("${thispropertydoesnotexist}")
|
||||
private String nonExistentProperty;
|
||||
|
||||
@Value("${baeldung.presentation}")
|
||||
private String baeldungPresentation;
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired
|
||||
private BaeldungProperties baeldungProperties;
|
||||
|
||||
@GetMapping("/environment_name")
|
||||
String getEnvironmentName_FromEnvironmentVariables() {
|
||||
return environmentName;
|
||||
}
|
||||
|
||||
@GetMapping("/java_home_and_environment")
|
||||
String getJavaHomeAndEnvironmentName_FromEnvironmentVariables() {
|
||||
return javaHomeAndEnvironmentName;
|
||||
}
|
||||
|
||||
@GetMapping("non_existent_property")
|
||||
String getNonexistentProperty_FromEnvironmentVariables() {
|
||||
return nonExistentProperty;
|
||||
}
|
||||
|
||||
@GetMapping("baeldung_presentation_from_value")
|
||||
String getBaeldungPresentation_FromValue() {
|
||||
return baeldungPresentation;
|
||||
}
|
||||
|
||||
@GetMapping("baeldung_presentation_from_environment")
|
||||
String getBaeldungPresentation_FromEnvironment() {
|
||||
return environment.getProperty("baeldung.presentation");
|
||||
}
|
||||
|
||||
@GetMapping("baeldung_configuration_properties")
|
||||
String getBaeldungPresentation_FromConfigurationProperties() {
|
||||
return baeldungProperties.getPresentation();
|
||||
}
|
||||
|
||||
}
|
||||
+6
-1
@@ -26,4 +26,9 @@ spring.config.activate.on-profile=multidocument-prod
|
||||
spring.datasource.password=password
|
||||
spring.datasource.url=jdbc:h2:prod
|
||||
spring.datasource.username=prodUser
|
||||
bael.property=prodValue
|
||||
bael.property=prodValue
|
||||
#---
|
||||
environment.name=${OS}
|
||||
java.home.and.environment=${JAVA_HOME}+${OS}
|
||||
not.existing.system.property=${thispropertydoesnotexist}
|
||||
baeldung.presentation=${HELLO_BAELDUNG}. Java is installed in the folder: ${JAVA_HOME}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.envvariables;
|
||||
|
||||
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.test.web.servlet.MockMvc;
|
||||
|
||||
@SpringBootTest(classes = MyController.class)
|
||||
@AutoConfigureMockMvc
|
||||
public class MyControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
/** NB : these tests are commented out because they are environment dependent
|
||||
* If you want to run one of them on your machine, follow the instruction above it
|
||||
*
|
||||
* expects the value of your system environment property 'OS' (it is already defined at least in Windows_NT)
|
||||
@Test void givenExistingSystemProperty_whenInjected_thenHasSystemPropertyValue() throws Exception {
|
||||
mockMvc.perform(get("/environment_name"))
|
||||
.andExpect(content().string(equalTo("Windows_NT")));
|
||||
}
|
||||
|
||||
* expects the value of the JAVA_HOME environment variable (you need to define it if you haven't yet), with a + and the 'OS' environment property in the end
|
||||
@Test void givenCombinationOfSystemPropertyAndEnvironmentVariable_whenInjected_thenHasExpectedValue() throws Exception {
|
||||
mockMvc.perform(get("/java_home_and_environment"))
|
||||
.andExpect(content().string(equalTo("C:\\Program Files\\Java\\jdk-11.0.14+Windows_NT")));
|
||||
}
|
||||
|
||||
* expects the content to be ${thispropertydoesnotexist} ; if you have defined an environment property called thispropertydoesnotexist, it would fail
|
||||
@Test void givenNonExistentProperty_whenInjected_thenKeepsTheStringValue() throws Exception {
|
||||
mockMvc.perform(get("/non_existent_property"))
|
||||
.andExpect(content().string(equalTo("${thispropertydoesnotexist}")));
|
||||
}
|
||||
*/
|
||||
}
|
||||
Reference in New Issue
Block a user