Merge branch 'master' into sla-pr/1423-jesus-custom-validator
This commit is contained in:
commit
f8bd663f7d
@ -5,7 +5,6 @@ install: travis_wait 40 mvn -q clean install -Dgib.enabled=true
|
|||||||
jdk:
|
jdk:
|
||||||
- oraclejdk8
|
- oraclejdk8
|
||||||
|
|
||||||
sudo: false
|
|
||||||
addons:
|
addons:
|
||||||
apt:
|
apt:
|
||||||
packages:
|
packages:
|
||||||
|
1
core-java-9/compile-modules.sh
Normal file
1
core-java-9/compile-modules.sh
Normal file
@ -0,0 +1 @@
|
|||||||
|
javac -d mods --module-source-path src/modules $(find src/modules -name "*.java")
|
3
core-java-9/compile-student-client.bat
Normal file
3
core-java-9/compile-student-client.bat
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
javac --module-path mods -d mods/com.baeldung.student.client^
|
||||||
|
src/modules/com.baeldung.student.client/module-info.java^
|
||||||
|
src/modules/com.baeldung.student.client/com/baeldung/student/client/StudentClient.java
|
2
core-java-9/compile-student-model.bat
Normal file
2
core-java-9/compile-student-model.bat
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
javac -d mods/com.baeldung.student.model src/modules/com.baeldung.student.model/module-info.java^
|
||||||
|
src/modules/com.baeldung.student.model/com/baeldung/student/model/Student.java
|
3
core-java-9/compile-student-service-dbimpl.bat
Normal file
3
core-java-9/compile-student-service-dbimpl.bat
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
javac --module-path mods -d mods/com.baeldung.student.service.dbimpl^
|
||||||
|
src/modules/com.baeldung.student.service.dbimpl/module-info.java^
|
||||||
|
src/modules/com.baeldung.student.service.dbimpl/com/baeldung/student/service/dbimpl/StudentDbService.java
|
3
core-java-9/compile-student-service.bat
Normal file
3
core-java-9/compile-student-service.bat
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
javac --module-path mods -d mods/com.baeldung.student.service^
|
||||||
|
src/modules/com.baeldung.student.service/module-info.java^
|
||||||
|
src/modules/com.baeldung.student.service/com/baeldung/student/service/StudentService.java
|
1
core-java-9/run-student-client.bat
Normal file
1
core-java-9/run-student-client.bat
Normal file
@ -0,0 +1 @@
|
|||||||
|
java --module-path mods -m com.baeldung.student.client/com.baeldung.student.client.StudentClient
|
1
core-java-9/run-student-client.sh
Normal file
1
core-java-9/run-student-client.sh
Normal file
@ -0,0 +1 @@
|
|||||||
|
java --module-path mods -m com.baeldung.student.client/com.baeldung.student.client.StudentClient
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.student.client;
|
||||||
|
|
||||||
|
import com.baeldung.student.service.StudentService;
|
||||||
|
import com.baeldung.student.service.dbimpl.StudentDbService;
|
||||||
|
import com.baeldung.student.model.Student;
|
||||||
|
|
||||||
|
public class StudentClient {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
StudentService service = new StudentDbService();
|
||||||
|
service.create(new Student());
|
||||||
|
service.read("17SS0001");
|
||||||
|
service.update(new Student());
|
||||||
|
service.delete("17SS0001");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
module com.baeldung.student.client{
|
||||||
|
requires com.baeldung.student.service.dbimpl;
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.student.model;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class Student {
|
||||||
|
private String registrationId;
|
||||||
|
|
||||||
|
public String getRegistrationId() {
|
||||||
|
return registrationId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRegistrationId(String registrationId) {
|
||||||
|
this.registrationId = registrationId;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
module com.baeldung.student.model{
|
||||||
|
exports com.baeldung.student.model;
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.student.service.dbimpl;
|
||||||
|
|
||||||
|
import com.baeldung.student.service.StudentService;
|
||||||
|
import com.baeldung.student.model.Student;
|
||||||
|
import java.util.logging.*;
|
||||||
|
|
||||||
|
public class StudentDbService implements StudentService {
|
||||||
|
|
||||||
|
private static Logger logger = Logger.getLogger("StudentDbService");
|
||||||
|
|
||||||
|
public String create(Student student) {
|
||||||
|
logger.log(Level.INFO, "Creating student in DB...");
|
||||||
|
return student.getRegistrationId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Student read(String registrationId) {
|
||||||
|
logger.log(Level.INFO, "Reading student from DB...");
|
||||||
|
return new Student();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Student update(Student student) {
|
||||||
|
logger.log(Level.INFO, "Updating sutdent in DB...");
|
||||||
|
return student;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String delete(String registrationId) {
|
||||||
|
logger.log(Level.INFO, "Deleteing sutdent in DB...");
|
||||||
|
return registrationId;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
module com.baeldung.student.service.dbimpl{
|
||||||
|
requires transitive com.baeldung.student.service;
|
||||||
|
exports com.baeldung.student.service.dbimpl;
|
||||||
|
requires java.logging;
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.student.service;
|
||||||
|
|
||||||
|
import com.baeldung.student.model.Student;
|
||||||
|
|
||||||
|
public interface StudentService {
|
||||||
|
|
||||||
|
public String create(Student student);
|
||||||
|
|
||||||
|
public Student read(String registrationId);
|
||||||
|
|
||||||
|
public Student update(Student student);
|
||||||
|
|
||||||
|
public String delete(String registrationId);
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
module com.baeldung.student.service{
|
||||||
|
requires transitive com.baeldung.student.model;
|
||||||
|
exports com.baeldung.student.service;
|
||||||
|
}
|
@ -1,17 +0,0 @@
|
|||||||
package com.baeldung.list.flattennestedlist;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class FlattenNestedList {
|
|
||||||
|
|
||||||
public List<String> flattenListOfLists(List<List<String>> lol) {
|
|
||||||
|
|
||||||
// flatten the list
|
|
||||||
List<String> ls = new ArrayList<>();
|
|
||||||
lol.forEach((k) -> ls.addAll(k));
|
|
||||||
|
|
||||||
return ls;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,52 +1,64 @@
|
|||||||
package com.baeldung.list.flattennestedlist;
|
package com.baeldung.list.flattennestedlist;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
public class FlattenNestedListTest {
|
public class FlattenNestedListTest {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(FlattenNestedListTest.class);
|
|
||||||
private FlattenNestedList flol;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setup() {
|
|
||||||
flol = new FlattenNestedList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenListOfListOfString_flattenNestedList() {
|
public void givenListOfListOfString_flattenNestedList1() {
|
||||||
|
// given
|
||||||
// create the list to flatten
|
|
||||||
List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three");
|
List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three");
|
||||||
List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three");
|
List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three");
|
||||||
List<String> ls3 = Arrays.asList("three:one", "three:two", "three:three");
|
List<String> ls3 = Arrays.asList("three:one", "three:two", "three:three");
|
||||||
|
|
||||||
List<List<String>> lol = new ArrayList<>();
|
List<List<String>> list = Arrays.asList(ls1, ls2, ls3);
|
||||||
lol.addAll(Arrays.asList(ls1, ls2, ls3));
|
|
||||||
|
|
||||||
// show nested list
|
// when
|
||||||
LOGGER.debug("\nNested list: ");
|
List<String> ls = flattenListOfListsImperatively(list);
|
||||||
lol.forEach((nl) -> LOGGER.debug(nl + ""));
|
|
||||||
|
|
||||||
// flatten it
|
|
||||||
List<String> ls = flol.flattenListOfLists(lol);
|
|
||||||
|
|
||||||
|
// then
|
||||||
assertNotNull(ls);
|
assertNotNull(ls);
|
||||||
assertTrue(ls.size() == 9);
|
assertTrue(ls.size() == 9);
|
||||||
|
//TODO content assertion
|
||||||
|
}
|
||||||
|
|
||||||
// show flattened list
|
@Test
|
||||||
LOGGER.debug("\nFlattened list:");
|
public void givenListOfListOfString_flattenNestedList2() {
|
||||||
ls.forEach((l) -> LOGGER.debug(l));
|
// given
|
||||||
|
List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three");
|
||||||
|
List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three");
|
||||||
|
List<String> ls3 = Arrays.asList("three:one", "three:two", "three:three");
|
||||||
|
|
||||||
|
List<List<String>> list = Arrays.asList(ls1, ls2, ls3);
|
||||||
|
|
||||||
|
// when
|
||||||
|
List<String> ls = flattenListOfListsStream(list);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertNotNull(ls);
|
||||||
|
assertTrue(ls.size() == 9);
|
||||||
|
//TODO content assertion
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> List<T> flattenListOfListsImperatively(List<List<T>> list) {
|
||||||
|
List<T> ls = new ArrayList<>();
|
||||||
|
list.forEach(ls::addAll);
|
||||||
|
return ls;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> List<T> flattenListOfListsStream(List<List<T>> list) {
|
||||||
|
return list.stream()
|
||||||
|
.flatMap(Collection::stream)
|
||||||
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
4
guice/README.md
Normal file
4
guice/README.md
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
## Google Guice Tutorials Project
|
||||||
|
|
||||||
|
### Relevant Articles
|
||||||
|
- [Guide to Google Guice](http://www.baeldung.com/guice)
|
@ -23,7 +23,7 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
|
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cglib</groupId>
|
<groupId>cglib</groupId>
|
||||||
@ -51,6 +51,11 @@
|
|||||||
<artifactId>javatuples</artifactId>
|
<artifactId>javatuples</artifactId>
|
||||||
<version>${javatuples.version}</version>
|
<version>${javatuples.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.javassist</groupId>
|
||||||
|
<artifactId>javassist</artifactId>
|
||||||
|
<version>${javaassist.version}</version>
|
||||||
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
|
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
@ -65,6 +70,7 @@
|
|||||||
<junit.version>4.12</junit.version>
|
<junit.version>4.12</junit.version>
|
||||||
<jasypt.version>1.9.2</jasypt.version>
|
<jasypt.version>1.9.2</jasypt.version>
|
||||||
<javatuples.version>1.2</javatuples.version>
|
<javatuples.version>1.2</javatuples.version>
|
||||||
|
<javaassist.version>3.21.0-GA</javaassist.version>
|
||||||
<assertj.version>3.6.2</assertj.version>
|
<assertj.version>3.6.2</assertj.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
17
libraries/src/main/java/com/baeldung/javasisst/Point.java
Normal file
17
libraries/src/main/java/com/baeldung/javasisst/Point.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.javasisst;
|
||||||
|
|
||||||
|
|
||||||
|
public class Point {
|
||||||
|
public int x = 0;
|
||||||
|
public int y = 0;
|
||||||
|
|
||||||
|
public Point(int x, int y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void move(int x, int y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.javasisst;
|
||||||
|
|
||||||
|
|
||||||
|
public class ThreeDimensionalPoint {
|
||||||
|
public int x = 0;
|
||||||
|
public int y = 0;
|
||||||
|
public int z = 0;
|
||||||
|
|
||||||
|
public ThreeDimensionalPoint(int x, int y, int z) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.z = z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void move(int x, int y) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,119 @@
|
|||||||
|
package com.baeldung.javassist;
|
||||||
|
|
||||||
|
|
||||||
|
import javassist.CannotCompileException;
|
||||||
|
import javassist.ClassPool;
|
||||||
|
import javassist.NotFoundException;
|
||||||
|
import javassist.bytecode.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
public class JavasisstTest {
|
||||||
|
@Test
|
||||||
|
public void givenJavasisstAPI_whenConstructClass_thenGenerateAClassFile() throws CannotCompileException, IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
|
||||||
|
//given
|
||||||
|
String classNameWithPackage = "com.baeldung.JavassistGeneratedClass";
|
||||||
|
ClassFile cf = new ClassFile(false, classNameWithPackage, null);
|
||||||
|
cf.setInterfaces(new String[]{"java.lang.Cloneable"});
|
||||||
|
|
||||||
|
FieldInfo f = new FieldInfo(cf.getConstPool(), "id", "I");
|
||||||
|
f.setAccessFlags(AccessFlag.PUBLIC);
|
||||||
|
cf.addField(f);
|
||||||
|
|
||||||
|
//when
|
||||||
|
String className = "JavassistGeneratedClass.class";
|
||||||
|
cf.write(new DataOutputStream(new FileOutputStream(className)));
|
||||||
|
|
||||||
|
//then
|
||||||
|
ClassPool classPool = ClassPool.getDefault();
|
||||||
|
Field[] fields = classPool.makeClass(cf).toClass().getFields();
|
||||||
|
assertEquals(fields[0].getName(), "id");
|
||||||
|
|
||||||
|
String classContent = new String(Files.readAllBytes(Paths.get(className)));
|
||||||
|
assertTrue(classContent.contains("java/lang/Cloneable"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJavaClass_whenLoadAtByJavassist_thenTraversWholeClass() throws NotFoundException, CannotCompileException, BadBytecode {
|
||||||
|
//given
|
||||||
|
ClassPool cp = ClassPool.getDefault();
|
||||||
|
ClassFile cf = cp.get("com.baeldung.javasisst.Point").getClassFile();
|
||||||
|
MethodInfo minfo = cf.getMethod("move");
|
||||||
|
CodeAttribute ca = minfo.getCodeAttribute();
|
||||||
|
CodeIterator ci = ca.iterator();
|
||||||
|
|
||||||
|
//when
|
||||||
|
List<String> operations = new LinkedList<>();
|
||||||
|
while (ci.hasNext()) {
|
||||||
|
int index = ci.next();
|
||||||
|
int op = ci.byteAt(index);
|
||||||
|
operations.add(Mnemonic.OPCODE[op]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//then
|
||||||
|
assertEquals(operations,
|
||||||
|
Arrays.asList("aload_0", "iload_1", "putfield", "aload_0", "iload_2", "putfield", "return"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTableOfInstructions_whenAddNewInstruction_thenShouldConstructProperSequence() throws NotFoundException, BadBytecode, CannotCompileException, IllegalAccessException, InstantiationException {
|
||||||
|
//given
|
||||||
|
ClassFile cf = ClassPool.getDefault().get("com.baeldung.javasisst.ThreeDimensionalPoint").getClassFile();
|
||||||
|
|
||||||
|
//when
|
||||||
|
FieldInfo f = new FieldInfo(cf.getConstPool(), "id", "I");
|
||||||
|
f.setAccessFlags(AccessFlag.PUBLIC);
|
||||||
|
cf.addField(f);
|
||||||
|
|
||||||
|
|
||||||
|
ClassPool classPool = ClassPool.getDefault();
|
||||||
|
Field[] fields = classPool.makeClass(cf).toClass().getFields();
|
||||||
|
List<String> fieldsList = Stream.of(fields).map(Field::getName).collect(Collectors.toList());
|
||||||
|
assertTrue(fieldsList.contains("id"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenLoadedClass_whenAddConstructorToClass_shouldCreateClassWithConstructor() throws NotFoundException, CannotCompileException, BadBytecode {
|
||||||
|
//given
|
||||||
|
ClassFile cf = ClassPool.getDefault().get("com.baeldung.javasisst.Point").getClassFile();
|
||||||
|
Bytecode code = new Bytecode(cf.getConstPool());
|
||||||
|
code.addAload(0);
|
||||||
|
code.addInvokespecial("java/lang/Object", MethodInfo.nameInit, "()V");
|
||||||
|
code.addReturn(null);
|
||||||
|
|
||||||
|
//when
|
||||||
|
MethodInfo minfo = new MethodInfo(cf.getConstPool(), MethodInfo.nameInit, "()V");
|
||||||
|
minfo.setCodeAttribute(code.toCodeAttribute());
|
||||||
|
cf.addMethod(minfo);
|
||||||
|
|
||||||
|
//then
|
||||||
|
CodeIterator ci = code.toCodeAttribute().iterator();
|
||||||
|
List<String> operations = new LinkedList<>();
|
||||||
|
while (ci.hasNext()) {
|
||||||
|
int index = ci.next();
|
||||||
|
int op = ci.byteAt(index);
|
||||||
|
operations.add(Mnemonic.OPCODE[op]);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(operations,
|
||||||
|
Arrays.asList("aload_0", "invokespecial", "return"));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@ buildscript {
|
|||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath "io.ratpack:ratpack-gradle:1.4.5"
|
classpath "io.ratpack:ratpack-gradle:1.4.5"
|
||||||
|
classpath "com.h2database:h2:1.4.193"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -19,6 +20,8 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
compile ratpack.dependency('hikari')
|
||||||
|
compile 'com.h2database:h2:1.4.193'
|
||||||
testCompile 'junit:junit:4.11'
|
testCompile 'junit:junit:4.11'
|
||||||
runtime "org.slf4j:slf4j-simple:1.7.21"
|
runtime "org.slf4j:slf4j-simple:1.7.21"
|
||||||
}
|
}
|
||||||
|
@ -15,22 +15,41 @@
|
|||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.ratpack</groupId>
|
<groupId>io.ratpack</groupId>
|
||||||
<artifactId>ratpack-core</artifactId>
|
<artifactId>ratpack-core</artifactId>
|
||||||
<version>1.4.5</version>
|
<version>1.4.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.ratpack</groupId>
|
<groupId>io.ratpack</groupId>
|
||||||
<artifactId>ratpack-test</artifactId>
|
<artifactId>ratpack-hikari</artifactId>
|
||||||
<version>1.4.5</version>
|
<version>1.4.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>io.ratpack</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>ratpack-test</artifactId>
|
||||||
<version>4.12</version>
|
<version>1.4.5</version>
|
||||||
<scope>test</scope>
|
</dependency>
|
||||||
</dependency>
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<version>1.4.193</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>${project.artifactId}</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -1,22 +1,48 @@
|
|||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.baeldung.filter.RequestValidatorFilter;
|
||||||
|
import com.baeldung.model.Employee;
|
||||||
|
|
||||||
|
import ratpack.guice.Guice;
|
||||||
|
import ratpack.hikari.HikariModule;
|
||||||
|
import ratpack.http.MutableHeaders;
|
||||||
|
import ratpack.jackson.Jackson;
|
||||||
import ratpack.http.MutableHeaders;
|
import ratpack.http.MutableHeaders;
|
||||||
import ratpack.server.RatpackServer;
|
import ratpack.server.RatpackServer;
|
||||||
|
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|
||||||
public static void main(String... args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
RatpackServer.start(server -> server.handlers(chain -> chain.all(ctx -> {
|
|
||||||
MutableHeaders headers = ctx.getResponse().getHeaders();
|
List<Employee> employees = new ArrayList<Employee>();
|
||||||
headers.set("Access-Control-Allow-Origin", "*");
|
employees.add(new Employee(1L, "Mr", "John Doe"));
|
||||||
headers.set("Accept-Language", "en-us");
|
employees.add(new Employee(2L, "Mr", "White Snow"));
|
||||||
headers.set("Accept-Charset", "UTF-8");
|
|
||||||
ctx.next();
|
|
||||||
})
|
RatpackServer.start(
|
||||||
.get(ctx -> ctx.render("Welcome to baeldung ratpack!!!"))
|
server -> server.registry(Guice.registry(bindings -> bindings.module(HikariModule.class, config -> {
|
||||||
.get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!"))
|
config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
|
||||||
.post(":amount", ctx -> ctx.render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!"))
|
config.addDataSourceProperty("URL",
|
||||||
));
|
"jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'");
|
||||||
|
}))).handlers(chain -> chain
|
||||||
|
.all(
|
||||||
|
// ctx -> {
|
||||||
|
// MutableHeaders headers =
|
||||||
|
// ctx.getResponse().getHeaders();
|
||||||
|
// headers.set("Access-Control-Allow-Origin","*");
|
||||||
|
// headers.set("Accept-Language", "en-us");
|
||||||
|
// headers.set("Accept-Charset", "UTF-8");
|
||||||
|
// ctx.next();
|
||||||
|
// }
|
||||||
|
new RequestValidatorFilter())
|
||||||
|
.get(ctx -> ctx.render("Welcome to baeldung ratpack!!!"))
|
||||||
|
.get("data/employees", ctx -> ctx.render(Jackson.json(employees)))
|
||||||
|
.get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!"))
|
||||||
|
.post(":amount", ctx -> ctx
|
||||||
|
.render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.filter;
|
||||||
|
|
||||||
|
import ratpack.handling.Context;
|
||||||
|
import ratpack.handling.Handler;
|
||||||
|
import ratpack.http.MutableHeaders;
|
||||||
|
|
||||||
|
public class RequestValidatorFilter implements Handler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(Context ctx) throws Exception {
|
||||||
|
MutableHeaders headers = ctx.getResponse().getHeaders();
|
||||||
|
headers.set("Access-Control-Allow-Origin", "*");
|
||||||
|
ctx.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
44
ratpack/src/main/java/com/baeldung/model/Employee.java
Normal file
44
ratpack/src/main/java/com/baeldung/model/Employee.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package com.baeldung.model;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class Employee implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 3077867088762010705L;
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
private String title;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Employee(Long id, String title, String name) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.title = title;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
6
ratpack/src/main/resources/DDL.sql
Normal file
6
ratpack/src/main/resources/DDL.sql
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
DROP TABLE IF EXISTS employee;
|
||||||
|
CREATE TABLE employee (
|
||||||
|
id bigint auto_increment primary key,
|
||||||
|
title varchar(255),
|
||||||
|
name varchar(255)
|
||||||
|
)
|
@ -4,10 +4,17 @@ import org.junit.After;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
|
import com.baeldung.model.Employee;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
import ratpack.test.MainClassApplicationUnderTest;
|
import ratpack.test.MainClassApplicationUnderTest;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
public class ApplicationTest {
|
public class ApplicationTest {
|
||||||
|
|
||||||
@ -23,6 +30,16 @@ public class ApplicationTest {
|
|||||||
assertEquals("Hello dummybot!!!", appUnderTest.getHttpClient().getText("/dummybot"));
|
assertEquals("Hello dummybot!!!", appUnderTest.getHttpClient().getText("/dummybot"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUrl_getListOfEmployee() throws JsonProcessingException {
|
||||||
|
List<Employee> employees = new ArrayList<Employee>();
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
employees.add(new Employee(1L, "Mr", "John Doe"));
|
||||||
|
employees.add(new Employee(2L, "Mr", "White Snow"));
|
||||||
|
|
||||||
|
assertEquals(mapper.writeValueAsString(employees), appUnderTest.getHttpClient().getText("/data/employees"));
|
||||||
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
appUnderTest.close();
|
appUnderTest.close();
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>1.5.1.RELEASE</version>
|
<version>1.5.2.RELEASE</version>
|
||||||
<relativePath/> <!-- lookup parent from repository -->
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.internationalization;
|
||||||
|
|
||||||
|
import javax.annotation.security.RolesAllowed;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class InternationalizationApp {
|
||||||
|
@RolesAllowed("*")
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.setProperty("security.basic.enabled", "false");
|
||||||
|
SpringApplication.run(InternationalizationApp.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.internationalization.config;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.LocaleResolver;
|
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
|
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||||
|
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableWebMvc
|
||||||
|
@ComponentScan(basePackages = "com.baeldung.internationalization.config")
|
||||||
|
public class MvcConfig extends WebMvcConfigurerAdapter {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocaleResolver localeResolver() {
|
||||||
|
SessionLocaleResolver slr = new SessionLocaleResolver();
|
||||||
|
slr.setDefaultLocale(Locale.US);
|
||||||
|
return slr;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LocaleChangeInterceptor localeChangeInterceptor() {
|
||||||
|
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
|
||||||
|
lci.setParamName("lang");
|
||||||
|
return lci;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
registry.addInterceptor(localeChangeInterceptor());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.internationalization.config;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class PageController {
|
||||||
|
|
||||||
|
@GetMapping("/international")
|
||||||
|
public String getInternationalPage() {
|
||||||
|
return "international";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -33,3 +33,11 @@ logging.level.org.springframework=INFO
|
|||||||
#Servlet Configuration
|
#Servlet Configuration
|
||||||
servlet.name=dispatcherExample
|
servlet.name=dispatcherExample
|
||||||
servlet.mapping=/dispatcherExampleURL
|
servlet.mapping=/dispatcherExampleURL
|
||||||
|
|
||||||
|
#banner.charset=UTF-8
|
||||||
|
#banner.location=classpath:banner.txt
|
||||||
|
#banner.image.location=classpath:banner.gif
|
||||||
|
#banner.image.width= //TODO
|
||||||
|
#banner.image.height= //TODO
|
||||||
|
#banner.image.margin= //TODO
|
||||||
|
#banner.image.invert= //TODO
|
14
spring-boot/src/main/resources/banner.txt
Normal file
14
spring-boot/src/main/resources/banner.txt
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||||||
|
@@@@@@@@@@@@@@@@@@@@@@@@@#@@@@@########@@@@@@@@@@@@@@@@@@@@@@@@...@@@@@@@@@:..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||||||
|
@@@@@@@@@@@@@@@@@@@@@@#. @@@@@* *@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||||||
|
@@@@@@@@@@@@@@@@@#o @@@@@* @@@@@* @@@:*.*@@@@@@@: *8@@@ @@@@&:.#@. @o**@@@@**:@o*o@@:.:@@@@@:.o#@&*:@@@@
|
||||||
|
@@@@@@@@@@@@* @@@@@* 8888 8@ @@@8 #@o 8@# .@ @@* :. @* @@@@ @. : &@ ** .@@@@
|
||||||
|
@@@@@@@@@@. @ o@@@@@* *@@@o::& .* 8@@@@. @@ 8@@@@. @* @@@@ @. @@@& * @@@@# .@@@@
|
||||||
|
@@@@@@@@@& @ @@@@@@* @@@@@@ 8 @@@@ .. o&&&&&&& @@ #@@@@. @* @@@@ @. @@@# * @@@@@ .@@@@
|
||||||
|
@@@@@@@@@ @@o @@@@@@@* oooo* 8 @@@& @* @@@ # 88. 88. *& o#: @. @@@# *@ &#& .@@@@
|
||||||
|
@@@@@@@@# @@@8 @@@@@@@* .*@@@#. *@@ @@@& :#@@@o .@@: *&@8 @o o@@: @. @@@# *@@#. :8# .@@@@
|
||||||
|
@@@@@@@@@ @@@@ &@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@# o@@@@ @@@@@
|
||||||
|
@@@@@& &@@@@ 8@@@@@@@@@8&8@@@@@#8#@@@o8@#&@@o&@@@&@@8@@&@@@@88@@8#@8&@@##@@@@@@#8@@#8@@88@@@@@ *@@@@@@@
|
||||||
|
@@@# #@@@@#. @@@@@@@@@@@@@8@@8#o@&#@@@@o.@o*@@*.@@@.@&:8o8*@@@8&@@#@@@8@@@@8@#@@@8&@@@@@@#@@@@@@@@@@@@@@@@@@@
|
||||||
|
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
||||||
|
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
4
spring-boot/src/main/resources/messages.properties
Normal file
4
spring-boot/src/main/resources/messages.properties
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
greeting=Hello! Welcome to our website!
|
||||||
|
lang.change=Change the language
|
||||||
|
lang.eng=English
|
||||||
|
lang.fr=French
|
4
spring-boot/src/main/resources/messages_fr.properties
Normal file
4
spring-boot/src/main/resources/messages_fr.properties
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
greeting=Bonjour! Bienvenue sur notre site!
|
||||||
|
lang.change=Changez la langue
|
||||||
|
lang.eng=Anglais
|
||||||
|
lang.fr=Francais
|
29
spring-boot/src/main/resources/templates/international.html
Normal file
29
spring-boot/src/main/resources/templates/international.html
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="ISO-8859-1" />
|
||||||
|
<title>Home</title>
|
||||||
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
$(document).ready(function() {
|
||||||
|
$("#locales").change(function () {
|
||||||
|
var selectedOption = $('#locales').val();
|
||||||
|
if (selectedOption != ''){
|
||||||
|
window.location.replace('international?lang=' + selectedOption);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1 th:text="#{greeting}"></h1>
|
||||||
|
|
||||||
|
<br /><br />
|
||||||
|
<span th:text="#{lang.change}"></span>:
|
||||||
|
<select id="locales">
|
||||||
|
<option value=""></option>
|
||||||
|
<option value="en" th:text="#{lang.eng}"></option>
|
||||||
|
<option value="fr" th:text="#{lang.fr}"></option>
|
||||||
|
</select>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -1,11 +1,19 @@
|
|||||||
package com.baeldung.spring.cloud.bootstrap.svcbook.book;
|
package com.baeldung.spring.cloud.bootstrap.svcbook.book;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PatchMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/books")
|
@RequestMapping("/books")
|
||||||
public class BookController {
|
public class BookController {
|
||||||
@ -13,7 +21,7 @@ public class BookController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private BookService bookService;
|
private BookService bookService;
|
||||||
|
|
||||||
@GetMapping("")
|
@GetMapping
|
||||||
public List<Book> findAllBooks() {
|
public List<Book> findAllBooks() {
|
||||||
return bookService.findAllBooks();
|
return bookService.findAllBooks();
|
||||||
}
|
}
|
||||||
@ -23,7 +31,7 @@ public class BookController {
|
|||||||
return bookService.findBookById(bookId);
|
return bookService.findBookById(bookId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("")
|
@PostMapping
|
||||||
public Book createBook(@RequestBody Book book) {
|
public Book createBook(@RequestBody Book book) {
|
||||||
return bookService.createBook(book);
|
return bookService.createBook(book);
|
||||||
}
|
}
|
||||||
@ -33,7 +41,12 @@ public class BookController {
|
|||||||
bookService.deleteBook(bookId);
|
bookService.deleteBook(bookId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping("/{bookId")
|
@PutMapping("/{bookId}")
|
||||||
|
public Book updateBook(@RequestBody Book book, @PathVariable Long bookId) {
|
||||||
|
return bookService.updateBook(book, bookId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/{bookId}")
|
||||||
public Book updateBook(@RequestBody Map<String, String> updates, @PathVariable Long bookId) {
|
public Book updateBook(@RequestBody Map<String, String> updates, @PathVariable Long bookId) {
|
||||||
return bookService.updateBook(updates, bookId);
|
return bookService.updateBook(updates, bookId);
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
package com.baeldung.spring.cloud.bootstrap.svcbook.book;
|
package com.baeldung.spring.cloud.bootstrap.svcbook.book;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Propagation;
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.List;
|
import com.google.common.base.Preconditions;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
@ -27,7 +29,7 @@ public class BookService {
|
|||||||
|
|
||||||
@Transactional(propagation = Propagation.REQUIRED)
|
@Transactional(propagation = Propagation.REQUIRED)
|
||||||
public Book createBook(Book book) {
|
public Book createBook(Book book) {
|
||||||
Book newBook = new Book();
|
final Book newBook = new Book();
|
||||||
newBook.setTitle(book.getTitle());
|
newBook.setTitle(book.getTitle());
|
||||||
newBook.setAuthor(book.getAuthor());
|
newBook.setAuthor(book.getAuthor());
|
||||||
return bookRepository.save(newBook);
|
return bookRepository.save(newBook);
|
||||||
@ -40,16 +42,25 @@ public class BookService {
|
|||||||
|
|
||||||
@Transactional(propagation = Propagation.REQUIRED)
|
@Transactional(propagation = Propagation.REQUIRED)
|
||||||
public Book updateBook(Map<String, String> updates, Long bookId) {
|
public Book updateBook(Map<String, String> updates, Long bookId) {
|
||||||
Book book = findBookById(bookId);
|
final Book book = findBookById(bookId);
|
||||||
updates.keySet().forEach(key -> {
|
updates.keySet()
|
||||||
switch (key) {
|
.forEach(key -> {
|
||||||
case "author":
|
switch (key) {
|
||||||
book.setAuthor(updates.get(key));
|
case "author":
|
||||||
break;
|
book.setAuthor(updates.get(key));
|
||||||
case "title":
|
break;
|
||||||
book.setTitle(updates.get(key));
|
case "title":
|
||||||
}
|
book.setTitle(updates.get(key));
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
return bookRepository.save(book);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(propagation = Propagation.REQUIRED)
|
||||||
|
public Book updateBook(Book book, Long bookId) {
|
||||||
|
Preconditions.checkNotNull(book);
|
||||||
|
Preconditions.checkState(book.getId() == bookId);
|
||||||
|
Preconditions.checkNotNull(bookRepository.findOne(bookId));
|
||||||
return bookRepository.save(book);
|
return bookRepository.save(book);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,20 @@
|
|||||||
package com.baeldung.spring.cloud.bootstrap.svcrating.rating;
|
package com.baeldung.spring.cloud.bootstrap.svcrating.rating;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PatchMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/ratings")
|
@RequestMapping("/ratings")
|
||||||
public class RatingController {
|
public class RatingController {
|
||||||
@ -13,7 +22,7 @@ public class RatingController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private RatingService ratingService;
|
private RatingService ratingService;
|
||||||
|
|
||||||
@GetMapping("")
|
@GetMapping
|
||||||
public List<Rating> findRatingsByBookId(@RequestParam(required = false, defaultValue = "0") Long bookId) {
|
public List<Rating> findRatingsByBookId(@RequestParam(required = false, defaultValue = "0") Long bookId) {
|
||||||
if (bookId.equals(0L)) {
|
if (bookId.equals(0L)) {
|
||||||
return ratingService.findAllRatings();
|
return ratingService.findAllRatings();
|
||||||
@ -21,7 +30,7 @@ public class RatingController {
|
|||||||
return ratingService.findRatingsByBookId(bookId);
|
return ratingService.findRatingsByBookId(bookId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("")
|
@PostMapping
|
||||||
public Rating createRating(@RequestBody Rating rating) {
|
public Rating createRating(@RequestBody Rating rating) {
|
||||||
return ratingService.createRating(rating);
|
return ratingService.createRating(rating);
|
||||||
}
|
}
|
||||||
@ -31,7 +40,12 @@ public class RatingController {
|
|||||||
ratingService.deleteRating(ratingId);
|
ratingService.deleteRating(ratingId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping("/{ratingId")
|
@PutMapping("/{ratingId}")
|
||||||
|
public Rating updateRating(@RequestBody Rating rating, @PathVariable Long ratingId) {
|
||||||
|
return ratingService.updateRating(rating, ratingId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PatchMapping("/{ratingId}")
|
||||||
public Rating updateRating(@RequestBody Map<String, String> updates, @PathVariable Long ratingId) {
|
public Rating updateRating(@RequestBody Map<String, String> updates, @PathVariable Long ratingId) {
|
||||||
return ratingService.updateRating(updates, ratingId);
|
return ratingService.updateRating(updates, ratingId);
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,15 @@
|
|||||||
package com.baeldung.spring.cloud.bootstrap.svcrating.rating;
|
package com.baeldung.spring.cloud.bootstrap.svcrating.rating;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Propagation;
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.List;
|
import com.google.common.base.Preconditions;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
@ -31,7 +33,7 @@ public class RatingService {
|
|||||||
|
|
||||||
@Transactional(propagation = Propagation.REQUIRED)
|
@Transactional(propagation = Propagation.REQUIRED)
|
||||||
public Rating createRating(Rating rating) {
|
public Rating createRating(Rating rating) {
|
||||||
Rating newRating = new Rating();
|
final Rating newRating = new Rating();
|
||||||
newRating.setBookId(rating.getBookId());
|
newRating.setBookId(rating.getBookId());
|
||||||
newRating.setStars(rating.getStars());
|
newRating.setStars(rating.getStars());
|
||||||
return ratingRepository.save(newRating);
|
return ratingRepository.save(newRating);
|
||||||
@ -44,14 +46,22 @@ public class RatingService {
|
|||||||
|
|
||||||
@Transactional(propagation = Propagation.REQUIRED)
|
@Transactional(propagation = Propagation.REQUIRED)
|
||||||
public Rating updateRating(Map<String, String> updates, Long ratingId) {
|
public Rating updateRating(Map<String, String> updates, Long ratingId) {
|
||||||
Rating rating = findRatingById(ratingId);
|
final Rating rating = findRatingById(ratingId);
|
||||||
updates.keySet().forEach(key -> {
|
updates.keySet()
|
||||||
switch (key) {
|
.forEach(key -> {
|
||||||
|
switch (key) {
|
||||||
case "stars":
|
case "stars":
|
||||||
rating.setStars(Integer.parseInt(updates.get(key)));
|
rating.setStars(Integer.parseInt(updates.get(key)));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return ratingRepository.save(rating);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Rating updateRating(Rating rating, Long ratingId) {
|
||||||
|
Preconditions.checkNotNull(rating);
|
||||||
|
Preconditions.checkState(rating.getId() == ratingId);
|
||||||
|
Preconditions.checkNotNull(ratingRepository.findOne(ratingId));
|
||||||
return ratingRepository.save(rating);
|
return ratingRepository.save(rating);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.beaninjection;
|
||||||
|
|
||||||
|
public class AnotherSampleDAOBean implements IAnotherSampleDAO {
|
||||||
|
|
||||||
|
private String propertyY;
|
||||||
|
|
||||||
|
public AnotherSampleDAOBean(String propertyY) {
|
||||||
|
this.propertyY = propertyY;
|
||||||
|
}
|
||||||
|
|
||||||
|
// standard setters and getters
|
||||||
|
|
||||||
|
public String getPropertyY() {
|
||||||
|
return propertyY;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.baeldung.beaninjection;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ExampleDAOBean implements IExampleDAO {
|
||||||
|
|
||||||
|
private String propertyX;
|
||||||
|
|
||||||
|
public ExampleDAOBean(String propertyX) {
|
||||||
|
this.propertyX = propertyX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPropertyX() {
|
||||||
|
return propertyX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPropertyX(String propertyX) {
|
||||||
|
this.propertyX = propertyX;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<SampleDomainObject> getDomainList() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SampleDomainObject createNewDomain(SampleDomainObject inputDomain) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SampleDomainObject getSomeDomain() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return new SampleDomainObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.baeldung.beaninjection;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ExampleServiceBean implements IExampleService {
|
||||||
|
|
||||||
|
private IExampleDAO exampleDAO;
|
||||||
|
private IAnotherSampleDAO anotherSampleDAO;
|
||||||
|
|
||||||
|
public ExampleServiceBean(IExampleDAO exampleDAO) {
|
||||||
|
this.exampleDAO = exampleDAO;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAnotherSampleDAO(IAnotherSampleDAO anotherSampleDAO) {
|
||||||
|
this.anotherSampleDAO = anotherSampleDAO;
|
||||||
|
}
|
||||||
|
|
||||||
|
// standard setters and getters
|
||||||
|
|
||||||
|
public IAnotherSampleDAO getAnotherSampleDAO() {
|
||||||
|
return anotherSampleDAO;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExampleDAO(ExampleDAOBean exampleDAO) {
|
||||||
|
this.exampleDAO = exampleDAO;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IExampleDAO getExampleDAO() {
|
||||||
|
return exampleDAO;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String propertyX;
|
||||||
|
|
||||||
|
public String getPropertyX() {
|
||||||
|
return propertyX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPropertyX(String propertyX) {
|
||||||
|
this.propertyX = propertyX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SampleDomainObject> serviceMethodX() {
|
||||||
|
/*get domain list from DAO .. business logic on domain objects..return*/
|
||||||
|
return exampleDAO.getDomainList();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.baeldung.beaninjection;
|
||||||
|
|
||||||
|
public interface IAnotherSampleDAO {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.beaninjection;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface IExampleDAO {
|
||||||
|
|
||||||
|
List<SampleDomainObject> getDomainList();
|
||||||
|
|
||||||
|
SampleDomainObject createNewDomain(SampleDomainObject domainObject);
|
||||||
|
|
||||||
|
SampleDomainObject getSomeDomain();
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.baeldung.beaninjection;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface IExampleService {
|
||||||
|
|
||||||
|
List<SampleDomainObject> serviceMethodX();
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.beaninjection;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class SampleDomainObject implements Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 449859763481296747L;
|
||||||
|
|
||||||
|
private String domainPropX;
|
||||||
|
private String domainPropY;
|
||||||
|
|
||||||
|
public String getDomainPropX() {
|
||||||
|
return domainPropX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDomainPropX(String domainPropX) {
|
||||||
|
this.domainPropX = domainPropX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDomainPropY() {
|
||||||
|
return domainPropY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDomainPropY(String domainPropY) {
|
||||||
|
this.domainPropY = domainPropY;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.baeldung.configuration;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import com.baeldung.beaninjection.AnotherSampleDAOBean;
|
||||||
|
import com.baeldung.beaninjection.ExampleDAOBean;
|
||||||
|
import com.baeldung.beaninjection.ExampleServiceBean;
|
||||||
|
import com.baeldung.beaninjection.IAnotherSampleDAO;
|
||||||
|
import com.baeldung.beaninjection.IExampleDAO;
|
||||||
|
import com.baeldung.beaninjection.IExampleService;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan(basePackages = { "com.baeldung.beaninjection" })
|
||||||
|
public class ApplicationContextTestBeanInjectionTypes {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public IExampleDAO exampleDAO() {
|
||||||
|
return new ExampleDAOBean("Mandatory DAO Property X");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public IExampleService exampleServiceBean() {
|
||||||
|
ExampleServiceBean serviceBean = new ExampleServiceBean(exampleDAO());
|
||||||
|
serviceBean.setAnotherSampleDAO(anotherSampleDAO());
|
||||||
|
serviceBean.setPropertyX("Some Service Property X");
|
||||||
|
return serviceBean;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public IAnotherSampleDAO anotherSampleDAO() {
|
||||||
|
return new AnotherSampleDAOBean("Mandatory DAO Property Y");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||||
|
|
||||||
|
<bean id="exampleDAO" class="com.baeldung.beaninjection.ExampleDAOBean">
|
||||||
|
<constructor-arg index="0" type="java.lang.String"
|
||||||
|
value="Mandatory DAO Property X" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<bean id="anotherSampleDAO" class="com.baeldung.beaninjection.AnotherSampleDAOBean">
|
||||||
|
<constructor-arg index="0" type="java.lang.String"
|
||||||
|
value="Mandatory DAO Property Y" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<bean id="exampleService" class="com.baeldung.beaninjection.ExampleServiceBean">
|
||||||
|
<constructor-arg index="0" ref="exampleDAO" />
|
||||||
|
<property name="propertyX" value="Some Service Property X"></property>
|
||||||
|
<property name="anotherSampleDAO" ref="anotherSampleDAO"></property>
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
</beans>
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.baeldung.test.beaninjection;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import com.baeldung.beaninjection.AnotherSampleDAOBean;
|
||||||
|
import com.baeldung.beaninjection.ExampleDAOBean;
|
||||||
|
import com.baeldung.beaninjection.ExampleServiceBean;
|
||||||
|
import com.baeldung.configuration.ApplicationContextTestBeanInjectionTypes;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = ApplicationContextTestBeanInjectionTypes.class)
|
||||||
|
public class BeanInjectionJavaConfigTest implements ApplicationContextAware {
|
||||||
|
|
||||||
|
private ApplicationContext beanInjectedContext;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDAOInjectionByJava() {
|
||||||
|
ExampleServiceBean serviceBean = beanInjectedContext.getBean(ExampleServiceBean.class);
|
||||||
|
assertNotNull("Failed: Constructor Injection,Bean Reference Injection,Java Config, ExampleServiceBean", serviceBean.getExampleDAO());
|
||||||
|
assertNotNull("Failed: Constructor Injection,Bean Reference Injection,Java Config, ExampleServiceBean", serviceBean.getAnotherSampleDAO());
|
||||||
|
assertTrue("Failed: Constructor Injection,String Property , Java Config", serviceBean.getPropertyX()
|
||||||
|
.equals("Some Service Property X"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPropertyInjectioninDAOByJava() {
|
||||||
|
ExampleDAOBean daoBean = beanInjectedContext.getBean(ExampleDAOBean.class);
|
||||||
|
assertTrue("Failed: Constructor Injection,String Property , Java Config", daoBean.getPropertyX()
|
||||||
|
.equals("Mandatory DAO Property X"));
|
||||||
|
|
||||||
|
AnotherSampleDAOBean anotherDAOBean = beanInjectedContext.getBean(AnotherSampleDAOBean.class);
|
||||||
|
assertTrue("Failed: Constructor Injection,String Property , XML Config", anotherDAOBean.getPropertyY()
|
||||||
|
.equals("Mandatory DAO Property Y"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
this.beanInjectedContext = applicationContext;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.baeldung.test.beaninjection;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import com.baeldung.beaninjection.AnotherSampleDAOBean;
|
||||||
|
import com.baeldung.beaninjection.ExampleDAOBean;
|
||||||
|
import com.baeldung.beaninjection.ExampleServiceBean;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(locations = "classpath:beaninjectiontypes-context.xml")
|
||||||
|
public class BeanInjectionXMLConfigTest implements ApplicationContextAware {
|
||||||
|
|
||||||
|
private ApplicationContext beanInjectedContext;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDAOInjectionByXML() {
|
||||||
|
ExampleServiceBean serviceBean = beanInjectedContext.getBean(ExampleServiceBean.class);
|
||||||
|
assertNotNull("Failed: Constructor Injection,Bean Reference Injection,XML Config, ExampleServiceBean", serviceBean.getExampleDAO());
|
||||||
|
assertNotNull("Failed: Constructor Injection,Bean Reference Injection,XML Config, ExampleServiceBean", serviceBean.getAnotherSampleDAO());
|
||||||
|
assertTrue("Failed: Constructor Injection,String Property , XML Config", serviceBean.getPropertyX()
|
||||||
|
.equals("Some Service Property X"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPropertyInjectioninDAOByXML() {
|
||||||
|
ExampleDAOBean daoBean = beanInjectedContext.getBean(ExampleDAOBean.class);
|
||||||
|
assertTrue("Failed: Constructor Injection,String Property , XML Config", daoBean.getPropertyX()
|
||||||
|
.equals("Mandatory DAO Property X"));
|
||||||
|
|
||||||
|
AnotherSampleDAOBean anotherDAOBean = beanInjectedContext.getBean(AnotherSampleDAOBean.class);
|
||||||
|
assertTrue("Failed: Constructor Injection,String Property , XML Config", anotherDAOBean.getPropertyY()
|
||||||
|
.equals("Mandatory DAO Property Y"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
this.beanInjectedContext = applicationContext;
|
||||||
|
}
|
||||||
|
}
|
9
spring-kafka/README.md
Normal file
9
spring-kafka/README.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# Spring Kakfa
|
||||||
|
|
||||||
|
This is a simple Spring Boot app to demonstrate sending and receiving of messages in Kafka using spring-kafka.
|
||||||
|
|
||||||
|
As Kafka topics are not created automatically by default, this application requires that a topic named 'baeldung' is created manually.
|
||||||
|
|
||||||
|
`$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic baeldung`
|
||||||
|
|
||||||
|
Two listeners with group Ids **foo** and **bar** are configured. When run successfully, the *Hello World!* message will be received by both the listeners and logged on console.
|
46
spring-kafka/pom.xml
Normal file
46
spring-kafka/pom.xml
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<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</groupId>
|
||||||
|
<artifactId>spring-kafka</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<name>spring-kafka</name>
|
||||||
|
<description>Intro to Kafka with Spring</description>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
<spring-kafka.version>1.1.3.RELEASE</spring-kafka.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>1.5.2.RELEASE</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.kafka</groupId>
|
||||||
|
<artifactId>spring-kafka</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,72 @@
|
|||||||
|
package com.baeldung.spring.kafka;
|
||||||
|
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.kafka.annotation.KafkaListener;
|
||||||
|
import org.springframework.kafka.core.KafkaTemplate;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class KafkaApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
ConfigurableApplicationContext context = SpringApplication.run(KafkaApplication.class, args);
|
||||||
|
MessageProducer producer = context.getBean(MessageProducer.class);
|
||||||
|
producer.sendMessage("Hello, World!");
|
||||||
|
|
||||||
|
MessageListener listener = context.getBean(MessageListener.class);
|
||||||
|
listener.latch.await(20, TimeUnit.SECONDS);
|
||||||
|
Thread.sleep(60000);
|
||||||
|
context.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MessageProducer messageProducer() {
|
||||||
|
return new MessageProducer();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MessageListener messageListener() {
|
||||||
|
return new MessageListener();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class MessageProducer {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private KafkaTemplate<String, String> kafkaTemplate;
|
||||||
|
|
||||||
|
@Value(value = "${message.topic.name}")
|
||||||
|
private String topicName;
|
||||||
|
|
||||||
|
public void sendMessage(String message) {
|
||||||
|
kafkaTemplate.send(topicName, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class MessageListener {
|
||||||
|
|
||||||
|
private CountDownLatch latch = new CountDownLatch(2);
|
||||||
|
|
||||||
|
@KafkaListener(topics = "${message.topic.name}", group = "foo", containerFactory = "fooKafkaListenerContainerFactory")
|
||||||
|
public void listenGroupFoo(String message) {
|
||||||
|
System.out.println("Received Messasge in group 'foo': " + message);
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@KafkaListener(topics = "${message.topic.name}", group = "bar", containerFactory = "barKafkaListenerContainerFactory")
|
||||||
|
public void listenGroupBar(String message) {
|
||||||
|
System.out.println("Received Messasge in group 'bar': " + message);
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.baeldung.spring.kafka;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.kafka.clients.consumer.ConsumerConfig;
|
||||||
|
import org.apache.kafka.common.serialization.StringDeserializer;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.kafka.annotation.EnableKafka;
|
||||||
|
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
|
||||||
|
import org.springframework.kafka.core.ConsumerFactory;
|
||||||
|
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
|
||||||
|
|
||||||
|
@EnableKafka
|
||||||
|
@Configuration
|
||||||
|
public class KafkaConsumerConfig {
|
||||||
|
|
||||||
|
@Value(value = "${kafka.bootstrapAddress}")
|
||||||
|
private String bootstrapAddress;
|
||||||
|
|
||||||
|
public ConsumerFactory<String, String> consumerFactory(String groupId) {
|
||||||
|
Map<String, Object> props = new HashMap<>();
|
||||||
|
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
|
||||||
|
props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
|
||||||
|
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
|
||||||
|
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
|
||||||
|
return new DefaultKafkaConsumerFactory<>(props);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ConcurrentKafkaListenerContainerFactory<String, String> fooKafkaListenerContainerFactory() {
|
||||||
|
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
|
||||||
|
factory.setConsumerFactory(consumerFactory("foo"));
|
||||||
|
return factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ConcurrentKafkaListenerContainerFactory<String, String> barKafkaListenerContainerFactory() {
|
||||||
|
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
|
||||||
|
factory.setConsumerFactory(consumerFactory("bar"));
|
||||||
|
return factory;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.baeldung.spring.kafka;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.kafka.clients.producer.ProducerConfig;
|
||||||
|
import org.apache.kafka.common.serialization.StringSerializer;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
|
||||||
|
import org.springframework.kafka.core.KafkaTemplate;
|
||||||
|
import org.springframework.kafka.core.ProducerFactory;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class KafkaProducerConfig {
|
||||||
|
|
||||||
|
@Value(value = "${kafka.bootstrapAddress}")
|
||||||
|
private String bootstrapAddress;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ProducerFactory<String, String> producerFactory() {
|
||||||
|
Map<String, Object> configProps = new HashMap<String, Object>();
|
||||||
|
configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
|
||||||
|
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
|
||||||
|
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
|
||||||
|
return new DefaultKafkaProducerFactory<String, String>(configProps);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public KafkaTemplate<String, String> kafkaTemplate() {
|
||||||
|
KafkaTemplate<String, String> template =
|
||||||
|
new KafkaTemplate<String, String>(producerFactory());
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
}
|
2
spring-kafka/src/main/resources/application.properties
Normal file
2
spring-kafka/src/main/resources/application.properties
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
kafka.bootstrapAddress=localhost:9092
|
||||||
|
message.topic.name=baeldung
|
@ -6,3 +6,4 @@ The "REST With Spring" Classes: http://github.learnspringsecurity.com
|
|||||||
- [Custom AccessDecisionVoters in Spring Security](http://www.baeldung.com/spring-security-custom-voter)
|
- [Custom AccessDecisionVoters in Spring Security](http://www.baeldung.com/spring-security-custom-voter)
|
||||||
- [Spring Security: Authentication with a Database-backed UserDetailsService](http://www.baeldung.com/spring-security-authentication-with-a-database)
|
- [Spring Security: Authentication with a Database-backed UserDetailsService](http://www.baeldung.com/spring-security-authentication-with-a-database)
|
||||||
- [Two Login Pages with Spring Security](http://www.baeldung.com/spring-security-two-login-pages)
|
- [Two Login Pages with Spring Security](http://www.baeldung.com/spring-security-two-login-pages)
|
||||||
|
- [Multiple Entry Points in Spring Security](http://www.baeldung.com/spring-security-multiple-entry-points)
|
||||||
|
@ -9,6 +9,10 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur
|
|||||||
import org.springframework.security.core.userdetails.User;
|
import org.springframework.security.core.userdetails.User;
|
||||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||||
|
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||||
|
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
|
||||||
|
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
|
||||||
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
@ -26,51 +30,60 @@ public class MultipleEntryPointsSecurityConfig {
|
|||||||
@Order(1)
|
@Order(1)
|
||||||
public static class App1ConfigurationAdapter extends WebSecurityConfigurerAdapter {
|
public static class App1ConfigurationAdapter extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
public App1ConfigurationAdapter() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
//@formatter:off
|
//@formatter:off
|
||||||
http.antMatcher("/admin/**")
|
http.antMatcher("/admin/**")
|
||||||
.authorizeRequests().anyRequest().hasRole("ADMIN")
|
.authorizeRequests().anyRequest().hasRole("ADMIN")
|
||||||
.and().httpBasic()
|
.and().httpBasic().authenticationEntryPoint(authenticationEntryPoint())
|
||||||
.and().exceptionHandling().accessDeniedPage("/403");
|
.and().exceptionHandling().accessDeniedPage("/403");
|
||||||
//@formatter:on
|
//@formatter:on
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public AuthenticationEntryPoint authenticationEntryPoint(){
|
||||||
|
BasicAuthenticationEntryPoint entryPoint = new BasicAuthenticationEntryPoint();
|
||||||
|
entryPoint.setRealmName("admin realm");
|
||||||
|
return entryPoint;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Order(2)
|
@Order(2)
|
||||||
public static class App2ConfigurationAdapter extends WebSecurityConfigurerAdapter {
|
public static class App2ConfigurationAdapter extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
public App2ConfigurationAdapter() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
|
|
||||||
//@formatter:off
|
//@formatter:off
|
||||||
http.antMatcher("/user/**")
|
http.antMatcher("/user/**")
|
||||||
.authorizeRequests().anyRequest().hasRole("USER")
|
.authorizeRequests().anyRequest().hasRole("USER")
|
||||||
.and().formLogin().loginPage("/userLogin").loginProcessingUrl("/user/login")
|
.and().formLogin().loginProcessingUrl("/user/login")
|
||||||
.failureUrl("/userLogin?error=loginError").defaultSuccessUrl("/user/myUserPage")
|
.failureUrl("/userLogin?error=loginError").defaultSuccessUrl("/user/myUserPage")
|
||||||
.and().logout().logoutUrl("/user/logout").logoutSuccessUrl("/multipleHttpLinks")
|
.and().logout().logoutUrl("/user/logout").logoutSuccessUrl("/multipleHttpLinks")
|
||||||
.deleteCookies("JSESSIONID")
|
.deleteCookies("JSESSIONID")
|
||||||
.and().exceptionHandling().accessDeniedPage("/403")
|
.and().exceptionHandling()
|
||||||
|
.defaultAuthenticationEntryPointFor(loginUrlauthenticationEntryPointWithWarning(), new AntPathRequestMatcher("/user/private/**"))
|
||||||
|
.defaultAuthenticationEntryPointFor(loginUrlauthenticationEntryPoint(), new AntPathRequestMatcher("/user/general/**"))
|
||||||
|
.accessDeniedPage("/403")
|
||||||
.and().csrf().disable();
|
.and().csrf().disable();
|
||||||
//@formatter:on
|
//@formatter:on
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public AuthenticationEntryPoint loginUrlauthenticationEntryPoint(){
|
||||||
|
return new LoginUrlAuthenticationEntryPoint("/userLogin");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public AuthenticationEntryPoint loginUrlauthenticationEntryPointWithWarning(){
|
||||||
|
return new LoginUrlAuthenticationEntryPoint("/userLoginWithWarning");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Order(3)
|
@Order(3)
|
||||||
public static class App3ConfigurationAdapter extends WebSecurityConfigurerAdapter {
|
public static class App3ConfigurationAdapter extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
public App3ConfigurationAdapter() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
http.antMatcher("/guest/**").authorizeRequests().anyRequest().permitAll();
|
http.antMatcher("/guest/**").authorizeRequests().anyRequest().permitAll();
|
||||||
}
|
}
|
||||||
|
@ -16,11 +16,16 @@ public class PagesController {
|
|||||||
return "multipleHttpElems/myAdminPage";
|
return "multipleHttpElems/myAdminPage";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/user/myUserPage")
|
@RequestMapping("/user/general/myUserPage")
|
||||||
public String getUserPage() {
|
public String getUserPage() {
|
||||||
return "multipleHttpElems/myUserPage";
|
return "multipleHttpElems/myUserPage";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/user/private/myPrivateUserPage")
|
||||||
|
public String getPrivateUserPage() {
|
||||||
|
return "multipleHttpElems/myPrivateUserPage";
|
||||||
|
}
|
||||||
|
|
||||||
@RequestMapping("/guest/myGuestPage")
|
@RequestMapping("/guest/myGuestPage")
|
||||||
public String getGuestPage() {
|
public String getGuestPage() {
|
||||||
return "multipleHttpElems/myGuestPage";
|
return "multipleHttpElems/myGuestPage";
|
||||||
@ -31,6 +36,11 @@ public class PagesController {
|
|||||||
return "multipleHttpElems/login";
|
return "multipleHttpElems/login";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/userLoginWithWarning")
|
||||||
|
public String getUserLoginPageWithWarning() {
|
||||||
|
return "multipleHttpElems/loginWithWarning";
|
||||||
|
}
|
||||||
|
|
||||||
@RequestMapping("/403")
|
@RequestMapping("/403")
|
||||||
public String getAccessDeniedPage() {
|
public String getAccessDeniedPage() {
|
||||||
return "403";
|
return "403";
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:security="http://www.springframework.org/schema/security"
|
xmlns:security="http://www.springframework.org/schema/security"
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.1.xsd
|
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd
|
||||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||||
|
|
||||||
<security:authentication-manager>
|
<security:authentication-manager>
|
||||||
@ -14,9 +14,10 @@
|
|||||||
</security:authentication-provider>
|
</security:authentication-provider>
|
||||||
</security:authentication-manager>
|
</security:authentication-manager>
|
||||||
|
|
||||||
<security:http pattern="/user/**" use-expressions="true" auto-config="true">
|
<security:http pattern="/user/general/**" use-expressions="true" auto-config="true"
|
||||||
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
|
entry-point-ref="loginUrlAuthenticationEntryPoint">
|
||||||
<security:form-login login-page="/userLogin" login-processing-url="/user/login"
|
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
|
||||||
|
<security:form-login login-processing-url="/user/general/login"
|
||||||
authentication-failure-url="/userLogin?error=loginError"
|
authentication-failure-url="/userLogin?error=loginError"
|
||||||
default-target-url="/user/myUserPage"/>
|
default-target-url="/user/myUserPage"/>
|
||||||
<security:csrf disabled="true"/>
|
<security:csrf disabled="true"/>
|
||||||
@ -24,14 +25,41 @@
|
|||||||
<security:logout logout-url="/user/logout" delete-cookies="JSESSIONID" logout-success-url="/multipleHttpLinks"/>
|
<security:logout logout-url="/user/logout" delete-cookies="JSESSIONID" logout-success-url="/multipleHttpLinks"/>
|
||||||
</security:http>
|
</security:http>
|
||||||
|
|
||||||
|
<bean id="loginUrlAuthenticationEntryPoint"
|
||||||
|
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
|
||||||
|
<constructor-arg name="loginFormUrl" value="/userLogin" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<security:http pattern="/user/private/**" use-expressions="true" auto-config="true"
|
||||||
|
entry-point-ref="loginUrlAuthenticationEntryPointWithWarning">
|
||||||
|
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
|
||||||
|
<security:form-login login-processing-url="/user/private/login"
|
||||||
|
authentication-failure-url="/userLogin?error=loginError"
|
||||||
|
default-target-url="/user/myUserPage" />
|
||||||
|
<security:csrf disabled="true"/>
|
||||||
|
<security:access-denied-handler error-page="/403"/>
|
||||||
|
<security:logout logout-url="/user/logout" delete-cookies="JSESSIONID" logout-success-url="/multipleHttpLinks"/>
|
||||||
|
</security:http>
|
||||||
|
|
||||||
|
<bean id="loginUrlAuthenticationEntryPointWithWarning"
|
||||||
|
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
|
||||||
|
<constructor-arg name="loginFormUrl" value="/userLoginWithWarning" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
<security:http pattern="/admin/**" use-expressions="true" auto-config="true">
|
<security:http pattern="/admin/**" use-expressions="true" auto-config="true">
|
||||||
<security:intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')"/>
|
<security:intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')"/>
|
||||||
<security:http-basic/>
|
<security:http-basic entry-point-ref="authenticationEntryPoint" />
|
||||||
<security:access-denied-handler error-page="/403"/>
|
<security:access-denied-handler error-page="/403"/>
|
||||||
</security:http>
|
</security:http>
|
||||||
|
|
||||||
|
<bean id="authenticationEntryPoint"
|
||||||
|
class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
|
||||||
|
<property name="realmName" value="admin realm" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
<security:http pattern="/**" use-expressions="true" auto-config="true">
|
<security:http pattern="/**" use-expressions="true" auto-config="true">
|
||||||
<security:intercept-url pattern="/guest/**" access="permitAll()"/>
|
<security:intercept-url pattern="/guest/**" access="permitAll()"/>
|
||||||
</security:http>
|
</security:http>
|
||||||
|
|
||||||
|
|
||||||
</beans>
|
</beans>
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
<html>
|
||||||
|
<head></head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>Login</h1>
|
||||||
|
<h3>Warning! You are about to access sensible data!</h3>
|
||||||
|
|
||||||
|
<form name='f' action="user/login" method='POST'>
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<td>Username:</td>
|
||||||
|
<td><input type="text" name="username" /></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Password:</td>
|
||||||
|
<td><input type="password" name="password" /></td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td><input name="submit" type="submit" value="submit" /></td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
</form>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -8,7 +8,9 @@
|
|||||||
|
|
||||||
<a th:href="@{/admin/myAdminPage}">Admin page</a>
|
<a th:href="@{/admin/myAdminPage}">Admin page</a>
|
||||||
<br />
|
<br />
|
||||||
<a th:href="@{/user/myUserPage}">User page</a>
|
<a th:href="@{/user/general/myUserPage}">User page</a>
|
||||||
|
<br />
|
||||||
|
<a th:href="@{/user/private/myPrivateUserPage}">Private user page</a>
|
||||||
<br />
|
<br />
|
||||||
<a th:href="@{/guest/myGuestPage}">Guest page</a>
|
<a th:href="@{/guest/myGuestPage}">Guest page</a>
|
||||||
|
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="ISO-8859-1" />
|
||||||
|
<title>Insert title here</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
Welcome user to your private page! <a th:href="@{/user/logout}" >Logout</a>
|
||||||
|
|
||||||
|
<br /><br />
|
||||||
|
<a th:href="@{/multipleHttpLinks}" >Back to links</a>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -46,9 +46,9 @@ public class MultipleEntryPointsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenTestUserCredentials_thenOk() throws Exception {
|
public void whenTestUserCredentials_thenOk() throws Exception {
|
||||||
mockMvc.perform(get("/user/myUserPage")).andExpect(status().isFound());
|
mockMvc.perform(get("/user/general/myUserPage")).andExpect(status().isFound());
|
||||||
|
|
||||||
mockMvc.perform(get("/user/myUserPage").with(user("user").password("userPass").roles("USER"))).andExpect(status().isOk());
|
mockMvc.perform(get("/user/general/myUserPage").with(user("user").password("userPass").roles("USER"))).andExpect(status().isOk());
|
||||||
|
|
||||||
mockMvc.perform(get("/admin/myAdminPage").with(user("user").password("userPass").roles("USER"))).andExpect(status().isForbidden());
|
mockMvc.perform(get("/admin/myAdminPage").with(user("user").password("userPass").roles("USER"))).andExpect(status().isForbidden());
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
package org.baeldung.persistence;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
|
|
||||||
|
public interface IEnhancedSpecification<T> extends Specification<T> {
|
||||||
|
|
||||||
|
default boolean isOfLowPrecedence() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
package org.baeldung.persistence.dao;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.baeldung.web.util.SearchOperation;
|
||||||
|
import org.baeldung.web.util.SpecSearchCriteria;
|
||||||
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
|
import org.springframework.data.jpa.domain.Specifications;
|
||||||
|
|
||||||
|
public class GenericSpecificationsBuilder {
|
||||||
|
|
||||||
|
private final List<SpecSearchCriteria> params;
|
||||||
|
|
||||||
|
public GenericSpecificationsBuilder() {
|
||||||
|
this.params = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public final GenericSpecificationsBuilder with(final String key, final String operation, final Object value,
|
||||||
|
final String prefix, final String suffix) {
|
||||||
|
return with(null, key, operation, value, prefix, suffix);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final GenericSpecificationsBuilder with(final String precedenceIndicator, final String key,
|
||||||
|
final String operation, final Object value, final String prefix, final String suffix) {
|
||||||
|
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||||
|
if (op != null) {
|
||||||
|
if (op == SearchOperation.EQUALITY) // the operation may be complex operation
|
||||||
|
{
|
||||||
|
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||||
|
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||||
|
|
||||||
|
if (startWithAsterisk && endWithAsterisk) {
|
||||||
|
op = SearchOperation.CONTAINS;
|
||||||
|
} else if (startWithAsterisk) {
|
||||||
|
op = SearchOperation.ENDS_WITH;
|
||||||
|
} else if (endWithAsterisk) {
|
||||||
|
op = SearchOperation.STARTS_WITH;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
params.add(new SpecSearchCriteria(precedenceIndicator, key, op, value));
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <U> Specification<U> build(Function<SpecSearchCriteria, Specification<U>> converter) {
|
||||||
|
|
||||||
|
if (params.size() == 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
params.sort((spec0, spec1) -> Boolean.compare(spec0.isLowPrecedence(), spec1.isLowPrecedence()));
|
||||||
|
|
||||||
|
final List<Specification<U>> specs = params.stream().map(converter).collect(Collectors.toCollection(ArrayList::new));
|
||||||
|
|
||||||
|
Specification<U> result = specs.get(0);
|
||||||
|
|
||||||
|
for (int idx = 1; idx < specs.size(); idx++) {
|
||||||
|
result=params.get(idx).isLowPrecedence()? Specifications.where(result).or(specs.get(idx)): Specifications.where(result).and(specs.get(idx));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -11,39 +11,39 @@ import org.springframework.data.jpa.domain.Specification;
|
|||||||
|
|
||||||
public class UserSpecification implements Specification<User> {
|
public class UserSpecification implements Specification<User> {
|
||||||
|
|
||||||
private SpecSearchCriteria criteria;
|
private SpecSearchCriteria criteria;
|
||||||
|
|
||||||
public UserSpecification(final SpecSearchCriteria criteria) {
|
public UserSpecification(final SpecSearchCriteria criteria) {
|
||||||
super();
|
super();
|
||||||
this.criteria = criteria;
|
this.criteria = criteria;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpecSearchCriteria getCriteria() {
|
public SpecSearchCriteria getCriteria() {
|
||||||
return criteria;
|
return criteria;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Predicate toPredicate(final Root<User> root, final CriteriaQuery<?> query, final CriteriaBuilder builder) {
|
public Predicate toPredicate(final Root<User> root, final CriteriaQuery<?> query, final CriteriaBuilder builder) {
|
||||||
switch (criteria.getOperation()) {
|
switch (criteria.getOperation()) {
|
||||||
case EQUALITY:
|
case EQUALITY:
|
||||||
return builder.equal(root.get(criteria.getKey()), criteria.getValue());
|
return builder.equal(root.get(criteria.getKey()), criteria.getValue());
|
||||||
case NEGATION:
|
case NEGATION:
|
||||||
return builder.notEqual(root.get(criteria.getKey()), criteria.getValue());
|
return builder.notEqual(root.get(criteria.getKey()), criteria.getValue());
|
||||||
case GREATER_THAN:
|
case GREATER_THAN:
|
||||||
return builder.greaterThan(root.<String> get(criteria.getKey()), criteria.getValue().toString());
|
return builder.greaterThan(root.<String> get(criteria.getKey()), criteria.getValue().toString());
|
||||||
case LESS_THAN:
|
case LESS_THAN:
|
||||||
return builder.lessThan(root.<String> get(criteria.getKey()), criteria.getValue().toString());
|
return builder.lessThan(root.<String> get(criteria.getKey()), criteria.getValue().toString());
|
||||||
case LIKE:
|
case LIKE:
|
||||||
return builder.like(root.<String> get(criteria.getKey()), criteria.getValue().toString());
|
return builder.like(root.<String> get(criteria.getKey()), criteria.getValue().toString());
|
||||||
case STARTS_WITH:
|
case STARTS_WITH:
|
||||||
return builder.like(root.<String> get(criteria.getKey()), criteria.getValue() + "%");
|
return builder.like(root.<String> get(criteria.getKey()), criteria.getValue() + "%");
|
||||||
case ENDS_WITH:
|
case ENDS_WITH:
|
||||||
return builder.like(root.<String> get(criteria.getKey()), "%" + criteria.getValue());
|
return builder.like(root.<String> get(criteria.getKey()), "%" + criteria.getValue());
|
||||||
case CONTAINS:
|
case CONTAINS:
|
||||||
return builder.like(root.<String> get(criteria.getKey()), "%" + criteria.getValue() + "%");
|
return builder.like(root.<String> get(criteria.getKey()), "%" + criteria.getValue() + "%");
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
package org.baeldung.persistence.dao;
|
package org.baeldung.persistence.dao;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.baeldung.persistence.model.User;
|
import org.baeldung.persistence.model.User;
|
||||||
import org.baeldung.web.util.SearchOperation;
|
import org.baeldung.web.util.SearchOperation;
|
||||||
import org.baeldung.web.util.SpecSearchCriteria;
|
import org.baeldung.web.util.SpecSearchCriteria;
|
||||||
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
import org.springframework.data.jpa.domain.Specifications;
|
import org.springframework.data.jpa.domain.Specifications;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public final class UserSpecificationsBuilder {
|
public final class UserSpecificationsBuilder {
|
||||||
|
|
||||||
private final List<SpecSearchCriteria> params;
|
private final List<SpecSearchCriteria> params;
|
||||||
@ -20,12 +20,15 @@ public final class UserSpecificationsBuilder {
|
|||||||
// API
|
// API
|
||||||
|
|
||||||
public final UserSpecificationsBuilder with(final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
public final UserSpecificationsBuilder with(final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||||
|
return with(null, key, operation, value, prefix, suffix);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final UserSpecificationsBuilder with(final String precedenceIndicator, final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||||
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||||
if (op != null) {
|
if (op != null) {
|
||||||
if (op == SearchOperation.EQUALITY) // the operation may be complex operation
|
if (op == SearchOperation.EQUALITY) { // the operation may be complex operation
|
||||||
{
|
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||||
final boolean startWithAsterisk = prefix.contains("*");
|
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||||
final boolean endWithAsterisk = suffix.contains("*");
|
|
||||||
|
|
||||||
if (startWithAsterisk && endWithAsterisk) {
|
if (startWithAsterisk && endWithAsterisk) {
|
||||||
op = SearchOperation.CONTAINS;
|
op = SearchOperation.CONTAINS;
|
||||||
@ -35,26 +38,37 @@ public final class UserSpecificationsBuilder {
|
|||||||
op = SearchOperation.STARTS_WITH;
|
op = SearchOperation.STARTS_WITH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
params.add(new SpecSearchCriteria(key, op, value));
|
params.add(new SpecSearchCriteria(precedenceIndicator, key, op, value));
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Specification<User> build() {
|
public Specification<User> build() {
|
||||||
if (params.size() == 0) {
|
|
||||||
|
if (params.size() == 0)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
params.sort((spec0, spec1) -> {
|
||||||
|
return Boolean.compare(spec0.isLowPrecedence(), spec1.isLowPrecedence());
|
||||||
|
});
|
||||||
|
|
||||||
|
Specification<User> result = new UserSpecification(params.get(0));
|
||||||
|
|
||||||
|
for (int i = 1; i < params.size(); i++) {
|
||||||
|
result = params.get(i).isLowPrecedence() ? Specifications.where(result).or(new UserSpecification(params.get(i))) : Specifications.where(result).and(new UserSpecification(params.get(i)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final List<Specification<User>> specs = new ArrayList<Specification<User>>();
|
|
||||||
for (final SpecSearchCriteria param : params) {
|
|
||||||
specs.add(new UserSpecification(param));
|
|
||||||
}
|
|
||||||
|
|
||||||
Specification<User> result = specs.get(0);
|
|
||||||
for (int i = 1; i < specs.size(); i++) {
|
|
||||||
result = Specifications.where(result).and(specs.get(i));
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final UserSpecificationsBuilder with(UserSpecification spec) {
|
||||||
|
params.add(spec.getCriteria());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final UserSpecificationsBuilder with(SpecSearchCriteria criteria) {
|
||||||
|
params.add(criteria);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,12 @@
|
|||||||
package org.baeldung.web.controller;
|
package org.baeldung.web.controller;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import com.google.common.base.Joiner;
|
||||||
import java.util.List;
|
import com.google.common.base.Preconditions;
|
||||||
import java.util.regex.Matcher;
|
import com.querydsl.core.types.Predicate;
|
||||||
import java.util.regex.Pattern;
|
import com.querydsl.core.types.dsl.BooleanExpression;
|
||||||
|
import cz.jirutka.rsql.parser.RSQLParser;
|
||||||
import org.baeldung.persistence.dao.IUserDAO;
|
import cz.jirutka.rsql.parser.ast.Node;
|
||||||
import org.baeldung.persistence.dao.MyUserPredicatesBuilder;
|
import org.baeldung.persistence.dao.*;
|
||||||
import org.baeldung.persistence.dao.MyUserRepository;
|
|
||||||
import org.baeldung.persistence.dao.UserRepository;
|
|
||||||
import org.baeldung.persistence.dao.UserSpecificationsBuilder;
|
|
||||||
import org.baeldung.persistence.dao.rsql.CustomRsqlVisitor;
|
import org.baeldung.persistence.dao.rsql.CustomRsqlVisitor;
|
||||||
import org.baeldung.persistence.model.MyUser;
|
import org.baeldung.persistence.model.MyUser;
|
||||||
import org.baeldung.persistence.model.User;
|
import org.baeldung.persistence.model.User;
|
||||||
@ -20,20 +17,12 @@ import org.springframework.data.jpa.domain.Specification;
|
|||||||
import org.springframework.data.querydsl.binding.QuerydslPredicate;
|
import org.springframework.data.querydsl.binding.QuerydslPredicate;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
||||||
|
|
||||||
import com.google.common.base.Joiner;
|
import java.util.ArrayList;
|
||||||
import com.google.common.base.Preconditions;
|
import java.util.List;
|
||||||
import com.querydsl.core.types.Predicate;
|
import java.util.regex.Matcher;
|
||||||
import com.querydsl.core.types.dsl.BooleanExpression;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import cz.jirutka.rsql.parser.RSQLParser;
|
|
||||||
import cz.jirutka.rsql.parser.ast.Node;
|
|
||||||
|
|
||||||
//@EnableSpringDataWebSupport
|
//@EnableSpringDataWebSupport
|
||||||
@Controller
|
@Controller
|
||||||
@ -84,6 +73,25 @@ public class UserController {
|
|||||||
return dao.findAll(spec);
|
return dao.findAll(spec);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.GET, value = "/users/espec")
|
||||||
|
@ResponseBody
|
||||||
|
public List<User> findAllByOptionalSpecification(@RequestParam(value = "search") final String search) {
|
||||||
|
final Specification<User> spec = resolveSpecification(search);
|
||||||
|
return dao.findAll(spec);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Specification<User> resolveSpecification(String searchParameters) {
|
||||||
|
|
||||||
|
final UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||||
|
final String operationSetExper = Joiner.on("|").join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||||
|
final Pattern pattern = Pattern.compile("(\\p{Punct}?)(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||||
|
final Matcher matcher = pattern.matcher(searchParameters + ",");
|
||||||
|
while (matcher.find()) {
|
||||||
|
builder.with(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(5), matcher.group(4), matcher.group(6));
|
||||||
|
}
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") final String search) {
|
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") final String search) {
|
||||||
|
@ -5,6 +5,10 @@ public enum SearchOperation {
|
|||||||
|
|
||||||
public static final String[] SIMPLE_OPERATION_SET = { ":", "!", ">", "<", "~" };
|
public static final String[] SIMPLE_OPERATION_SET = { ":", "!", ">", "<", "~" };
|
||||||
|
|
||||||
|
public static final String LOW_PRECEDENCE_INDICATOR="'";
|
||||||
|
|
||||||
|
public static final String ZERO_OR_MORE_REGEX="*";
|
||||||
|
|
||||||
public static SearchOperation getSimpleOperation(final char input) {
|
public static SearchOperation getSimpleOperation(final char input) {
|
||||||
switch (input) {
|
switch (input) {
|
||||||
case ':':
|
case ':':
|
||||||
|
@ -5,6 +5,7 @@ public class SpecSearchCriteria {
|
|||||||
private String key;
|
private String key;
|
||||||
private SearchOperation operation;
|
private SearchOperation operation;
|
||||||
private Object value;
|
private Object value;
|
||||||
|
private boolean lowPrecedence;
|
||||||
|
|
||||||
public SpecSearchCriteria() {
|
public SpecSearchCriteria() {
|
||||||
|
|
||||||
@ -17,6 +18,14 @@ public class SpecSearchCriteria {
|
|||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SpecSearchCriteria(final String lowPrecedenceIndicator, final String key, final SearchOperation operation, final Object value) {
|
||||||
|
super();
|
||||||
|
this.lowPrecedence = lowPrecedenceIndicator != null && lowPrecedenceIndicator.equals(SearchOperation.LOW_PRECEDENCE_INDICATOR);
|
||||||
|
this.key = key;
|
||||||
|
this.operation = operation;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
public String getKey() {
|
public String getKey() {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
@ -41,4 +50,12 @@ public class SpecSearchCriteria {
|
|||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isLowPrecedence() {
|
||||||
|
return lowPrecedence;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLowPrecedence(boolean lowPrecedence) {
|
||||||
|
this.lowPrecedence = lowPrecedence;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,9 @@
|
|||||||
package org.baeldung.persistence.query;
|
package org.baeldung.persistence.query;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import org.baeldung.persistence.dao.GenericSpecificationsBuilder;
|
||||||
import static org.hamcrest.collection.IsIn.isIn;
|
|
||||||
import static org.hamcrest.core.IsNot.not;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.baeldung.persistence.dao.UserRepository;
|
import org.baeldung.persistence.dao.UserRepository;
|
||||||
import org.baeldung.persistence.dao.UserSpecification;
|
import org.baeldung.persistence.dao.UserSpecification;
|
||||||
|
import org.baeldung.persistence.dao.UserSpecificationsBuilder;
|
||||||
import org.baeldung.persistence.model.User;
|
import org.baeldung.persistence.model.User;
|
||||||
import org.baeldung.spring.PersistenceConfig;
|
import org.baeldung.spring.PersistenceConfig;
|
||||||
import org.baeldung.web.util.SearchOperation;
|
import org.baeldung.web.util.SearchOperation;
|
||||||
@ -16,12 +12,21 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
import org.springframework.data.jpa.domain.Specifications;
|
import org.springframework.data.jpa.domain.Specifications;
|
||||||
import org.springframework.test.annotation.Rollback;
|
import org.springframework.test.annotation.Rollback;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
|
||||||
|
import static org.hamcrest.collection.IsIn.isIn;
|
||||||
|
import static org.hamcrest.core.IsNot.not;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = { PersistenceConfig.class })
|
@ContextConfiguration(classes = { PersistenceConfig.class })
|
||||||
@Transactional
|
@Transactional
|
||||||
@ -35,6 +40,8 @@ public class JPASpecificationIntegrationTest {
|
|||||||
|
|
||||||
private User userTom;
|
private User userTom;
|
||||||
|
|
||||||
|
private User userPercy;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
userJohn = new User();
|
userJohn = new User();
|
||||||
@ -50,6 +57,13 @@ public class JPASpecificationIntegrationTest {
|
|||||||
userTom.setEmail("tom@doe.com");
|
userTom.setEmail("tom@doe.com");
|
||||||
userTom.setAge(26);
|
userTom.setAge(26);
|
||||||
repository.save(userTom);
|
repository.save(userTom);
|
||||||
|
|
||||||
|
userPercy = new User();
|
||||||
|
userPercy.setFirstName("percy");
|
||||||
|
userPercy.setLastName("blackney");
|
||||||
|
userPercy.setEmail("percy@blackney.com");
|
||||||
|
userPercy.setAge(30);
|
||||||
|
repository.save(userPercy);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -62,6 +76,33 @@ public class JPASpecificationIntegrationTest {
|
|||||||
assertThat(userTom, not(isIn(results)));
|
assertThat(userTom, not(isIn(results)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFirstOrLastName_whenGettingListOfUsers_thenCorrect() {
|
||||||
|
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||||
|
|
||||||
|
final SpecSearchCriteria spec = new SpecSearchCriteria("'", "firstName", SearchOperation.EQUALITY, "john");
|
||||||
|
final SpecSearchCriteria spec1 = new SpecSearchCriteria("lastName", SearchOperation.EQUALITY, "doe");
|
||||||
|
|
||||||
|
final List<User> results = repository.findAll(builder.with(spec1).with(spec).build());
|
||||||
|
|
||||||
|
assertThat(results, hasSize(2));
|
||||||
|
assertThat(userJohn, isIn(results));
|
||||||
|
assertThat(userTom, isIn(results));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFirstOrLastNameGenericBuilder_whenGettingListOfUsers_thenCorrect() {
|
||||||
|
GenericSpecificationsBuilder builder = new GenericSpecificationsBuilder();
|
||||||
|
Function<SpecSearchCriteria, Specification<User>> converter = UserSpecification::new;
|
||||||
|
builder.with("'", "firstName", ":", "john", null, null);
|
||||||
|
builder.with(null, "lastName", ":", "doe", null, null);
|
||||||
|
|
||||||
|
final List<User> results = repository.findAll(builder.build(converter));
|
||||||
|
assertThat(results, hasSize(2));
|
||||||
|
assertThat(userJohn, isIn(results));
|
||||||
|
assertThat(userTom, isIn(results));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstNameInverse_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstNameInverse_whenGettingListOfUsers_thenCorrect() {
|
||||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.NEGATION, "john"));
|
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.NEGATION, "john"));
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
package org.baeldung.persistence.query;
|
package org.baeldung.persistence.query;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import com.jayway.restassured.RestAssured;
|
||||||
import static org.junit.Assert.assertTrue;
|
import com.jayway.restassured.response.Response;
|
||||||
|
import com.jayway.restassured.specification.RequestSpecification;
|
||||||
import org.baeldung.persistence.model.User;
|
import org.baeldung.persistence.model.User;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
|
||||||
import com.jayway.restassured.RestAssured;
|
import static org.junit.Assert.assertFalse;
|
||||||
import com.jayway.restassured.response.Response;
|
import static org.junit.Assert.assertTrue;
|
||||||
import com.jayway.restassured.specification.RequestSpecification;
|
|
||||||
|
|
||||||
//@RunWith(SpringJUnit4ClassRunner.class)
|
//@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
//@ContextConfiguration(classes = { ConfigTest.class, PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
//@ContextConfiguration(classes = { ConfigTest.class,
|
||||||
|
// PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
@ActiveProfiles("test")
|
@ActiveProfiles("test")
|
||||||
public class JPASpecificationLiveTest {
|
public class JPASpecificationLiveTest {
|
||||||
|
|
||||||
@ -43,6 +43,16 @@ public class JPASpecificationLiveTest {
|
|||||||
// repository.save(userTom);
|
// repository.save(userTom);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final String EURL_PREFIX = "http://localhost:8082/spring-security-rest-full/auth/users/espec?search=";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFirstOrLastName_whenGettingListOfUsers_thenCorrect() {
|
||||||
|
final Response response = givenAuth().get(EURL_PREFIX + "'firstName:john,lastName:doe");
|
||||||
|
final String result = response.body().asString();
|
||||||
|
assertTrue(result.contains(userJohn.getEmail()));
|
||||||
|
assertTrue(result.contains(userTom.getEmail()));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
||||||
final Response response = givenAuth().get(URL_PREFIX + "firstName:john,lastName:doe");
|
final Response response = givenAuth().get(URL_PREFIX + "firstName:john,lastName:doe");
|
||||||
|
@ -9,23 +9,14 @@ package com.baeldung.xml.jibx;
|
|||||||
import org.apache.commons.lang.builder.ToStringBuilder;
|
import org.apache.commons.lang.builder.ToStringBuilder;
|
||||||
|
|
||||||
public class Person extends Identity {
|
public class Person extends Identity {
|
||||||
private String firstName;
|
private String name;
|
||||||
private String lastName;
|
|
||||||
|
|
||||||
public String getFirstName() {
|
public String getName() {
|
||||||
return firstName;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFirstName(String firstName) {
|
public void setName(String name) {
|
||||||
this.firstName = firstName;
|
this.name = name;
|
||||||
}
|
|
||||||
|
|
||||||
public String getLastName() {
|
|
||||||
return lastName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLastName(String lastName) {
|
|
||||||
this.lastName = lastName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -2,26 +2,8 @@ package com.baeldung.xml.jibx;
|
|||||||
|
|
||||||
public class Phone {
|
public class Phone {
|
||||||
|
|
||||||
private String countryCode;
|
|
||||||
private String networkPrefix;
|
|
||||||
private String number;
|
private String number;
|
||||||
|
|
||||||
public String getCountryCode() {
|
|
||||||
return countryCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCountryCode(String countryCode) {
|
|
||||||
this.countryCode = countryCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getNetworkPrefix() {
|
|
||||||
return networkPrefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setNetworkPrefix(String networkPrefix) {
|
|
||||||
this.networkPrefix = networkPrefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getNumber() {
|
public String getNumber() {
|
||||||
return number;
|
return number;
|
||||||
}
|
}
|
||||||
|
@ -13,29 +13,6 @@
|
|||||||
<xs:attribute type="xs:string" name="Type" use="optional" />
|
<xs:attribute type="xs:string" name="Type" use="optional" />
|
||||||
</xs:complexType>
|
</xs:complexType>
|
||||||
</xs:element>
|
</xs:element>
|
||||||
<xs:element type="xs:string" name="DeliveryNotes" />
|
|
||||||
<xs:element name="Items">
|
|
||||||
<xs:complexType>
|
|
||||||
<xs:sequence>
|
|
||||||
<xs:element name="Item" maxOccurs="unbounded"
|
|
||||||
minOccurs="0">
|
|
||||||
<xs:complexType>
|
|
||||||
<xs:sequence>
|
|
||||||
<xs:element type="xs:string" name="ProductName" />
|
|
||||||
<xs:element type="xs:byte" name="Quantity" />
|
|
||||||
<xs:element type="xs:float" name="USPrice" />
|
|
||||||
<xs:element type="xs:string" name="Comment"
|
|
||||||
minOccurs="0" />
|
|
||||||
<xs:element type="xs:date" name="ShipDate"
|
|
||||||
minOccurs="0" />
|
|
||||||
</xs:sequence>
|
|
||||||
<xs:attribute type="xs:string" name="PartNumber"
|
|
||||||
use="optional" />
|
|
||||||
</xs:complexType>
|
|
||||||
</xs:element>
|
|
||||||
</xs:sequence>
|
|
||||||
</xs:complexType>
|
|
||||||
</xs:element>
|
|
||||||
</xs:sequence>
|
</xs:sequence>
|
||||||
<xs:attribute type="xs:int" name="OrderNumber" />
|
<xs:attribute type="xs:int" name="OrderNumber" />
|
||||||
<xs:attribute type="xs:date" name="OrderDate" />
|
<xs:attribute type="xs:date" name="OrderDate" />
|
||||||
|
@ -14,14 +14,11 @@
|
|||||||
<mapping name="person" class="com.baeldung.xml.jibx.Person"
|
<mapping name="person" class="com.baeldung.xml.jibx.Person"
|
||||||
extends="com.baeldung.xml.jibx.Identity">
|
extends="com.baeldung.xml.jibx.Identity">
|
||||||
<structure map-as="com.baeldung.xml.jibx.Identity" />
|
<structure map-as="com.baeldung.xml.jibx.Identity" />
|
||||||
<value name="first-name" field="firstName" />
|
<value name="name" field="name" />
|
||||||
<value name="last-name" field="lastName" />
|
|
||||||
|
|
||||||
</mapping>
|
</mapping>
|
||||||
|
|
||||||
<mapping class="com.baeldung.xml.jibx.Phone" abstract="true">
|
<mapping class="com.baeldung.xml.jibx.Phone" abstract="true">
|
||||||
<value name="country-code" field="countryCode" />
|
|
||||||
<value name="network-prefix" field="networkPrefix" />
|
|
||||||
<value name="number" field="number" />
|
<value name="number" field="number" />
|
||||||
</mapping>
|
</mapping>
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ import org.jibx.runtime.IUnmarshallingContext;
|
|||||||
import org.jibx.runtime.JiBXException;
|
import org.jibx.runtime.JiBXException;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
@ -22,8 +21,7 @@ public class CustomerTest {
|
|||||||
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
|
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
|
||||||
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
|
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
|
||||||
|
|
||||||
assertEquals("Stefan", customer.getPerson().getFirstName());
|
assertEquals("Stefan Jaegar", customer.getPerson().getName());
|
||||||
assertEquals("Jaeger", customer.getPerson().getLastName());
|
|
||||||
assertEquals("Davos Dorf", customer.getCity());
|
assertEquals("Davos Dorf", customer.getCity());
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -48,8 +46,6 @@ public class CustomerTest {
|
|||||||
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
|
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
|
||||||
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
|
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
|
||||||
|
|
||||||
assertEquals("1", customer.getHomePhone().getCountryCode());
|
|
||||||
assertEquals("234", customer.getHomePhone().getNetworkPrefix());
|
|
||||||
assertEquals("234678", customer.getHomePhone().getNumber());
|
assertEquals("234678", customer.getHomePhone().getNumber());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,18 +2,13 @@
|
|||||||
<customer>
|
<customer>
|
||||||
<person>
|
<person>
|
||||||
<customer-id>12345</customer-id>
|
<customer-id>12345</customer-id>
|
||||||
<first-name>Stefan</first-name>
|
<name>Stefan Jaeger</name>
|
||||||
<last-name>Jaeger</last-name>
|
|
||||||
</person>
|
</person>
|
||||||
<home-phone>
|
<home-phone>
|
||||||
<country-code>1</country-code>
|
|
||||||
<network-prefix>234</network-prefix>
|
|
||||||
<number>234678</number>
|
<number>234678</number>
|
||||||
</home-phone>
|
</home-phone>
|
||||||
|
|
||||||
<office-phone>
|
<office-phone>
|
||||||
<country-code>1</country-code>
|
|
||||||
<network-prefix>234</network-prefix>
|
|
||||||
<number>234678</number>
|
<number>234678</number>
|
||||||
</office-phone>
|
</office-phone>
|
||||||
<city>Davos Dorf</city>
|
<city>Davos Dorf</city>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user