Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
4526309cf2
|
@ -4,5 +4,5 @@ This module contains articles about Java 11 core features
|
|||
|
||||
### Relevant articles
|
||||
- [Guide to Java 8 Optional](https://www.baeldung.com/java-optional)
|
||||
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
|
||||
- [Guide to Java 8’s Collectors](https://www.baeldung.com/java-8-collectors)
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public abstract class Animal implements Eating {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public class Bird extends Animal {
|
||||
private boolean walks;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public interface Eating {
|
||||
String eats();
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public class Goat extends Animal implements Locomotion {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
@Greeter(greet="Good morning")
|
||||
public class Greetings {
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public interface Locomotion {
|
||||
String getLocomotion();
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public class Operations {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertFalse;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.java.reflection;
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
@ -32,23 +32,23 @@ public class ReflectionUnitTest {
|
|||
final Class<?> clazz = goat.getClass();
|
||||
|
||||
assertEquals("Goat", clazz.getSimpleName());
|
||||
assertEquals("com.baeldung.java.reflection.Goat", clazz.getName());
|
||||
assertEquals("com.baeldung.java.reflection.Goat", clazz.getCanonicalName());
|
||||
assertEquals("com.baeldung.reflection.Goat", clazz.getName());
|
||||
assertEquals("com.baeldung.reflection.Goat", clazz.getCanonicalName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClassName_whenCreatesObject_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> clazz = Class.forName("com.baeldung.java.reflection.Goat");
|
||||
final Class<?> clazz = Class.forName("com.baeldung.reflection.Goat");
|
||||
|
||||
assertEquals("Goat", clazz.getSimpleName());
|
||||
assertEquals("com.baeldung.java.reflection.Goat", clazz.getName());
|
||||
assertEquals("com.baeldung.java.reflection.Goat", clazz.getCanonicalName());
|
||||
assertEquals("com.baeldung.reflection.Goat", clazz.getName());
|
||||
assertEquals("com.baeldung.reflection.Goat", clazz.getCanonicalName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenRecognisesModifiers_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> goatClass = Class.forName("com.baeldung.java.reflection.Goat");
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
|
||||
final Class<?> goatClass = Class.forName("com.baeldung.reflection.Goat");
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.reflection.Animal");
|
||||
final int goatMods = goatClass.getModifiers();
|
||||
final int animalMods = animalClass.getModifiers();
|
||||
|
||||
|
@ -63,7 +63,7 @@ public class ReflectionUnitTest {
|
|||
final Class<?> goatClass = goat.getClass();
|
||||
final Package pkg = goatClass.getPackage();
|
||||
|
||||
assertEquals("com.baeldung.java.reflection", pkg.getName());
|
||||
assertEquals("com.baeldung.reflection", pkg.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -81,8 +81,8 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenClass_whenGetsImplementedInterfaces_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> goatClass = Class.forName("com.baeldung.java.reflection.Goat");
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
|
||||
final Class<?> goatClass = Class.forName("com.baeldung.reflection.Goat");
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.reflection.Animal");
|
||||
final Class<?>[] goatInterfaces = goatClass.getInterfaces();
|
||||
final Class<?>[] animalInterfaces = animalClass.getInterfaces();
|
||||
|
||||
|
@ -94,16 +94,16 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenClass_whenGetsConstructor_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> goatClass = Class.forName("com.baeldung.java.reflection.Goat");
|
||||
final Class<?> goatClass = Class.forName("com.baeldung.reflection.Goat");
|
||||
final Constructor<?>[] constructors = goatClass.getConstructors();
|
||||
|
||||
assertEquals(1, constructors.length);
|
||||
assertEquals("com.baeldung.java.reflection.Goat", constructors[0].getName());
|
||||
assertEquals("com.baeldung.reflection.Goat", constructors[0].getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClass_whenGetsFields_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.reflection.Animal");
|
||||
final Field[] fields = animalClass.getDeclaredFields();
|
||||
|
||||
final List<String> actualFields = getFieldNames(fields);
|
||||
|
@ -114,7 +114,7 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenClass_whenGetsMethods_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.java.reflection.Animal");
|
||||
final Class<?> animalClass = Class.forName("com.baeldung.reflection.Animal");
|
||||
final Method[] methods = animalClass.getDeclaredMethods();
|
||||
final List<String> actualMethods = getMethodNames(methods);
|
||||
|
||||
|
@ -124,7 +124,7 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenClass_whenGetsAllConstructors_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Constructor<?>[] constructors = birdClass.getConstructors();
|
||||
|
||||
assertEquals(3, constructors.length);
|
||||
|
@ -132,7 +132,7 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenClass_whenGetsEachConstructorByParamTypes_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
birdClass.getConstructor();
|
||||
birdClass.getConstructor(String.class);
|
||||
birdClass.getConstructor(String.class, boolean.class);
|
||||
|
@ -140,7 +140,7 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenClass_whenInstantiatesObjectsAtRuntime_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
|
||||
final Constructor<?> cons1 = birdClass.getConstructor();
|
||||
final Constructor<?> cons2 = birdClass.getConstructor(String.class);
|
||||
|
@ -159,7 +159,7 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenClass_whenGetsPublicFields_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Field[] fields = birdClass.getFields();
|
||||
assertEquals(1, fields.length);
|
||||
assertEquals("CATEGORY", fields[0].getName());
|
||||
|
@ -168,7 +168,7 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenClass_whenGetsPublicFieldByName_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Field field = birdClass.getField("CATEGORY");
|
||||
assertEquals("CATEGORY", field.getName());
|
||||
|
||||
|
@ -176,7 +176,7 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenClass_whenGetsDeclaredFields_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Field[] fields = birdClass.getDeclaredFields();
|
||||
assertEquals(1, fields.length);
|
||||
assertEquals("walks", fields[0].getName());
|
||||
|
@ -184,7 +184,7 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenClass_whenGetsFieldsByName_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Field field = birdClass.getDeclaredField("walks");
|
||||
assertEquals("walks", field.getName());
|
||||
|
||||
|
@ -192,14 +192,14 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenClassField_whenGetsType_thenCorrect() throws Exception {
|
||||
final Field field = Class.forName("com.baeldung.java.reflection.Bird").getDeclaredField("walks");
|
||||
final Field field = Class.forName("com.baeldung.reflection.Bird").getDeclaredField("walks");
|
||||
final Class<?> fieldClass = field.getType();
|
||||
assertEquals("boolean", fieldClass.getSimpleName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClassField_whenSetsAndGetsValue_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Bird bird = (Bird) birdClass.getConstructor().newInstance();
|
||||
final Field field = birdClass.getDeclaredField("walks");
|
||||
field.setAccessible(true);
|
||||
|
@ -216,7 +216,7 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenClassField_whenGetsAndSetsWithNull_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Field field = birdClass.getField("CATEGORY");
|
||||
field.setAccessible(true);
|
||||
|
||||
|
@ -225,7 +225,7 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenClass_whenGetsAllPublicMethods_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Method[] methods = birdClass.getMethods();
|
||||
final List<String> methodNames = getMethodNames(methods);
|
||||
|
||||
|
@ -235,7 +235,7 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenClass_whenGetsOnlyDeclaredMethods_thenCorrect() throws ClassNotFoundException {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final List<String> actualMethodNames = getMethodNames(birdClass.getDeclaredMethods());
|
||||
|
||||
final List<String> expectedMethodNames = Arrays.asList("setWalks", "walks", "getSound", "eats");
|
||||
|
@ -248,24 +248,17 @@ public class ReflectionUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenMethodName_whenGetsMethod_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Method walksMethod = birdClass.getDeclaredMethod("walks");
|
||||
final Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class);
|
||||
|
||||
assertFalse(walksMethod.isAccessible());
|
||||
assertFalse(setWalksMethod.isAccessible());
|
||||
|
||||
walksMethod.setAccessible(true);
|
||||
setWalksMethod.setAccessible(true);
|
||||
|
||||
assertTrue(walksMethod.isAccessible());
|
||||
assertTrue(setWalksMethod.isAccessible());
|
||||
final Bird bird = new Bird();
|
||||
final Method walksMethod = bird.getClass().getDeclaredMethod("walks");
|
||||
final Method setWalksMethod = bird.getClass().getDeclaredMethod("setWalks", boolean.class);
|
||||
|
||||
assertTrue(walksMethod.canAccess(bird));
|
||||
assertTrue(setWalksMethod.canAccess(bird));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethod_whenInvokes_thenCorrect() throws Exception {
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
|
||||
final Class<?> birdClass = Class.forName("com.baeldung.reflection.Bird");
|
||||
final Bird bird = (Bird) birdClass.getConstructor().newInstance();
|
||||
final Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class);
|
||||
final Method walksMethod = birdClass.getDeclaredMethod("walks");
|
|
@ -0,0 +1,86 @@
|
|||
package com.baeldung.collections.removeallperformance;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Level;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
import org.openjdk.jmh.runner.Runner;
|
||||
import org.openjdk.jmh.runner.options.Options;
|
||||
import org.openjdk.jmh.runner.options.OptionsBuilder;
|
||||
|
||||
import com.baeldung.collections.containsperformance.Employee;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@Warmup(iterations = 5)
|
||||
public class HashSetBenchmark {
|
||||
|
||||
@State(Scope.Thread)
|
||||
public static class MyState {
|
||||
private Set<Employee> employeeSet1 = new HashSet<>();
|
||||
private List<Employee> employeeList1 = new ArrayList<>();
|
||||
private Set<Employee> employeeSet2 = new HashSet<>();
|
||||
private List<Employee> employeeList2 = new ArrayList<>();
|
||||
|
||||
private long set1Size = 60000;
|
||||
private long list1Size = 50000;
|
||||
private long set2Size = 50000;
|
||||
private long list2Size = 60000;
|
||||
|
||||
@Setup(Level.Trial)
|
||||
public void setUp() {
|
||||
|
||||
for (long i = 0; i < set1Size; i++) {
|
||||
employeeSet1.add(new Employee(i, RandomStringUtils.random(7, true, false)));
|
||||
}
|
||||
|
||||
for (long i = 0; i < list1Size; i++) {
|
||||
employeeList1.add(new Employee(i, RandomStringUtils.random(7, true, false)));
|
||||
}
|
||||
|
||||
for (long i = 0; i < set2Size; i++) {
|
||||
employeeSet2.add(new Employee(i, RandomStringUtils.random(7, true, false)));
|
||||
}
|
||||
|
||||
for (long i = 0; i < list2Size; i++) {
|
||||
employeeList2.add(new Employee(i, RandomStringUtils.random(7, true, false)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean given_SizeOfHashsetGreaterThanSizeOfCollection_When_RemoveAllFromHashSet_Then_GoodPerformance(MyState state) {
|
||||
return state.employeeSet1.removeAll(state.employeeList1);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean given_SizeOfHashsetSmallerThanSizeOfCollection_When_RemoveAllFromHashSet_Then_BadPerformance(MyState state) {
|
||||
return state.employeeSet2.removeAll(state.employeeList2);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Options options = new OptionsBuilder().include(HashSetBenchmark.class.getSimpleName())
|
||||
.threads(1)
|
||||
.forks(1)
|
||||
.shouldFailOnError(true)
|
||||
.shouldDoGC(true)
|
||||
.jvmArgs("-server")
|
||||
.build();
|
||||
new Runner(options).run();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.collections.hashset;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class HashSetUnitTest {
|
||||
|
||||
@Test
|
||||
void whenRemoveAllFromHashset_thenRemovesAllElementsFromHashsetThatArePresentInCollection() {
|
||||
Set<Integer> set = new HashSet<>();
|
||||
Collection<Integer> collection = new ArrayList<>();
|
||||
set.add(1);
|
||||
set.add(2);
|
||||
set.add(3);
|
||||
set.add(4);
|
||||
collection.add(1);
|
||||
collection.add(3);
|
||||
|
||||
set.removeAll(collection);
|
||||
|
||||
assertEquals(2, set.size());
|
||||
Integer[] actualElements = new Integer[set.size()];
|
||||
Integer[] expectedElements = new Integer[] { 2, 4 };
|
||||
assertArrayEquals(expectedElements, set.toArray(actualElements));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.checkclassexistence;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CheckClassExistenceUnitTest {
|
||||
|
||||
public static class InitializingClass {
|
||||
static {
|
||||
if (true) { //enable throwing of an exception in a static initialization block
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = ClassNotFoundException.class) //thrown when class does not exist
|
||||
public void givenNonExistingClass_whenUsingForName_thenClassNotFound() throws ClassNotFoundException {
|
||||
Class.forName("class.that.does.not.exist");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistingClass_whenUsingForName_thenNoException() throws ClassNotFoundException {
|
||||
Class.forName("java.lang.String");
|
||||
}
|
||||
|
||||
@Test(expected = ExceptionInInitializerError.class) //thrown when exception occurs inside of a static initialization block
|
||||
public void givenInitializingClass_whenUsingForName_thenInitializationError() throws ClassNotFoundException {
|
||||
Class.forName("com.baeldung.checkclassexistence.CheckClassExistenceUnitTest$InitializingClass");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInitializingClass_whenUsingForNameWithoutInitialization_thenNoException() throws ClassNotFoundException {
|
||||
Class.forName("com.baeldung.checkclassexistence.CheckClassExistenceUnitTest$InitializingClass", false, getClass().getClassLoader());
|
||||
}
|
||||
}
|
|
@ -35,6 +35,12 @@
|
|||
<artifactId>async-http-client</artifactId>
|
||||
<version>${async-http-client.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.icegreen</groupId>
|
||||
<artifactId>greenmail</artifactId>
|
||||
<version>1.5.8</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
package com.baeldung.mail.mailwithattachment;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
import javax.mail.BodyPart;
|
||||
import javax.mail.Message;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.Multipart;
|
||||
import javax.mail.PasswordAuthentication;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Transport;
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeBodyPart;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import javax.mail.internet.MimeMultipart;
|
||||
|
||||
public class MailWithAttachmentService {
|
||||
|
||||
private String username = "";
|
||||
private String password = "";
|
||||
private String host = "";
|
||||
private String port = "";
|
||||
|
||||
MailWithAttachmentService() {
|
||||
}
|
||||
|
||||
MailWithAttachmentService(String username, String password, String host, String port) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public Session getSession() {
|
||||
Properties props = new Properties();
|
||||
props.put("mail.smtp.auth", "true");
|
||||
props.put("mail.smtp.starttls.enable", "true");
|
||||
props.put("mail.smtp.host", this.host);
|
||||
props.put("mail.smtp.port", this.port);
|
||||
|
||||
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(username, password);
|
||||
}
|
||||
});
|
||||
return session;
|
||||
}
|
||||
|
||||
public Message createMail(Session session) throws AddressException, MessagingException, IOException {
|
||||
Message message = new MimeMessage(session);
|
||||
message.setFrom(new InternetAddress("mail@gmail.com"));
|
||||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("mail@gmail.com"));
|
||||
message.setSubject("Testing Subject");
|
||||
|
||||
BodyPart messageBodyPart = new MimeBodyPart();
|
||||
messageBodyPart.setText("This is message body");
|
||||
|
||||
Multipart multipart = new MimeMultipart();
|
||||
multipart.addBodyPart(messageBodyPart);
|
||||
|
||||
MimeBodyPart attachmentPart = new MimeBodyPart();
|
||||
MimeBodyPart attachmentPart2 = new MimeBodyPart();
|
||||
|
||||
attachmentPart.attachFile(new File("C:\\Document1.txt"));
|
||||
attachmentPart2.attachFile(new File("C:\\Document2.txt"));
|
||||
|
||||
multipart.addBodyPart(attachmentPart);
|
||||
multipart.addBodyPart(attachmentPart2);
|
||||
|
||||
message.setContent(multipart);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
public void sendMail(Session session) throws MessagingException, IOException {
|
||||
|
||||
Message message = createMail(session);
|
||||
Transport.send(message);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.mail.mailwithattachment;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import javax.annotation.Resource;
|
||||
import javax.mail.Session;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.mail.mailwithattachment.MailWithAttachmentService;
|
||||
import com.icegreen.greenmail.util.GreenMail;
|
||||
import com.icegreen.greenmail.util.ServerSetupTest;
|
||||
|
||||
public class MailWithAttachmentServiceLiveTest {
|
||||
|
||||
@Resource
|
||||
private MailWithAttachmentService emailService;
|
||||
private GreenMail greenMail;
|
||||
|
||||
@Before
|
||||
public void startMailServer() {
|
||||
emailService = new MailWithAttachmentService();
|
||||
greenMail = new GreenMail(ServerSetupTest.SMTP);
|
||||
greenMail.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void stopMailServer() {
|
||||
greenMail.stop();
|
||||
emailService = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canSendMail() {
|
||||
try {
|
||||
Session testSession = greenMail.getSmtp()
|
||||
.createSession();
|
||||
emailService.sendMail(testSession);
|
||||
assertEquals(1, greenMail.getReceivedMessages().length);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -3,7 +3,6 @@
|
|||
- [Void Type in Java](https://www.baeldung.com/java-void-type)
|
||||
- [Retrieve Fields from a Java Class Using Reflection](https://www.baeldung.com/java-reflection-class-fields)
|
||||
- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection)
|
||||
- [Guide to Java Reflection](http://www.baeldung.com/java-reflection)
|
||||
- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection)
|
||||
- [Changing Annotation Parameters At Runtime](http://www.baeldung.com/java-reflection-change-annotation-params)
|
||||
- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies)
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
apply plugin: "java"
|
||||
apply plugin: "application"
|
||||
description = "Gradle Command Line Arguments examples"
|
||||
|
||||
ext.javaMainClass = "com.baeldung.cmd.MainClass"
|
||||
|
||||
application {
|
||||
mainClassName = javaMainClass
|
||||
}
|
||||
|
||||
task propertyTypes(){
|
||||
doLast{
|
||||
if (project.hasProperty("args")) {
|
||||
println "Our input argument with project property ["+project.getProperty("args")+"]"
|
||||
}
|
||||
println "Our input argument with system property ["+System.getProperty("args")+"]"
|
||||
}
|
||||
}
|
||||
|
||||
if (project.hasProperty("args")) {
|
||||
ext.cmdargs = project.getProperty("args")
|
||||
} else {
|
||||
ext.cmdargs = "ls"
|
||||
}
|
||||
|
||||
task cmdLineJavaExec(type: JavaExec) {
|
||||
group = "Execution"
|
||||
description = "Run the main class with JavaExecTask"
|
||||
classpath = sourceSets.main.runtimeClasspath
|
||||
main = javaMainClass
|
||||
args cmdargs.split()
|
||||
}
|
||||
|
||||
task cmdLineExec(type: Exec) {
|
||||
group = "Execution"
|
||||
description = "Run an external program with ExecTask"
|
||||
commandLine cmdargs.split()
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.cmd;
|
||||
|
||||
public class MainClass {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Gradle command line arguments example");
|
||||
for (String arg : args) {
|
||||
System.out.println("Got argument [" + arg + "]");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
rootProject.name='gradle-5-articles'
|
||||
include 'java-exec'
|
||||
include 'unused-dependencies'
|
||||
include 'source-sets'
|
||||
include 'source-sets'
|
||||
include 'cmd-line-args'
|
|
@ -15,6 +15,12 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.17.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.baeldung.arrayconversion;
|
||||
|
||||
import org.assertj.core.api.ListAssert;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -7,8 +8,7 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ArrayToListConversionUnitTest {
|
||||
|
||||
|
@ -17,8 +17,8 @@ public class ArrayToListConversionUnitTest {
|
|||
String[] stringArray = new String[] { "A", "B", "C", "D" };
|
||||
List<String> stringList = Arrays.asList(stringArray);
|
||||
stringList.set(0, "E");
|
||||
assertThat(stringList, CoreMatchers.hasItems("E", "B", "C", "D"));
|
||||
assertArrayEquals(stringArray, new String[] { "E", "B", "C", "D" });
|
||||
assertThat(stringList).containsExactly("E", "B", "C", "D");
|
||||
assertThat(stringArray).containsExactly("E", "B", "C", "D");
|
||||
stringList.add("F");
|
||||
}
|
||||
|
||||
|
@ -27,9 +27,9 @@ public class ArrayToListConversionUnitTest {
|
|||
String[] stringArray = new String[] { "A", "B", "C", "D" };
|
||||
List<String> stringList = new ArrayList<>(Arrays.asList(stringArray));
|
||||
stringList.set(0, "E");
|
||||
assertThat(stringList, CoreMatchers.hasItems("E", "B", "C", "D"));
|
||||
assertArrayEquals(stringArray, new String[] { "A", "B", "C", "D" });
|
||||
assertThat(stringList).containsExactly("E", "B", "C", "D");
|
||||
assertThat(stringArray).containsExactly("A", "B", "C", "D");
|
||||
stringList.add("F");
|
||||
assertThat(stringList, CoreMatchers.hasItems("E", "B", "C", "D", "F"));
|
||||
assertThat(stringList).containsExactly("E", "B", "C", "D", "F");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>logging-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -79,6 +79,8 @@
|
|||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>logging-service</finalName>
|
||||
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
@ -93,13 +95,21 @@
|
|||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<finalName>logging-service</finalName>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<log4j2.version>2.7</log4j2.version>
|
||||
<disruptor.version>3.3.6</disruptor.version>
|
||||
<jbosslogging.version>3.3.0.Final</jbosslogging.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -9,9 +9,9 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<artifactId>logging-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -7,9 +7,9 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<artifactId>logging-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -9,9 +9,9 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<artifactId>logging-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>..</relativePath>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
|
@ -21,4 +21,17 @@
|
|||
<module>flogger</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.22.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.entitymodule</groupId>
|
||||
<artifactId>entitymodule</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>entitymodule</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.multimodule-maven-project</groupId>
|
||||
<artifactId>multimodule-maven-project</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,19 +0,0 @@
|
|||
package com.baeldung.entity;
|
||||
|
||||
public class User {
|
||||
|
||||
private final String name;
|
||||
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "name=" + name + '}';
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
module com.baeldung.entity {
|
||||
exports com.baeldung.entity;
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.mainappmodule</groupId>
|
||||
<artifactId>mainappmodule</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>mainappmodule</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.multimodule-maven-project</groupId>
|
||||
<artifactId>multimodule-maven-project</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.entitymodule</groupId>
|
||||
<artifactId>entitymodule</artifactId>
|
||||
<version>${entitymodule.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.daomodule</groupId>
|
||||
<artifactId>daomodule</artifactId>
|
||||
<version>${daomodule.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.userdaomodule</groupId>
|
||||
<artifactId>userdaomodule</artifactId>
|
||||
<version>${userdaomodule.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<entitymodule.version>1.0</entitymodule.version>
|
||||
<daomodule.version>1.0</daomodule.version>
|
||||
<userdaomodule.version>1.0</userdaomodule.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,19 +0,0 @@
|
|||
package com.baeldung.mainapp;
|
||||
|
||||
import com.baeldung.dao.Dao;
|
||||
import com.baeldung.entity.User;
|
||||
import com.baeldung.userdao.UserDao;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Map<Integer, User> users = new HashMap<>();
|
||||
users.put(1, new User("Julie"));
|
||||
users.put(2, new User("David"));
|
||||
Dao userDao = new UserDao(users);
|
||||
userDao.findAll().forEach(System.out::println);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
module com.baeldung.mainapp {
|
||||
requires com.baeldung.entity;
|
||||
requires com.baeldung.userdao;
|
||||
requires com.baeldung.dao;
|
||||
uses com.baeldung.dao.Dao;
|
||||
}
|
|
@ -14,6 +14,11 @@
|
|||
<relativePath>../..</relativePath>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<!--<module>custom-rule</module>--> <!-- Fixing in JAVA-2819 -->
|
||||
<module>maven-enforcer</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
|
|
@ -2,8 +2,8 @@ package com.baeldung.multipledb.dao.user;
|
|||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.multipledb.model.user.PossessionMultipleDB;
|
||||
import com.baeldung.multipledb.model.user.Possession;
|
||||
|
||||
public interface PossessionRepository extends JpaRepository<PossessionMultipleDB, Long> {
|
||||
public interface PossessionRepository extends JpaRepository<Possession, Long> {
|
||||
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.baeldung.multipledb.dao.user;
|
|||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.multipledb.model.user.UserMultipleDB;
|
||||
import com.baeldung.multipledb.model.user.User;
|
||||
|
||||
public interface UserRepository extends JpaRepository<UserMultipleDB, Integer> {
|
||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
}
|
|
@ -4,7 +4,7 @@ import javax.persistence.*;
|
|||
|
||||
@Entity
|
||||
@Table
|
||||
public class PossessionMultipleDB {
|
||||
public class Possession {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
|
@ -12,11 +12,11 @@ public class PossessionMultipleDB {
|
|||
|
||||
private String name;
|
||||
|
||||
public PossessionMultipleDB() {
|
||||
public Possession() {
|
||||
super();
|
||||
}
|
||||
|
||||
public PossessionMultipleDB(final String name) {
|
||||
public Possession(final String name) {
|
||||
super();
|
||||
|
||||
this.name = name;
|
||||
|
@ -58,7 +58,7 @@ public class PossessionMultipleDB {
|
|||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final PossessionMultipleDB other = (PossessionMultipleDB) obj;
|
||||
final Possession other = (Possession) obj;
|
||||
if (id != other.id) {
|
||||
return false;
|
||||
}
|
|
@ -6,7 +6,7 @@ import java.util.List;
|
|||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class UserMultipleDB {
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
|
@ -18,13 +18,13 @@ public class UserMultipleDB {
|
|||
private Integer status;
|
||||
|
||||
@OneToMany
|
||||
List<PossessionMultipleDB> possessionList;
|
||||
List<Possession> possessionList;
|
||||
|
||||
public UserMultipleDB() {
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserMultipleDB(String name, String email, Integer status) {
|
||||
public User(String name, String email, Integer status) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.status = status;
|
||||
|
@ -70,11 +70,11 @@ public class UserMultipleDB {
|
|||
this.age = age;
|
||||
}
|
||||
|
||||
public List<PossessionMultipleDB> getPossessionList() {
|
||||
public List<Possession> getPossessionList() {
|
||||
return possessionList;
|
||||
}
|
||||
|
||||
public void setPossessionList(List<PossessionMultipleDB> possessionList) {
|
||||
public void setPossessionList(List<Possession> possessionList) {
|
||||
this.possessionList = possessionList;
|
||||
}
|
||||
|
|
@ -20,8 +20,8 @@ import com.baeldung.multipledb.dao.product.ProductRepository;
|
|||
import com.baeldung.multipledb.dao.user.PossessionRepository;
|
||||
import com.baeldung.multipledb.dao.user.UserRepository;
|
||||
import com.baeldung.multipledb.model.product.Product;
|
||||
import com.baeldung.multipledb.model.user.PossessionMultipleDB;
|
||||
import com.baeldung.multipledb.model.user.UserMultipleDB;
|
||||
import com.baeldung.multipledb.model.user.Possession;
|
||||
import com.baeldung.multipledb.model.user.User;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes=MultipleDbApplication.class)
|
||||
|
@ -42,15 +42,15 @@ public class JpaMultipleDBIntegrationTest {
|
|||
@Test
|
||||
@Transactional("userTransactionManager")
|
||||
public void whenCreatingUser_thenCreated() {
|
||||
UserMultipleDB user = new UserMultipleDB();
|
||||
User user = new User();
|
||||
user.setName("John");
|
||||
user.setEmail("john@test.com");
|
||||
user.setAge(20);
|
||||
PossessionMultipleDB p = new PossessionMultipleDB("sample");
|
||||
Possession p = new Possession("sample");
|
||||
p = possessionRepository.save(p);
|
||||
user.setPossessionList(Collections.singletonList(p));
|
||||
user = userRepository.save(user);
|
||||
final Optional<UserMultipleDB> result = userRepository.findById(user.getId());
|
||||
final Optional<User> result = userRepository.findById(user.getId());
|
||||
assertTrue(result.isPresent());
|
||||
System.out.println(result.get().getPossessionList());
|
||||
assertEquals(1, result.get().getPossessionList().size());
|
||||
|
@ -59,14 +59,14 @@ public class JpaMultipleDBIntegrationTest {
|
|||
@Test
|
||||
@Transactional("userTransactionManager")
|
||||
public void whenCreatingUsersWithSameEmail_thenRollback() {
|
||||
UserMultipleDB user1 = new UserMultipleDB();
|
||||
User user1 = new User();
|
||||
user1.setName("John");
|
||||
user1.setEmail("john@test.com");
|
||||
user1.setAge(20);
|
||||
user1 = userRepository.save(user1);
|
||||
assertTrue(userRepository.findById(user1.getId()).isPresent());
|
||||
|
||||
UserMultipleDB user2 = new UserMultipleDB();
|
||||
User user2 = new User();
|
||||
user2.setName("Tom");
|
||||
user2.setEmail("john@test.com");
|
||||
user2.setAge(10);
|
||||
|
|
4
pom.xml
4
pom.xml
|
@ -508,7 +508,6 @@
|
|||
|
||||
<module>maven-modules</module>
|
||||
<module>maven-archetype</module>
|
||||
<!-- <module>maven-java-11</module> --> <!-- we haven't upgraded to Java 11 -->
|
||||
<module>maven-polyglot</module>
|
||||
|
||||
<module>mesos-marathon</module>
|
||||
|
@ -697,7 +696,6 @@
|
|||
<module>spring-remoting</module>
|
||||
<module>spring-rest-angular</module>
|
||||
<module>spring-rest-compress</module>
|
||||
<module>spring-rest-hal-browser</module>
|
||||
<module>spring-rest-http</module>
|
||||
<module>spring-rest-http-2</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
|
@ -1019,7 +1017,6 @@
|
|||
|
||||
<module>maven-modules</module>
|
||||
<module>maven-archetype</module>
|
||||
<!-- <module>maven-java-11</module> --> <!-- we haven't upgraded to Java 11 -->
|
||||
<module>maven-polyglot</module>
|
||||
|
||||
<module>mesos-marathon</module>
|
||||
|
@ -1200,7 +1197,6 @@
|
|||
<module>spring-remoting</module>
|
||||
<module>spring-rest-angular</module>
|
||||
<module>spring-rest-compress</module>
|
||||
<module>spring-rest-hal-browser</module>
|
||||
<module>spring-rest-http</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
<module>spring-rest-shell</module>
|
||||
|
|
|
@ -10,6 +10,7 @@ import org.springframework.core.annotation.Order;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
import springfox.bean.validators.plugins.Validators;
|
||||
import springfox.documentation.builders.StringElementFacetBuilder;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
|
||||
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
|
||||
|
@ -30,8 +31,9 @@ public class EmailAnnotationPlugin implements ModelPropertyBuilderPlugin {
|
|||
public void apply(ModelPropertyContext context) {
|
||||
Optional<Email> email = annotationFromBean(context, Email.class);
|
||||
if (email.isPresent()) {
|
||||
context.getBuilder().pattern(email.get().regexp());
|
||||
context.getBuilder().example("email@email.com");
|
||||
context.getSpecificationBuilder().facetBuilder(StringElementFacetBuilder.class)
|
||||
.pattern(email.get().regexp());
|
||||
context.getSpecificationBuilder().example("email@email.com");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -43,6 +43,7 @@
|
|||
<module>spring-cloud-ribbon-retry</module>
|
||||
<module>spring-cloud-circuit-breaker</module>
|
||||
<module>spring-cloud-eureka-self-preservation</module>
|
||||
<!--<module>spring-cloud-openfeign</module>--> <!-- Fixing in JAVA-2820 -->
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -12,6 +12,7 @@ This module contains articles about Spring Data REST
|
|||
- [Customizing HTTP Endpoints in Spring Data REST](https://www.baeldung.com/spring-data-rest-customize-http-endpoints)
|
||||
- [Spring Boot with SQLite](https://www.baeldung.com/spring-boot-sqlite)
|
||||
- [Spring Data Web Support](https://www.baeldung.com/spring-data-web-support)
|
||||
- [Spring REST and HAL Browser](https://www.baeldung.com/spring-rest-hal)
|
||||
|
||||
### The Course
|
||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
|
|
@ -32,6 +32,15 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-rest-hal-browser -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-rest-hal-browser</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung;
|
||||
package com.baeldung.halbrowser;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
@ -1,14 +1,15 @@
|
|||
package com.baeldung.config;
|
||||
package com.baeldung.halbrowser.config;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import com.baeldung.data.BookRepository;
|
||||
import com.baeldung.model.Book;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.stream.IntStream;
|
||||
import com.baeldung.halbrowser.data.BookRepository;
|
||||
import com.baeldung.halbrowser.model.Book;
|
||||
|
||||
@Component
|
||||
public class DBLoader implements ApplicationRunner {
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.config;
|
||||
package com.baeldung.halbrowser.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
|
@ -1,14 +1,14 @@
|
|||
package com.baeldung.data;
|
||||
package com.baeldung.halbrowser.data;
|
||||
|
||||
import com.baeldung.model.Book;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.data.rest.core.annotation.RestResource;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.halbrowser.model.Book;
|
||||
|
||||
@Repository
|
||||
public interface BookRepository extends PagingAndSortingRepository<Book, Long> {
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.model;
|
||||
package com.baeldung.halbrowser.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
|
@ -69,7 +69,7 @@
|
|||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.thymeleaf</groupId>
|
||||
<artifactId>thymeleaf-spring4</artifactId>
|
||||
<artifactId>thymeleaf-spring5</artifactId>
|
||||
<version>${org.thymeleaf-version}</version>
|
||||
</dependency>
|
||||
|
||||
|
@ -164,7 +164,7 @@
|
|||
<hibernate-validator.version>6.0.10.Final</hibernate-validator.version>
|
||||
<deploy-path>enter-location-of-server</deploy-path>
|
||||
<commons-fileupload.version>1.3.2</commons-fileupload.version>
|
||||
<org.thymeleaf-version>3.0.7.RELEASE</org.thymeleaf-version>
|
||||
<org.thymeleaf-version>3.0.11.RELEASE</org.thymeleaf-version>
|
||||
<groovy.version>2.4.12</groovy.version>
|
||||
<freemarker.version>2.3.27-incubating</freemarker.version>
|
||||
<jade.version>1.2.5</jade.version>
|
||||
|
|
|
@ -11,8 +11,8 @@ import org.springframework.mail.javamail.JavaMailSender;
|
|||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
|
||||
import org.thymeleaf.spring5.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = { "com.baeldung.spring.mail" })
|
||||
|
|
|
@ -4,9 +4,9 @@ import org.springframework.context.annotation.Bean;
|
|||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
|
||||
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||
import org.thymeleaf.spring5.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
|
||||
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
|
|
|
@ -19,7 +19,7 @@ import org.springframework.stereotype.Service;
|
|||
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
import org.thymeleaf.context.Context;
|
||||
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring5.SpringTemplateEngine;
|
||||
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
## Spring REST HAL Browser
|
||||
|
||||
This module contains articles about Spring REST with the HAL browser
|
||||
|
||||
### Relevant Articles:
|
||||
- [Spring REST and HAL Browser](https://www.baeldung.com/spring-rest-hal)
|
|
@ -1,62 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-rest-hal-browser</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>spring-rest-hal-browser</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-rest-hal-browser -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-rest-hal-browser</artifactId>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${source.version}</source>
|
||||
<target>${target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<source.version>1.8</source.version>
|
||||
<target.version>1.8</target.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,13 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -3,6 +3,7 @@ package com.baeldung.springretry;
|
|||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.retry.annotation.EnableRetry;
|
||||
import org.springframework.retry.backoff.FixedBackOffPolicy;
|
||||
import org.springframework.retry.policy.SimpleRetryPolicy;
|
||||
|
@ -11,9 +12,7 @@ import org.springframework.retry.support.RetryTemplate;
|
|||
@Configuration
|
||||
@ComponentScan(basePackages = "com.baeldung.springretry")
|
||||
@EnableRetry
|
||||
// Uncomment this two lines if we need XML configuration
|
||||
// @EnableAspectJAutoProxy
|
||||
// @ImportResource("classpath:/retryadvice.xml")
|
||||
@PropertySource("classpath:retryConfig.properties")
|
||||
public class AppConfig {
|
||||
|
||||
@Bean
|
||||
|
|
|
@ -2,18 +2,27 @@ package com.baeldung.springretry;
|
|||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.retry.annotation.Backoff;
|
||||
import org.springframework.retry.annotation.Recover;
|
||||
import org.springframework.retry.annotation.Retryable;
|
||||
|
||||
|
||||
public interface MyService {
|
||||
|
||||
@Retryable
|
||||
void retryService();
|
||||
|
||||
@Retryable(value = { SQLException.class }, maxAttempts = 2, backoff = @Backoff(delay = 5000))
|
||||
@Retryable(value = SQLException.class)
|
||||
void retryServiceWithRecovery(String sql) throws SQLException;
|
||||
|
||||
@Retryable(value = { SQLException.class }, maxAttempts = 2, backoff = @Backoff(delay = 100))
|
||||
void retryServiceWithCustomization(String sql) throws SQLException;
|
||||
|
||||
@Retryable( value = SQLException.class, maxAttemptsExpression = "${retry.maxAttempts}",
|
||||
backoff = @Backoff(delayExpression = "${retry.maxDelay}"))
|
||||
void retryServiceWithExternalConfiguration(String sql) throws SQLException;
|
||||
|
||||
@Recover
|
||||
void recover(SQLException e, String sql);
|
||||
|
||||
|
|
|
@ -26,6 +26,22 @@ public class MyServiceImpl implements MyService {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void retryServiceWithCustomization(String sql) throws SQLException {
|
||||
if (StringUtils.isEmpty(sql)) {
|
||||
logger.info("throw SQLException in method retryServiceWithCustomization()");
|
||||
throw new SQLException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void retryServiceWithExternalConfiguration(String sql) throws SQLException {
|
||||
if (StringUtils.isEmpty(sql)) {
|
||||
logger.info("throw SQLException in method retryServiceWithExternalConfiguration()");
|
||||
throw new SQLException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recover(SQLException e, String sql) {
|
||||
logger.info("In recover method");
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
retry.maxAttempts=2
|
||||
retry.maxDelay=100
|
|
@ -1,50 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/aop
|
||||
http://www.springframework.org/schema/aop/spring-aop.xsd
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
<aop:config>
|
||||
<aop:pointcut id="transactional"
|
||||
expression="execution(* com.baeldung.springretry..*MyService.defaultXmlRetryService(..))" />
|
||||
<aop:advisor pointcut-ref="transactional" advice-ref="taskRetryAdvice" order="-1" />
|
||||
</aop:config>
|
||||
|
||||
<bean id="taskRetryAdvice" class="org.springframework.retry.interceptor.RetryOperationsInterceptor">
|
||||
<property name="RetryOperations" ref="taskBatchRetryTemplate" />
|
||||
</bean>
|
||||
|
||||
<bean id="taskBatchRetryTemplate" class="org.springframework.retry.support.RetryTemplate">
|
||||
<property name="retryPolicy" ref="taskBatchRetryPolicy" />
|
||||
<property name="backOffPolicy" ref="ExponentialBackOffPolicy" />
|
||||
</bean>
|
||||
|
||||
<bean id="taskBatchRetryPolicy" class="org.springframework.retry.policy.SimpleRetryPolicy">
|
||||
<constructor-arg index="0" value="2" />
|
||||
<constructor-arg index="1">
|
||||
<map>
|
||||
<entry key="java.lang.RuntimeException" value="true" />
|
||||
</map>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="ExponentialBackOffPolicy" class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
|
||||
<property name="initialInterval" value="300">
|
||||
<description>
|
||||
Initial sleep interval value, default 300 ms
|
||||
</description>
|
||||
</property>
|
||||
<property name="maxInterval" value="30000">
|
||||
<description>
|
||||
The maximum value of the backoff period in milliseconds.
|
||||
</description>
|
||||
</property>
|
||||
<property name="multiplier" value="2.0">
|
||||
<description>
|
||||
The value to increment the exp seed with for each retry attempt.
|
||||
</description>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
|
@ -30,6 +30,16 @@ public class SpringRetryIntegrationTest {
|
|||
myService.retryServiceWithRecovery(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRetryServiceWithCustomization_whenCallWithException_thenRetryRecover() throws SQLException {
|
||||
myService.retryServiceWithCustomization(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRetryServiceWithExternalConfiguration_whenCallWithException_thenRetryRecover() throws SQLException {
|
||||
myService.retryServiceWithExternalConfiguration(null);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void givenTemplateRetryService_whenCallWithException_thenRetry() {
|
||||
retryTemplate.execute(arg0 -> {
|
||||
|
|
|
@ -24,7 +24,6 @@
|
|||
<module>spring-security-web-boot-2</module>
|
||||
<module>spring-security-web-mvc-custom</module>
|
||||
<module>spring-security-web-digest-auth</module>
|
||||
<module>spring-security-web-jsonview</module>
|
||||
<module>spring-security-ldap</module>
|
||||
<module>spring-security-web-login</module>
|
||||
<module>spring-security-web-persisted-remember-me</module>
|
||||
|
|
|
@ -9,6 +9,7 @@ This module contains articles about core Spring Security
|
|||
- [Overview and Need for DelegatingFilterProxy in Spring](https://www.baeldung.com/spring-delegating-filter-proxy)
|
||||
- [Deny Access on Missing @PreAuthorize to Spring Controller Methods](https://www.baeldung.com/spring-deny-access)
|
||||
- [Spring Security: Check If a User Has a Role in Java](https://www.baeldung.com/spring-security-check-user-role)
|
||||
- [Filtering Jackson JSON Output Based on Spring Security Role](https://www.baeldung.com/spring-security-role-filter-json)
|
||||
|
||||
### Build the Project
|
||||
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.filterresponse;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.spring;
|
||||
package com.baeldung.filterresponse.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
@ -12,12 +12,10 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@EnableWebSecurity
|
||||
@ComponentScan("com.baeldung")
|
||||
@ComponentScan("com.baeldung.filterresponse")
|
||||
public class AppConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
|
@ -1,6 +1,9 @@
|
|||
package com.baeldung.spring;
|
||||
package com.baeldung.filterresponse.config;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.baeldung.controller.View;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.json.MappingJacksonValue;
|
||||
|
@ -11,9 +14,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
|||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.AbstractMappingJacksonResponseBodyAdvice;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import com.baeldung.filterresponse.controller.View;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class SecurityJsonViewControllerAdvice extends AbstractMappingJacksonResponseBodyAdvice {
|
|
@ -1,14 +1,12 @@
|
|||
package com.baeldung.controller;
|
||||
package com.baeldung.filterresponse.controller;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import com.baeldung.model.Item;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import com.baeldung.filterresponse.model.Item;
|
||||
|
||||
@RestController
|
||||
public class ItemsController {
|
|
@ -1,10 +1,11 @@
|
|||
package com.baeldung.controller;
|
||||
package com.baeldung.filterresponse.controller;
|
||||
|
||||
import com.baeldung.spring.AppConfig.Role;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.baeldung.filterresponse.config.AppConfig.Role;
|
||||
|
||||
public class View {
|
||||
|
||||
public static final Map<Role, Class> MAPPING = new HashMap<>();
|
|
@ -1,6 +1,6 @@
|
|||
package com.baeldung.model;
|
||||
package com.baeldung.filterresponse.model;
|
||||
|
||||
import com.baeldung.controller.View;
|
||||
import com.baeldung.filterresponse.controller.View;
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
|
||||
public class Item {
|
|
@ -1,6 +1,6 @@
|
|||
package com.baeldung.security;
|
||||
package com.baeldung.filterresponse;
|
||||
|
||||
import com.baeldung.spring.AppConfig;
|
||||
import com.baeldung.filterresponse.config.AppConfig;
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.junit.Before;
|
|
@ -1,13 +0,0 @@
|
|||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
|
@ -1,7 +0,0 @@
|
|||
## Spring Security Web Json View
|
||||
|
||||
This module contains articles about Spring Security with JSON
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Filtering Jackson JSON Output Based on Spring Security Role](https://www.baeldung.com/spring-security-role-filter-json)
|
|
@ -1,206 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-security-web-jsonview</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>spring-security-web-jsonview</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Security -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-web</artifactId>
|
||||
<version>${spring-security.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-config</artifactId>
|
||||
<version>${spring-security.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-taglibs</artifactId>
|
||||
<version>${spring-security.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aop</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-expression</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- web -->
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet-api.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>${jstl.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-test</artifactId>
|
||||
<version>${spring-security.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.jsonpath</groupId>
|
||||
<artifactId>json-path</artifactId>
|
||||
<version>${json-path.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-security-mvc-jsonview</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>default-war</id>
|
||||
<phase>prepare-package</phase>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.codehaus.cargo</groupId>
|
||||
<artifactId>cargo-maven2-plugin</artifactId>
|
||||
<version>${cargo-maven2-plugin.version}</version>
|
||||
<configuration>
|
||||
<deployables>
|
||||
<deployable>
|
||||
<location>${project.build.directory}/${project.name}.war</location>
|
||||
<type>war</type>
|
||||
<properties>
|
||||
<context>/</context>
|
||||
</properties>
|
||||
</deployable>
|
||||
</deployables>
|
||||
<container>
|
||||
<timeout>2400000</timeout>
|
||||
<containerId>tomcat8x</containerId>
|
||||
<type>embedded</type>
|
||||
<systemProperties>
|
||||
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
|
||||
</systemProperties>
|
||||
</container>
|
||||
<configuration>
|
||||
<properties>
|
||||
<cargo.servlet.port>8084</cargo.servlet.port>
|
||||
</properties>
|
||||
</configuration>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
<json-path.version>2.4.0</json-path.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,33 +0,0 @@
|
|||
package com.baeldung;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
import org.springframework.web.filter.DelegatingFilterProxy;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
public class AppInitializer implements WebApplicationInitializer {
|
||||
|
||||
@Override
|
||||
public void onStartup(final ServletContext sc) throws ServletException {
|
||||
|
||||
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
||||
|
||||
root.scan("com.baeldung");
|
||||
sc.addListener(new ContextLoaderListener(root));
|
||||
|
||||
ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
|
||||
appServlet.setLoadOnStartup(1);
|
||||
appServlet.addMapping("/");
|
||||
|
||||
sc.addFilter("securityFilter", new DelegatingFilterProxy("springSecurityFilterChain"))
|
||||
.addMappingForUrlPatterns(null, false, "/*");
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -1,17 +0,0 @@
|
|||
package com.baeldung;
|
||||
|
||||
import com.baeldung.spring.AppConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = AppConfig.class)
|
||||
@WebAppConfiguration
|
||||
public class SpringContextTest {
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
|
@ -145,10 +145,10 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<spring-data.version>2.0.9.RELEASE</spring-data.version>
|
||||
<org.thymeleaf-version>3.0.9.RELEASE</org.thymeleaf-version>
|
||||
<org.thymeleaf.extras-version>3.0.1.RELEASE</org.thymeleaf.extras-version>
|
||||
<thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
|
||||
<spring-data.version>2.3.2.RELEASE</spring-data.version>
|
||||
<org.thymeleaf-version>3.0.11.RELEASE</org.thymeleaf-version>
|
||||
<org.thymeleaf.extras-version>3.0.4.RELEASE</org.thymeleaf.extras-version>
|
||||
<thymeleaf-layout-dialect.version>2.4.1</thymeleaf-layout-dialect.version>
|
||||
<javax.validation-version>2.0.1.Final</javax.validation-version>
|
||||
<hibernate-validator.version>6.0.11.Final</hibernate-validator.version>
|
||||
|
||||
|
|
Loading…
Reference in New Issue