Merge branch 'master' of github.com:eugenp/tutorials into master
This commit is contained in:
commit
e9103c10a6
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
|
@ -9,4 +9,5 @@ This module contains articles about core Java input and output (IO)
|
|||
- [Check If a File or Directory Exists in Java](https://www.baeldung.com/java-file-directory-exists)
|
||||
- [Copy a Directory in Java](https://www.baeldung.com/java-copy-directory)
|
||||
- [Java Files Open Options](https://www.baeldung.com/java-file-options)
|
||||
- [Creating Temporary Directories in Java](https://www.baeldung.com/java-temp-directories)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-io-2)
|
||||
|
|
|
@ -4,6 +4,7 @@ import org.junit.Test;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
@ -20,8 +21,8 @@ public class DirectoryEmptinessUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenNotDirectory_thenReturnsFalse() throws IOException {
|
||||
Path aFile = Paths.get(getClass().getResource("/notDir.txt").getPath());
|
||||
public void givenPath_whenNotDirectory_thenReturnsFalse() throws IOException, URISyntaxException {
|
||||
Path aFile = Paths.get(getClass().getResource("/notDir.txt").toURI());
|
||||
assertThat(isEmpty(aFile)).isFalse();
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import org.apache.commons.io.FileUtils;
|
|||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
@ -66,10 +67,16 @@ public class TemporaryDirectoriesUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenTempDirWithPrefixWithFileAttrs_whenCreatePlainJava_thenAttributesAreSet() throws IOException {
|
||||
final FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("r--------"));
|
||||
boolean isPosix = FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
|
||||
|
||||
final Path tmpdir = Files.createTempDirectory(Paths.get("target"), "tmpDirPrefix", attrs);
|
||||
assertThat(tmpdir.toFile().getPath()).startsWith("target");
|
||||
assertThat(tmpdir.toFile().canWrite()).isFalse();
|
||||
if(!isPosix){
|
||||
System.out.println("You must be under a Posix Compliant Filesystem to run this test.");
|
||||
} else {
|
||||
final FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("r--------"));
|
||||
|
||||
final Path tmpdir = Files.createTempDirectory(Paths.get("target"), "tmpDirPrefix", attrs);
|
||||
assertThat(tmpdir.toFile().getPath()).startsWith("target");
|
||||
assertThat(tmpdir.toFile().canWrite()).isFalse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
<?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">
|
||||
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>hazelcast</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
@ -15,7 +15,6 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- Hazelcast Jet -->
|
||||
<dependency>
|
||||
<groupId>com.hazelcast.jet</groupId>
|
||||
<artifactId>hazelcast-jet</artifactId>
|
||||
|
@ -34,8 +33,7 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- hazelcast jet -->
|
||||
<hazelcast.jet.version>0.6</hazelcast.jet.version>
|
||||
<hazelcast.jet.version>4.2</hazelcast.jet.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,24 +1,20 @@
|
|||
package com.baeldung.hazelcast.cluster;
|
||||
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import com.hazelcast.client.HazelcastClient;
|
||||
import com.hazelcast.client.config.ClientConfig;
|
||||
import com.hazelcast.config.GroupConfig;
|
||||
import com.hazelcast.core.HazelcastInstance;
|
||||
import com.hazelcast.core.IMap;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class NativeClient {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
public static void main(String[] args) {
|
||||
ClientConfig config = new ClientConfig();
|
||||
GroupConfig groupConfig = config.getGroupConfig();
|
||||
groupConfig.setName("dev");
|
||||
groupConfig.setPassword("dev-pass");
|
||||
config.setClusterName("dev");
|
||||
HazelcastInstance hazelcastInstanceClient = HazelcastClient.newHazelcastClient(config);
|
||||
IMap<Long, String> map = hazelcastInstanceClient.getMap("data");
|
||||
for (Entry<Long, String> entry : map.entrySet()) {
|
||||
System.out.println(String.format("Key: %d, Value: %s", entry.getKey(), entry.getValue()));
|
||||
Map<Long, String> map = hazelcastInstanceClient.getMap("data");
|
||||
for (Map.Entry<Long, String> entry : map.entrySet()) {
|
||||
System.out.printf("Key: %d, Value: %s%n", entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
package com.baeldung.hazelcast.cluster;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.hazelcast.core.Hazelcast;
|
||||
import com.hazelcast.core.HazelcastInstance;
|
||||
import com.hazelcast.core.IdGenerator;
|
||||
import com.hazelcast.flakeidgen.FlakeIdGenerator;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class ServerNode {
|
||||
|
||||
public static void main(String[] args) {
|
||||
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
|
||||
Map<Long, String> map = hazelcastInstance.getMap("data");
|
||||
IdGenerator idGenerator = hazelcastInstance.getIdGenerator("newid");
|
||||
FlakeIdGenerator idGenerator = hazelcastInstance.getFlakeIdGenerator("newid");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
map.put(idGenerator.newId(), "message" + 1);
|
||||
map.put(idGenerator.newId(), "message" + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,33 +1,31 @@
|
|||
package com.baeldung.hazelcast.jet;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.hazelcast.jet.Traversers.traverseArray;
|
||||
import static com.hazelcast.jet.aggregate.AggregateOperations.counting;
|
||||
import static com.hazelcast.jet.function.DistributedFunctions.wholeItem;
|
||||
|
||||
import com.hazelcast.jet.Jet;
|
||||
import com.hazelcast.jet.JetInstance;
|
||||
import com.hazelcast.jet.pipeline.Pipeline;
|
||||
import com.hazelcast.jet.pipeline.Sinks;
|
||||
import com.hazelcast.jet.pipeline.Sources;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.hazelcast.function.Functions.wholeItem;
|
||||
import static com.hazelcast.jet.Traversers.traverseArray;
|
||||
import static com.hazelcast.jet.aggregate.AggregateOperations.counting;
|
||||
|
||||
public class WordCounter {
|
||||
|
||||
private static final String LIST_NAME = "textList";
|
||||
|
||||
private static final String MAP_NAME = "countMap";
|
||||
|
||||
private Pipeline createPipeLine() {
|
||||
Pipeline p = Pipeline.create();
|
||||
p.drawFrom(Sources.<String> list(LIST_NAME))
|
||||
.flatMap(word -> traverseArray(word.toLowerCase()
|
||||
.split("\\W+")))
|
||||
p.readFrom(Sources.<String>list(LIST_NAME))
|
||||
.flatMap(word -> traverseArray(word.toLowerCase().split("\\W+")))
|
||||
.filter(word -> !word.isEmpty())
|
||||
.groupingKey(wholeItem())
|
||||
.aggregate(counting())
|
||||
.drainTo(Sinks.map(MAP_NAME));
|
||||
.writeTo(Sinks.map(MAP_NAME));
|
||||
return p;
|
||||
}
|
||||
|
||||
|
@ -38,8 +36,7 @@ public class WordCounter {
|
|||
List<String> textList = jet.getList(LIST_NAME);
|
||||
textList.addAll(sentences);
|
||||
Pipeline p = createPipeLine();
|
||||
jet.newJob(p)
|
||||
.join();
|
||||
jet.newJob(p).join();
|
||||
Map<String, Long> counts = jet.getMap(MAP_NAME);
|
||||
count = counts.get(word);
|
||||
} finally {
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package com.baeldung.hazelcast.jet;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class WordCounterUnitTest {
|
||||
|
||||
|
@ -15,7 +15,7 @@ public class WordCounterUnitTest {
|
|||
sentences.add("The first second was alright, but the second second was tough.");
|
||||
WordCounter wordCounter = new WordCounter();
|
||||
long countSecond = wordCounter.countWord(sentences, "second");
|
||||
assertTrue(countSecond == 3);
|
||||
assertEquals(3, countSecond);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
|
@ -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() {
|
||||
}
|
||||
}
|
|
@ -12,7 +12,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.web")
|
||||
@ComponentScan("com.baeldung")
|
||||
@EnableWebMvc
|
||||
@EnableAsync
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
|
|
@ -20,9 +20,9 @@
|
|||
<param-value>com.baeldung.spring</param-value>
|
||||
</context-param>
|
||||
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
<!-- <listener>-->
|
||||
<!-- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->
|
||||
<!-- </listener>-->
|
||||
|
||||
<!-- Spring child -->
|
||||
<servlet>
|
||||
|
@ -40,17 +40,17 @@
|
|||
</servlet-mapping>
|
||||
|
||||
<!-- Spring Security -->
|
||||
<filter>
|
||||
<filter-name>springSecurityFilterChain</filter-name>
|
||||
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
|
||||
<async-supported>true</async-supported>
|
||||
</filter>
|
||||
<filter-mapping>
|
||||
<filter-name>springSecurityFilterChain</filter-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
<dispatcher>REQUEST</dispatcher>
|
||||
<dispatcher>ASYNC</dispatcher>
|
||||
</filter-mapping>
|
||||
<!-- <filter>-->
|
||||
<!-- <filter-name>springSecurityFilterChain</filter-name>-->
|
||||
<!-- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>-->
|
||||
<!-- <async-supported>true</async-supported>-->
|
||||
<!-- </filter>-->
|
||||
<!-- <filter-mapping>-->
|
||||
<!-- <filter-name>springSecurityFilterChain</filter-name>-->
|
||||
<!-- <url-pattern>/*</url-pattern>-->
|
||||
<!-- <dispatcher>REQUEST</dispatcher>-->
|
||||
<!-- <dispatcher>ASYNC</dispatcher>-->
|
||||
<!-- </filter-mapping>-->
|
||||
|
||||
<!-- <welcome-file-list> -->
|
||||
<!-- <welcome-file>index.html</welcome-file> -->
|
||||
|
|
Loading…
Reference in New Issue