Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-15958
This commit is contained in:
commit
04fe87094d
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.algorithms.inoutsort;
|
||||
|
||||
public class InOutSort {
|
||||
|
||||
public static int[] reverseInPlace(int A[]) {
|
||||
int n = A.length;
|
||||
for (int i = 0; i < n / 2; i++) {
|
||||
int temp = A[i];
|
||||
A[i] = A[n - 1 - i];
|
||||
A[n - 1 - i] = temp;
|
||||
}
|
||||
|
||||
return A;
|
||||
}
|
||||
|
||||
public static int[] reverseOutOfPlace(int A[]) {
|
||||
int n = A.length;
|
||||
int[] B = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
B[n - i - 1] = A[i];
|
||||
}
|
||||
|
||||
return B;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.algorithms.inoutsort;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class InOutSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenArray_whenInPlaceSort_thenReversed() {
|
||||
int[] input = {1, 2, 3, 4, 5, 6, 7};
|
||||
int[] expected = {7, 6, 5, 4, 3, 2, 1};
|
||||
assertArrayEquals("the two arrays are not equal", expected, InOutSort.reverseInPlace(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArray_whenOutOfPlaceSort_thenReversed() {
|
||||
int[] input = {1, 2, 3, 4, 5, 6, 7};
|
||||
int[] expected = {7, 6, 5, 4, 3, 2, 1};
|
||||
assertArrayEquals("the two arrays are not equal", expected, InOutSort.reverseOutOfPlace(input));
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.incrementdecrementunaryoperators;
|
||||
package com.baeldung.unaryoperators;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.jackson.entities;
|
||||
|
||||
public class File {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
package com.baeldung.jackson.entities;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
public class Folder {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String owner;
|
||||
|
||||
private Date created;
|
||||
|
||||
private Date modified;
|
||||
|
||||
private Date lastAccess;
|
||||
|
||||
@JsonIgnore
|
||||
private List<File> files = new ArrayList<>();
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getOwner() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public void setOwner(String owner) {
|
||||
this.owner = owner;
|
||||
}
|
||||
|
||||
public Date getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
public void setCreated(Date created) {
|
||||
this.created = created;
|
||||
}
|
||||
|
||||
public Date getModified() {
|
||||
return modified;
|
||||
}
|
||||
|
||||
public void setModified(Date modified) {
|
||||
this.modified = modified;
|
||||
}
|
||||
|
||||
public Date getLastAccess() {
|
||||
return lastAccess;
|
||||
}
|
||||
|
||||
public void setLastAccess(Date lastAccess) {
|
||||
this.lastAccess = lastAccess;
|
||||
}
|
||||
|
||||
public List<File> getFiles() {
|
||||
return files;
|
||||
}
|
||||
|
||||
public void setFiles(List<File> files) {
|
||||
this.files = files;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.jackson.serialization.custom.serializer;
|
||||
|
||||
import com.baeldung.jackson.entities.Folder;
|
||||
import com.fasterxml.jackson.databind.BeanDescription;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializationConfig;
|
||||
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
|
||||
|
||||
public class FolderBeanSerializerModifier extends BeanSerializerModifier {
|
||||
|
||||
@Override
|
||||
public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
|
||||
|
||||
if (beanDesc.getBeanClass().equals(Folder.class)) {
|
||||
return new FolderSerializerWithDefaultSerializerStored((JsonSerializer<Object>) serializer);
|
||||
}
|
||||
|
||||
return serializer;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.jackson.serialization.custom.serializer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.entities.File;
|
||||
import com.baeldung.jackson.entities.Folder;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class FolderSerializer extends StdSerializer<Folder> {
|
||||
|
||||
public FolderSerializer() {
|
||||
super(Folder.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Folder value, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||
|
||||
gen.writeStartObject();
|
||||
gen.writeStringField("name", value.getName());
|
||||
|
||||
gen.writeArrayFieldStart("files");
|
||||
for (File file : value.getFiles()) {
|
||||
gen.writeStartObject();
|
||||
gen.writeNumberField("id", file.getId());
|
||||
gen.writeStringField("name", file.getName());
|
||||
gen.writeEndObject();
|
||||
}
|
||||
gen.writeEndArray();
|
||||
|
||||
gen.writeEndObject();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.jackson.serialization.custom.serializer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.entities.Folder;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class FolderSerializerWithCallingOwnSerializer extends StdSerializer<Folder> {
|
||||
|
||||
public FolderSerializerWithCallingOwnSerializer() {
|
||||
super(Folder.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Folder value, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||
|
||||
gen.writeStartObject();
|
||||
gen.writeStringField("name", value.getName());
|
||||
|
||||
provider.defaultSerializeField("files", value.getFiles(), gen);
|
||||
|
||||
provider.defaultSerializeField("details", value, gen);
|
||||
|
||||
gen.writeEndObject();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.jackson.serialization.custom.serializer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.entities.Folder;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class FolderSerializerWithDefaultSerializerStored extends StdSerializer<Folder> {
|
||||
|
||||
private final JsonSerializer<Object> defaultSerializer;
|
||||
|
||||
public FolderSerializerWithDefaultSerializerStored(JsonSerializer<Object> defaultSerializer) {
|
||||
super(Folder.class);
|
||||
this.defaultSerializer = defaultSerializer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Folder value, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||
|
||||
gen.writeStartObject();
|
||||
gen.writeStringField("name", value.getName());
|
||||
|
||||
provider.defaultSerializeField("files", value.getFiles(), gen);
|
||||
|
||||
gen.writeFieldName("details");
|
||||
defaultSerializer.serialize(value, gen, provider);
|
||||
|
||||
gen.writeEndObject();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.jackson.serialization.custom.serializer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.entities.Folder;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class FolderSerializerWithInternalObjectMapper extends StdSerializer<Folder> {
|
||||
|
||||
public FolderSerializerWithInternalObjectMapper() {
|
||||
super(Folder.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Folder value, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||
|
||||
gen.writeStartObject();
|
||||
gen.writeStringField("name", value.getName());
|
||||
|
||||
// we access internal mapper to delegate the serialization of File list
|
||||
ObjectMapper mapper = (ObjectMapper) gen.getCodec();
|
||||
|
||||
gen.writeFieldName("files");
|
||||
String stringValue = mapper.writeValueAsString(value.getFiles());
|
||||
gen.writeRawValue(stringValue);
|
||||
|
||||
gen.writeEndObject();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.jackson.serialization.custom.serializer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.baeldung.jackson.entities.Folder;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
public class FolderSerializerWithSerializerProvider extends StdSerializer<Folder> {
|
||||
|
||||
public FolderSerializerWithSerializerProvider() {
|
||||
super(Folder.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Folder value, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||
|
||||
gen.writeStartObject();
|
||||
gen.writeStringField("name", value.getName());
|
||||
|
||||
// we delegate the File list serialization to its default serializer
|
||||
provider.defaultSerializeField("files", value.getFiles(), gen);
|
||||
|
||||
gen.writeEndObject();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,166 @@
|
|||
package com.baeldung.jackson.serialization.custom.serializer;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.jackson.entities.File;
|
||||
import com.baeldung.jackson.entities.Folder;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
|
||||
public class CallingDefaultSerializerUnitTest {
|
||||
|
||||
private ObjectMapper mapper;
|
||||
private Folder mockFolder;
|
||||
private TypeReference<HashMap<String, Object>> mapType;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
||||
mapType = new TypeReference<HashMap<String, Object>>() {
|
||||
};
|
||||
|
||||
mapper = new ObjectMapper();
|
||||
|
||||
mockFolder = new Folder();
|
||||
mockFolder.setId(1L);
|
||||
mockFolder.setName("Root Folder");
|
||||
mockFolder.setOwner("root");
|
||||
mockFolder.setCreated(Date.from(Instant.now().minusSeconds(60)));
|
||||
mockFolder.setModified(Date.from(Instant.now().minusSeconds(30)));
|
||||
mockFolder.setLastAccess(Date.from(Instant.now()));
|
||||
|
||||
File file1 = new File();
|
||||
file1.setId(1L);
|
||||
file1.setName("File 1");
|
||||
|
||||
File file2 = new File();
|
||||
file2.setId(2L);
|
||||
file2.setName("File 2");
|
||||
|
||||
List<File> files = new ArrayList<>();
|
||||
files.add(file1);
|
||||
files.add(file2);
|
||||
mockFolder.setFiles(files);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFolder_whenSerialized_onlyNameAndFilesFieldsSerialized() throws IOException {
|
||||
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.addSerializer(new FolderSerializer());
|
||||
mapper.registerModule(module);
|
||||
|
||||
String json = mapper.writeValueAsString(mockFolder);
|
||||
|
||||
HashMap<String, Object> actual = mapper.readValue(json, mapType);
|
||||
|
||||
assertTrue(actual.containsKey("name"));
|
||||
assertTrue(actual.containsKey("files"));
|
||||
assertEquals(mockFolder.getName(), actual.get("name"));
|
||||
|
||||
List actualFiles = (List) actual.get("files");
|
||||
assertEquals(mockFolder.getFiles().size(), actualFiles.size());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFolder_whenSerializedWithSerializerProvider_onlyNameAndFilesFieldsSerialized() throws IOException {
|
||||
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.addSerializer(new FolderSerializerWithSerializerProvider());
|
||||
mapper.registerModule(module);
|
||||
|
||||
String json = mapper.writeValueAsString(mockFolder);
|
||||
|
||||
HashMap<String, Object> actual = mapper.readValue(json, mapType);
|
||||
|
||||
assertTrue(actual.containsKey("name"));
|
||||
assertTrue(actual.containsKey("files"));
|
||||
assertEquals(mockFolder.getName(), actual.get("name"));
|
||||
|
||||
List actualFiles = (List) actual.get("files");
|
||||
assertEquals(mockFolder.getFiles().size(), actualFiles.size());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFolder_whenSerializedWithInternalObjectMapper_onlyNameAndFilesFieldsSerialized() throws IOException {
|
||||
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.addSerializer(new FolderSerializerWithInternalObjectMapper());
|
||||
mapper.registerModule(module);
|
||||
|
||||
String json = mapper.writeValueAsString(mockFolder);
|
||||
|
||||
HashMap<String, Object> actual = mapper.readValue(json, mapType);
|
||||
|
||||
assertTrue(actual.containsKey("name"));
|
||||
assertTrue(actual.containsKey("files"));
|
||||
assertEquals(mockFolder.getName(), actual.get("name"));
|
||||
|
||||
List actualFiles = (List) actual.get("files");
|
||||
assertEquals(mockFolder.getFiles().size(), actualFiles.size());
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = StackOverflowError.class)
|
||||
public void givenFolder_whenSerializedWithCallingOwnSerializer_exceptionOccured() throws IOException {
|
||||
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.addSerializer(new FolderSerializerWithCallingOwnSerializer());
|
||||
mapper.registerModule(module);
|
||||
|
||||
mapper.writeValueAsString(mockFolder);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFolder_whenSerializedWithDefaultSerializerStored_NameAndFilesAndDetailsFieldsSerialized() throws IOException {
|
||||
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.setSerializerModifier(new FolderBeanSerializerModifier());
|
||||
mapper.registerModule(module);
|
||||
|
||||
String json = mapper.writeValueAsString(mockFolder);
|
||||
|
||||
HashMap<String, Object> actual = mapper.readValue(json, mapType);
|
||||
|
||||
assertTrue(actual.containsKey("name"));
|
||||
assertTrue(actual.containsKey("files"));
|
||||
assertEquals(mockFolder.getName(), actual.get("name"));
|
||||
|
||||
List actualFiles = (List) actual.get("files");
|
||||
assertEquals(mockFolder.getFiles().size(), actualFiles.size());
|
||||
|
||||
Map actualDetails = (Map) actual.get("details");
|
||||
assertTrue(actualDetails.containsKey("id"));
|
||||
assertTrue(actualDetails.containsKey("name"));
|
||||
assertTrue(actualDetails.containsKey("owner"));
|
||||
assertTrue(actualDetails.containsKey("created"));
|
||||
assertTrue(actualDetails.containsKey("modified"));
|
||||
assertTrue(actualDetails.containsKey("lastAccess"));
|
||||
|
||||
assertEquals(mockFolder.getId().longValue(), ((Number)actualDetails.get("id")).longValue());
|
||||
assertEquals(mockFolder.getName(), actualDetails.get("name"));
|
||||
assertEquals(mockFolder.getOwner(), actualDetails.get("owner"));
|
||||
assertEquals(mockFolder.getCreated(), new Date((long) actualDetails.get("created")));
|
||||
assertEquals(mockFolder.getModified(), new Date((long) actualDetails.get("modified")));
|
||||
assertEquals(mockFolder.getLastAccess(), new Date((long) actualDetails.get("lastAccess")));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.algorithms.gcd;
|
||||
|
||||
public class GCDImplementation {
|
||||
|
||||
public static int gcdByBruteForce(int n1, int n2) {
|
||||
int gcd = 1;
|
||||
for (int i = 1; i <= n1 && i <= n2; i++) {
|
||||
if (n1 % i == 0 && n2 % i == 0) {
|
||||
gcd = i;
|
||||
}
|
||||
}
|
||||
return gcd;
|
||||
}
|
||||
|
||||
public static int gcdByEuclidsAlgorithm(int n1, int n2) {
|
||||
if (n2 == 0) {
|
||||
return n1;
|
||||
}
|
||||
return gcdByEuclidsAlgorithm(n2, n1 % n2);
|
||||
}
|
||||
|
||||
public static int gcdBySteinsAlgorithm(int n1, int n2) {
|
||||
if (n1 == 0) {
|
||||
return n2;
|
||||
}
|
||||
|
||||
if (n2 == 0) {
|
||||
return n1;
|
||||
}
|
||||
|
||||
int n;
|
||||
for (n = 0; ((n1 | n2) & 1) == 0; n++) {
|
||||
n1 >>= 1;
|
||||
n2 >>= 1;
|
||||
}
|
||||
|
||||
while ((n1 & 1) == 0) {
|
||||
n1 >>= 1;
|
||||
}
|
||||
|
||||
do {
|
||||
while ((n2 & 1) == 0) {
|
||||
n2 >>= 1;
|
||||
}
|
||||
|
||||
if (n1 > n2) {
|
||||
int temp = n1;
|
||||
n1 = n2;
|
||||
n2 = temp;
|
||||
}
|
||||
n2 = (n2 - n1);
|
||||
} while (n2 != 0);
|
||||
return n1 << n;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.algorithms.gcd;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class GCDImplementationUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCalculatingGCDByBruteForceMethod_thenCorrect() {
|
||||
int n1 = 60;
|
||||
int n2 = 90;
|
||||
int gcd = GCDImplementation.gcdByBruteForce(n1, n2);
|
||||
assertThat(gcd).isEqualTo(30);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingGCDByEuclidsAlgorithm_thenCorrect() {
|
||||
int n1 = 60;
|
||||
int n2 = 90;
|
||||
int gcd = GCDImplementation.gcdByEuclidsAlgorithm(n1, n2);
|
||||
assertThat(gcd).isEqualTo(30);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculatingGCDBySteinsAlgorithm_thenCorrect() {
|
||||
int n1 = 60;
|
||||
int n2 = 90;
|
||||
int gcd = GCDImplementation.gcdBySteinsAlgorithm(n1, n2);
|
||||
assertThat(gcd).isEqualTo(30);
|
||||
}
|
||||
}
|
|
@ -55,7 +55,7 @@
|
|||
|
||||
<properties>
|
||||
<rest-assured.version>3.1.0</rest-assured.version>
|
||||
<spring-boot.version>1.5.19.RELEASE</spring-boot.version>
|
||||
<spring-boot.version>1.5.22.RELEASE</spring-boot.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -78,6 +78,6 @@
|
|||
<rest-assured.version>3.3.0</rest-assured.version>
|
||||
<!-- plugins -->
|
||||
<thin.version>1.0.22.RELEASE</thin.version>
|
||||
<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
|
||||
<spring-boot.version>2.1.7.RELEASE</spring-boot.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [Better Retries with Exponential Backoff and Jitter](https://baeldung.com/retries-with-exponential-backoff-and-jitter)
|
||||
- [Better Retries with Exponential Backoff and Jitter](https://www.baeldung.com/resilience4j-backoff-jitter)
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [A Guide to Sql2o](http://www.baeldung.com/sql2o)
|
||||
- [A Guide to Sql2o](https://www.baeldung.com/java-sql2o)
|
||||
|
|
|
@ -11,7 +11,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
### Relevant articles:
|
||||
|
||||
- [Guide to Spring @Autowired](http://www.baeldung.com/spring-autowire)
|
||||
- [Properties with Spring and Spring Boot](http://www.baeldung.com/properties-with-spring) - checkout the `org.baeldung.properties` package for all scenarios of properties injection and usage
|
||||
- [Spring Profiles](http://www.baeldung.com/spring-profiles)
|
||||
- [A Spring Custom Annotation for a Better DAO](http://www.baeldung.com/spring-annotation-bean-pre-processor)
|
||||
- [What’s New in Spring 4.3?](http://www.baeldung.com/whats-new-in-spring-4-3)
|
||||
|
|
|
@ -19,7 +19,7 @@ public class SpringDataJPAIntegrationTest {
|
|||
private ApplicationContext context;
|
||||
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void givenAutoconfigurationIsDisable_whenApplicationStarts_thenContextWillNotHaveTheAutoconfiguredClasses() {
|
||||
public void givenAutoConfigDisabled_whenStarting_thenNoAutoconfiguredBeansInContext() {
|
||||
context.getBean(DataSource.class);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ public class SpringDataMongoDBIntegrationTest {
|
|||
private ApplicationContext context;
|
||||
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void givenAutoconfigurationIsDisable_whenApplicationStarts_thenContextWillNotHaveTheAutoconfiguredClasses() {
|
||||
public void givenAutoConfigDisabled_whenStarting_thenNoAutoconfiguredBeansInContext() {
|
||||
context.getBean(MongoTemplate.class);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ public class SpringDataRedisIntegrationTest {
|
|||
private ApplicationContext context;
|
||||
|
||||
@Test(expected = NoSuchBeanDefinitionException.class)
|
||||
public void givenAutoconfigurationIsDisable_whenApplicationStarts_thenContextWillNotHaveTheAutoconfiguredClasses() {
|
||||
public void givenAutoConfigDisabled_whenStarting_thenNoAutoconfiguredBeansInContext() {
|
||||
context.getBean(RedisTemplate.class);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,2 +1,10 @@
|
|||
### Relevant Articles:
|
||||
- [Reloading Properties in Spring](https://www.baeldung.com/reloading-properties-files-in-spring/)
|
||||
- [Reloading Properties in Spring](https://www.baeldung.com/reloading-properties-files-in-spring/)
|
||||
- [Guide to @ConfigurationProperties in Spring Boot](http://www.baeldung.com/configuration-properties-in-spring-boot)
|
||||
- [Load Spring Boot Properties From a JSON File](https://www.baeldung.com/spring-boot-json-properties)
|
||||
- [Guide to @EnableConfigurationProperties](https://www.baeldung.com/spring-enable-config-properties)
|
||||
- [Properties with Spring and Spring Boot](http://www.baeldung.com/properties-with-spring) - checkout the `org.baeldung.properties` package for all scenarios of properties injection and usage
|
||||
- [A Quick Guide to Spring @Value](http://www.baeldung.com/spring-value-annotation)
|
||||
- [Spring YAML Configuration](http://www.baeldung.com/spring-yaml)
|
||||
- [Using Spring @Value with Defaults](http://www.baeldung.com/spring-value-defaults)
|
||||
- [How to Inject a Property Value Into a Class Not Managed by Spring?](http://www.baeldung.com/inject-properties-value-non-spring-class)
|
|
@ -37,6 +37,16 @@
|
|||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
<version>${httpcore.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
|
@ -99,6 +109,8 @@
|
|||
<java.version>1.8</java.version>
|
||||
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
|
||||
<commons-configuration.version>1.10</commons-configuration.version>
|
||||
<guava.version>20.0</guava.version>
|
||||
<httpcore.version>4.4.11</httpcore.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.properties;
|
||||
package com.baeldung.properties;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
@ -1,7 +1,9 @@
|
|||
package org.baeldung.properties;
|
||||
package com.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "additional")
|
||||
public class AdditionalProperties {
|
||||
|
|
@ -10,6 +10,7 @@ import javax.validation.constraints.Pattern;
|
|||
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
@ -54,7 +55,7 @@ public class ConfigProperties {
|
|||
}
|
||||
|
||||
@NotBlank
|
||||
private String host;
|
||||
private String hostName;
|
||||
|
||||
@Min(1025)
|
||||
@Max(65536)
|
||||
|
@ -67,12 +68,12 @@ public class ConfigProperties {
|
|||
private List<String> defaultRecipients;
|
||||
private Map<String, String> additionalHeaders;
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
public String getHostName() {
|
||||
return hostName;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
public void setHostName(String hostName) {
|
||||
this.hostName = hostName;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
|
@ -114,4 +115,10 @@ public class ConfigProperties {
|
|||
public void setAdditionalHeaders(Map<String, String> additionalHeaders) {
|
||||
this.additionalHeaders = additionalHeaders;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "item")
|
||||
public Item item(){
|
||||
return new Item();
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.properties;
|
||||
package com.baeldung.properties;
|
||||
|
||||
public class Item {
|
||||
|
|
@ -22,3 +22,6 @@ item.name=Item name
|
|||
item.size=42
|
||||
|
||||
|
||||
#Additional properties
|
||||
additional.unit=km
|
||||
additional.max=100
|
|
@ -0,0 +1,2 @@
|
|||
conversion.timeInDefaultUnit=10
|
||||
conversion.timeInNano=9ns
|
|
@ -1,8 +1,5 @@
|
|||
package com.baeldung.properties;
|
||||
|
||||
import org.baeldung.properties.AdditionalProperties;
|
||||
import org.baeldung.properties.ConfigProperties;
|
||||
import org.baeldung.properties.ConfigPropertiesDemoApplication;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
|
@ -1,4 +1,4 @@
|
|||
package org.baeldung.properties;
|
||||
package com.baeldung.properties;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
###### time unit
|
||||
conversion.timeInDefaultUnit=10
|
||||
conversion.timeInNano=9ns
|
||||
conversion.timeInDays=2
|
||||
|
||||
###### data size
|
||||
conversion.sizeInDefaultUnit=300
|
||||
conversion.sizeInGB=2GB
|
||||
conversion.sizeInTB=4
|
||||
|
||||
conversion.employee=john,2000
|
|
@ -14,7 +14,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
- [Custom Information in Spring Boot Info Endpoint](http://www.baeldung.com/spring-boot-info-actuator-custom)
|
||||
- [Using @JsonComponent in Spring Boot](http://www.baeldung.com/spring-boot-jsoncomponent)
|
||||
- [Testing in Spring Boot](http://www.baeldung.com/spring-boot-testing)
|
||||
- [Guide to @ConfigurationProperties in Spring Boot](http://www.baeldung.com/configuration-properties-in-spring-boot)
|
||||
- [How to Get All Spring-Managed Beans?](http://www.baeldung.com/spring-show-all-beans)
|
||||
- [Spring Boot and Togglz Aspect](http://www.baeldung.com/spring-togglz)
|
||||
- [Getting Started with GraphQL and Spring Boot](http://www.baeldung.com/spring-graphql)
|
||||
|
@ -29,9 +28,7 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||
- [Spring Shutdown Callbacks](http://www.baeldung.com/spring-shutdown-callbacks)
|
||||
- [Container Configuration in Spring Boot 2](http://www.baeldung.com/embeddedservletcontainercustomizer-configurableembeddedservletcontainer-spring-boot)
|
||||
- [Introduction to Chaos Monkey](https://www.baeldung.com/spring-boot-chaos-monkey)
|
||||
- [Load Spring Boot Properties From a JSON File](https://www.baeldung.com/spring-boot-json-properties)
|
||||
- [Display Auto-Configuration Report in Spring Boot](https://www.baeldung.com/spring-boot-auto-configuration-report)
|
||||
- [Injecting Git Information Into Spring](https://www.baeldung.com/spring-git-information)
|
||||
- [Validation in Spring Boot](https://www.baeldung.com/spring-boot-bean-validation)
|
||||
- [Guide to Creating and Running a Jar File in Java](https://www.baeldung.com/java-create-jar)
|
||||
- [Guide to @EnableConfigurationProperties](https://www.baeldung.com/spring-enable-config-properties)
|
||||
|
|
|
@ -1,90 +0,0 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@Configuration
|
||||
@PropertySource("classpath:configprops.properties")
|
||||
@ConfigurationProperties(prefix = "mail")
|
||||
@Validated
|
||||
public class ConfigProperties {
|
||||
|
||||
@NotBlank
|
||||
private String hostName;
|
||||
|
||||
@Min(1025)
|
||||
@Max(65536)
|
||||
private int port;
|
||||
|
||||
@Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$")
|
||||
private String from;
|
||||
|
||||
private List<String> defaultRecipients;
|
||||
private Map<String, String> additionalHeaders;
|
||||
private Credentials credentials;
|
||||
|
||||
public String getHostName() {
|
||||
return hostName;
|
||||
}
|
||||
|
||||
public void setHostName(String hostName) {
|
||||
this.hostName = hostName;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public List<String> getDefaultRecipients() {
|
||||
return defaultRecipients;
|
||||
}
|
||||
|
||||
public void setDefaultRecipients(List<String> defaultRecipients) {
|
||||
this.defaultRecipients = defaultRecipients;
|
||||
}
|
||||
|
||||
public Map<String, String> getAdditionalHeaders() {
|
||||
return additionalHeaders;
|
||||
}
|
||||
|
||||
public void setAdditionalHeaders(Map<String, String> additionalHeaders) {
|
||||
this.additionalHeaders = additionalHeaders;
|
||||
}
|
||||
|
||||
public Credentials getCredentials() {
|
||||
return credentials;
|
||||
}
|
||||
|
||||
public void setCredentials(Credentials credentials) {
|
||||
this.credentials = credentials;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "item")
|
||||
public Item item(){
|
||||
return new Item();
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackageClasses = {ConfigProperties.class,
|
||||
JsonProperties.class,
|
||||
CustomJsonProperties.class,
|
||||
AdditionalConfiguration.class})
|
||||
public class ConfigPropertiesDemoApplication {
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer())
|
||||
.run();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@Validated
|
||||
public class Credentials {
|
||||
|
||||
@Length(max = 4, min = 1)
|
||||
private String authMethod;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public String getAuthMethod() {
|
||||
return authMethod;
|
||||
}
|
||||
|
||||
public void setAuthMethod(String authMethod) {
|
||||
this.authMethod = authMethod;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "custom")
|
||||
public class CustomJsonProperties {
|
||||
|
||||
private String host;
|
||||
|
||||
private int port;
|
||||
|
||||
private boolean resend;
|
||||
|
||||
private Person sender;
|
||||
|
||||
public static class Person {
|
||||
|
||||
private String name;
|
||||
private String address;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public boolean isResend() {
|
||||
return resend;
|
||||
}
|
||||
|
||||
public void setResend(boolean resend) {
|
||||
this.resend = resend;
|
||||
}
|
||||
|
||||
public Person getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(Person sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
package org.baeldung.properties;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class)
|
||||
@ConfigurationProperties
|
||||
public class JsonProperties {
|
||||
|
||||
private String host;
|
||||
|
||||
private int port;
|
||||
|
||||
private boolean resend;
|
||||
|
||||
private List<String> topics;
|
||||
|
||||
private LinkedHashMap<String, ?> sender;
|
||||
|
||||
public LinkedHashMap<String, ?> getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(LinkedHashMap<String, ?> sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
public List<String> getTopics() {
|
||||
return topics;
|
||||
}
|
||||
|
||||
public void setTopics(List<String> topics) {
|
||||
this.topics = topics;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public boolean isResend() {
|
||||
return resend;
|
||||
}
|
||||
|
||||
public void setResend(boolean resend) {
|
||||
this.resend = resend;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue