Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
0266120fd3
|
@ -23,6 +23,12 @@
|
|||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>${groovy-all.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-dateutil</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
|
@ -103,9 +109,12 @@
|
|||
|
||||
<properties>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<groovy.version>2.4.13</groovy.version>
|
||||
<groovy-all.version>2.4.13</groovy-all.version>
|
||||
<groovy-sql.version>2.4.13</groovy-sql.version>
|
||||
<!-- <groovy.version>2.4.13</groovy.version> -->
|
||||
<!-- <groovy-all.version>2.4.13</groovy-all.version> -->
|
||||
<!-- <groovy-sql.version>2.4.13</groovy-sql.version> -->
|
||||
<groovy.version>2.5.6</groovy.version>
|
||||
<groovy-all.version>2.5.6</groovy-all.version>
|
||||
<groovy-sql.version>2.5.6</groovy-sql.version>
|
||||
<hsqldb.version>2.4.0</hsqldb.version>
|
||||
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
||||
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.groovy.sql
|
||||
|
||||
import static org.junit.Assert.*
|
||||
import java.util.Calendar.*
|
||||
import java.time.LocalDate
|
||||
import java.text.SimpleDateFormat
|
||||
import org.junit.Test
|
||||
|
||||
|
||||
class DateTest {
|
||||
|
||||
def dateStr = "2019-02-28"
|
||||
def pattern = "yyyy-MM-dd"
|
||||
|
||||
@Test
|
||||
void whenGetStringRepresentation_thenCorrectlyConvertIntoDate() {
|
||||
def dateFormat = new SimpleDateFormat(pattern)
|
||||
def date = dateFormat.parse(dateStr)
|
||||
|
||||
println(" String to Date with DateFormatter : " + date)
|
||||
|
||||
def cal = new GregorianCalendar();
|
||||
cal.setTime(date);
|
||||
|
||||
assertEquals(cal.get(Calendar.YEAR),2019)
|
||||
assertEquals(cal.get(Calendar.DAY_OF_MONTH),28)
|
||||
assertEquals(cal.get(Calendar.MONTH),java.util.Calendar.FEBRUARY)
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGetStringRepresentation_thenCorrectlyConvertWithDateUtilsExtension() {
|
||||
|
||||
def date = Date.parse(pattern, dateStr)
|
||||
|
||||
println(" String to Date with Date.parse : " + date)
|
||||
|
||||
def cal = new GregorianCalendar();
|
||||
cal.setTime(date);
|
||||
|
||||
assertEquals(cal.get(Calendar.YEAR),2019)
|
||||
assertEquals(cal.get(Calendar.DAY_OF_MONTH),28)
|
||||
assertEquals(cal.get(Calendar.MONTH),java.util.Calendar.FEBRUARY)
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGetStringRepresentation_thenCorrectlyConvertIntoDateWithLocalDate() {
|
||||
def date = LocalDate.parse(dateStr, pattern)
|
||||
|
||||
println(" String to Date with LocalDate : " + date)
|
||||
|
||||
assertEquals(date.getYear(),2019)
|
||||
assertEquals(date.getMonth(),java.time.Month.FEBRUARY)
|
||||
assertEquals(date.getDayOfMonth(),28)
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -13,4 +13,5 @@
|
|||
- [Working with Network Interfaces in Java](http://www.baeldung.com/java-network-interfaces)
|
||||
- [A Guide to Java Sockets](http://www.baeldung.com/a-guide-to-java-sockets)
|
||||
- [URL Encoding and Decoding in Java](http://www.baeldung.com/java-url-encoding-decoding)
|
||||
- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
|
||||
- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
|
||||
- [Difference between URL and URI](http://www.baeldung.com/java-url-vs-uri)
|
||||
|
|
|
@ -19,7 +19,6 @@
|
|||
- [Introduction to Java Serialization](http://www.baeldung.com/java-serialization)
|
||||
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
|
||||
- [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char)
|
||||
- [Difference between URL and URI](http://www.baeldung.com/java-url-vs-uri)
|
||||
- [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin)
|
||||
- [Quick Guide to Java Stack](http://www.baeldung.com/java-stack)
|
||||
- [Compiling Java *.class Files with javac](http://www.baeldung.com/javac)
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.urlconnection;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class PostJSONWithHttpURLConnection {
|
||||
|
||||
public static void main (String []args) throws IOException{
|
||||
//Change the URL with any other publicly accessible POST resource, which accepts JSON request body
|
||||
URL url = new URL ("https://reqres.in/api/users");
|
||||
|
||||
HttpURLConnection con = (HttpURLConnection)url.openConnection();
|
||||
con.setRequestMethod("POST");
|
||||
|
||||
con.setRequestProperty("Content-Type", "application/json; utf-8");
|
||||
con.setRequestProperty("Accept", "application/json");
|
||||
|
||||
con.setDoOutput(true);
|
||||
|
||||
//JSON String need to be constructed for the specific resource.
|
||||
//We may construct complex JSON using any third-party JSON libraries such as jackson or org.json
|
||||
String jsonInputString = "{\"name\": \"Upendra\", \"job\": \"Programmer\"}";
|
||||
|
||||
try(OutputStream os = con.getOutputStream()){
|
||||
byte[] input = jsonInputString.getBytes("utf-8");
|
||||
os.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
int code = con.getResponseCode();
|
||||
System.out.println(code);
|
||||
|
||||
try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))){
|
||||
StringBuilder response = new StringBuilder();
|
||||
String responseLine = null;
|
||||
while ((responseLine = br.readLine()) != null) {
|
||||
response.append(responseLine.trim());
|
||||
}
|
||||
System.out.println(response.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package stringcomparison
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class StringComparisonUnitTest {
|
||||
|
||||
@Test
|
||||
fun `compare using equals operator`() {
|
||||
val first = "kotlin"
|
||||
val second = "kotlin"
|
||||
val firstCapitalized = "KOTLIN"
|
||||
assertTrue { first == second }
|
||||
assertFalse { first == firstCapitalized }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compare using referential equals operator`() {
|
||||
val first = "kotlin"
|
||||
val second = "kotlin"
|
||||
val copyOfFirst = buildString { "kotlin" }
|
||||
assertTrue { first === second }
|
||||
assertFalse { first === copyOfFirst }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compare using equals method`() {
|
||||
val first = "kotlin"
|
||||
val second = "kotlin"
|
||||
val firstCapitalized = "KOTLIN"
|
||||
assertTrue { first.equals(second) }
|
||||
assertFalse { first.equals(firstCapitalized) }
|
||||
assertTrue { first.equals(firstCapitalized, true) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compare using compare method`() {
|
||||
val first = "kotlin"
|
||||
val second = "kotlin"
|
||||
val firstCapitalized = "KOTLIN"
|
||||
assertTrue { first.compareTo(second) == 0 }
|
||||
assertTrue { first.compareTo(firstCapitalized) == 32 }
|
||||
assertTrue { firstCapitalized.compareTo(first) == -32 }
|
||||
assertTrue { first.compareTo(firstCapitalized, true) == 0 }
|
||||
}
|
||||
}
|
|
@ -33,24 +33,11 @@
|
|||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>maven2-repository.dev.java.net</id>
|
||||
<name>Java.net repository</name>
|
||||
<url>http://download.java.net/maven/2</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>osgeo</id>
|
||||
<name>Open Source Geospatial Foundation Repository</name>
|
||||
<url>http://download.osgeo.org/webdav/geotools/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<id>opengeo</id>
|
||||
<name>OpenGeo Maven Repository</name>
|
||||
<url>http://repo.opengeo.org</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
package com.baeldung.reduce.application;
|
||||
|
||||
import com.baeldung.reduce.entities.User;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||
int result1 = numbers.stream().reduce(0, (subtotal, element) -> subtotal + element);
|
||||
System.out.println(result1);
|
||||
|
||||
int result2 = numbers.stream().reduce(0, Integer::sum);
|
||||
System.out.println(result2);
|
||||
|
||||
List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
|
||||
String result3 = letters.stream().reduce("", (partialString, element) -> partialString + element);
|
||||
System.out.println(result3);
|
||||
|
||||
String result4 = letters.stream().reduce("", String::concat);
|
||||
System.out.println(result4);
|
||||
|
||||
String result5 = letters.stream().reduce("", (partialString, element) -> partialString.toUpperCase() + element.toUpperCase());
|
||||
System.out.println(result5);
|
||||
|
||||
List<User> users = Arrays.asList(new User("John", 30), new User("Julie", 35));
|
||||
int result6 = users.stream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
|
||||
System.out.println(result6);
|
||||
|
||||
String result7 = letters.parallelStream().reduce("", String::concat);
|
||||
System.out.println(result7);
|
||||
|
||||
int result8 = users.parallelStream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
|
||||
System.out.println(result8);
|
||||
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@Fork(value = 1, warmups = 2)
|
||||
@Warmup(iterations = 2)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public void executeReduceOnParallelizedStream() {
|
||||
List<User> userList = new ArrayList<>();
|
||||
for (int i = 0; i <= 1000000; i++) {
|
||||
userList.add(new User("John" + i, i));
|
||||
}
|
||||
userList.parallelStream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@Fork(value = 1, warmups = 2)
|
||||
@Warmup(iterations = 2)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public void executeReduceOnSequentialStream() {
|
||||
List<User> userList = new ArrayList<>();
|
||||
for (int i = 0; i <= 1000000; i++) {
|
||||
userList.add(new User("John" + i, i));
|
||||
}
|
||||
userList.stream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.reduce.entities;
|
||||
|
||||
public class User {
|
||||
|
||||
private final String name;
|
||||
private final int age;
|
||||
|
||||
public User(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "name=" + name + ", age=" + age + '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.reduce.utilities;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public abstract class NumberUtils {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(NumberUtils.class.getName());
|
||||
|
||||
public static int divideListElements(List<Integer> values, Integer divider) {
|
||||
return values.stream()
|
||||
.reduce(0, (a, b) -> {
|
||||
try {
|
||||
return a / divider + b / divider;
|
||||
} catch (ArithmeticException e) {
|
||||
LOGGER.log(Level.INFO, "Arithmetic Exception: Division by Zero");
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
public static int divideListElementsWithExtractedTryCatchBlock(List<Integer> values, int divider) {
|
||||
return values.stream().reduce(0, (a, b) -> divide(a, divider) + divide(b, divider));
|
||||
}
|
||||
|
||||
public static int divideListElementsWithApplyFunctionMethod(List<Integer> values, int divider) {
|
||||
BiFunction<Integer, Integer, Integer> division = (a, b) -> a / b;
|
||||
return values.stream().reduce(0, (a, b) -> applyFunction(division, a, divider) + applyFunction(division, b, divider));
|
||||
}
|
||||
|
||||
private static int divide(int value, int factor) {
|
||||
int result = 0;
|
||||
try {
|
||||
result = value / factor;
|
||||
} catch (ArithmeticException e) {
|
||||
LOGGER.log(Level.INFO, "Arithmetic Exception: Division by Zero");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int applyFunction(BiFunction<Integer, Integer, Integer> function, int a, int b) {
|
||||
try {
|
||||
return function.apply(a, b);
|
||||
}
|
||||
catch(Exception e) {
|
||||
LOGGER.log(Level.INFO, "Exception occurred!");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
package com.baeldung.reduce.tests;
|
||||
|
||||
import com.baeldung.reduce.entities.User;
|
||||
import com.baeldung.reduce.utilities.NumberUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StreamReduceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenIntegerList_whenReduceWithSumAccumulatorLambda_thenCorrect() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||
int result = numbers.stream().reduce(0, (a, b) -> a + b);
|
||||
assertThat(result).isEqualTo(21);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntegerList_whenReduceWithSumAccumulatorMethodReference_thenCorrect() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||
int result = numbers.stream().reduce(0, Integer::sum);
|
||||
assertThat(result).isEqualTo(21);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringList_whenReduceWithConcatenatorAccumulatorLambda_thenCorrect() {
|
||||
List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
|
||||
String result = letters.stream().reduce("", (a, b) -> a + b);
|
||||
assertThat(result).isEqualTo("abcde");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringList_whenReduceWithConcatenatorAccumulatorMethodReference_thenCorrect() {
|
||||
List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
|
||||
String result = letters.stream().reduce("", String::concat);
|
||||
assertThat(result).isEqualTo("abcde");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringList_whenReduceWithUppercaseConcatenatorAccumulator_thenCorrect() {
|
||||
List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
|
||||
String result = letters.stream().reduce("", (a, b) -> a.toUpperCase() + b.toUpperCase());
|
||||
assertThat(result).isEqualTo("ABCDE");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserList_whenReduceWithAgeAccumulatorAndSumCombiner_thenCorrect() {
|
||||
List<User> users = Arrays.asList(new User("John", 30), new User("Julie", 35));
|
||||
int result = users.stream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
|
||||
assertThat(result).isEqualTo(65);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringList_whenReduceWithParallelStream_thenCorrect() {
|
||||
List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
|
||||
String result = letters.parallelStream().reduce("", String::concat);
|
||||
assertThat(result).isEqualTo("abcde");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNumberUtilsClass_whenCalledDivideListElements_thenCorrect() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||
assertThat(NumberUtils.divideListElements(numbers, 1)).isEqualTo(21);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNumberUtilsClass_whenCalledDivideListElementsWithExtractedTryCatchBlock_thenCorrect() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||
assertThat(NumberUtils.divideListElementsWithExtractedTryCatchBlock(numbers, 1)).isEqualTo(21);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStream_whneCalleddivideListElementsWithApplyFunctionMethod_thenCorrect() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||
assertThat(NumberUtils.divideListElementsWithApplyFunctionMethod(numbers, 1)).isEqualTo(21);
|
||||
}
|
||||
}
|
|
@ -2,9 +2,8 @@
|
|||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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.jhipster</groupId>
|
||||
<artifactId>jhipster-microservice-uaa</artifactId>
|
||||
<name>JHipster Microservice with UAA</name>
|
||||
<artifactId>jhipster-uaa</artifactId>
|
||||
<name>jhipster-uaa</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>lombok-custom</artifactId>
|
||||
<name>lombok-custom</name>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>custom-rule</artifactId>
|
||||
<name>custom-rule</name>
|
||||
|
||||
<properties>
|
||||
<api.version>3.0.0-M2</api.version>
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>maven-enforcer</artifactId>
|
||||
<name>maven-enforcer</name>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>osgi-intro-sample-activator</artifactId>
|
||||
<name>osgi-intro-sample-activator</name>
|
||||
<!-- Please, note this is not the usual 'jar'. -->
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<!-- mvn:com.baeldung/osgi-intro-sample-client/1.0-SNAPSHOT -->
|
||||
<artifactId>osgi-intro-sample-client</artifactId>
|
||||
<name>osgi-intro-sample-client</name>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>osgi-intro-sample-service</artifactId>
|
||||
<name>osgi-intro-sample-service</name>
|
||||
<!-- Please, note this is not the usual 'jar'. -->
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
|
|
|
@ -55,7 +55,7 @@
|
|||
|
||||
<properties>
|
||||
<rest-assured.version>3.1.0</rest-assured.version>
|
||||
<spring-boot.version>1.5.16.RELEASE</spring-boot.version>
|
||||
<spring-boot.version>1.5.19.RELEASE</spring-boot.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -77,9 +77,7 @@
|
|||
<properties>
|
||||
<rest-assured.version>3.1.0</rest-assured.version>
|
||||
<!-- plugins -->
|
||||
<thin.version>1.0.11.RELEASE</thin.version>
|
||||
<spring-boot.version>2.1.1.RELEASE</spring-boot.version>
|
||||
<thin.version>1.0.21.RELEASE</thin.version>
|
||||
<spring-boot.version>2.1.3.RELEASE</spring-boot.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
|
||||
|
|
|
@ -18,3 +18,4 @@
|
|||
- [Chain of Responsibility Design Pattern in Java](http://www.baeldung.com/chain-of-responsibility-pattern)
|
||||
- [The Command Pattern in Java](http://www.baeldung.com/java-command-pattern)
|
||||
- [Java Constructors vs Static Factory Methods](https://www.baeldung.com/java-constructors-vs-static-factory-methods)
|
||||
- [The Adapter Pattern in Java](https://www.baeldung.com/java-adapter-pattern)
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>solid</artifactId>
|
||||
<name>solid</name>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.baeldung.dao.repositories;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Item;
|
||||
|
@ -12,4 +14,8 @@ public interface CustomItemRepository {
|
|||
Item findItemById(Long id);
|
||||
|
||||
void findThenDelete(Long id);
|
||||
|
||||
List<Item> findItemsByColorAndGrade();
|
||||
|
||||
List<Item> findItemByColorOrGrade();
|
||||
}
|
||||
|
|
|
@ -1,12 +1,18 @@
|
|||
package com.baeldung.dao.repositories.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Item;
|
||||
import com.baeldung.dao.repositories.CustomItemRepository;
|
||||
import com.baeldung.domain.Item;
|
||||
|
||||
@Repository
|
||||
public class CustomItemRepositoryImpl implements CustomItemRepository {
|
||||
|
@ -29,4 +35,54 @@ public class CustomItemRepositoryImpl implements CustomItemRepository {
|
|||
final Item item = entityManager.find(Item.class, id);
|
||||
entityManager.remove(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Item> findItemsByColorAndGrade() {
|
||||
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Item> criteriaQuery = criteriaBuilder.createQuery(Item.class);
|
||||
Root<Item> itemRoot = criteriaQuery.from(Item.class);
|
||||
|
||||
Predicate predicateForBlueColor = criteriaBuilder.equal(itemRoot.get("color"), "blue");
|
||||
Predicate predicateForRedColor = criteriaBuilder.equal(itemRoot.get("color"), "red");
|
||||
Predicate predicateForColor = criteriaBuilder.or(predicateForBlueColor, predicateForRedColor);
|
||||
|
||||
Predicate predicateForGradeA = criteriaBuilder.equal(itemRoot.get("grade"), "A");
|
||||
Predicate predicateForGradeB = criteriaBuilder.equal(itemRoot.get("grade"), "B");
|
||||
Predicate predicateForGrade = criteriaBuilder.or(predicateForGradeA, predicateForGradeB);
|
||||
|
||||
// final search filter
|
||||
Predicate finalPredicate = criteriaBuilder.and(predicateForColor, predicateForGrade);
|
||||
|
||||
criteriaQuery.where(finalPredicate);
|
||||
|
||||
List<Item> items = entityManager.createQuery(criteriaQuery)
|
||||
.getResultList();
|
||||
return items;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Item> findItemByColorOrGrade() {
|
||||
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Item> criteriaQuery = criteriaBuilder.createQuery(Item.class);
|
||||
Root<Item> itemRoot = criteriaQuery.from(Item.class);
|
||||
|
||||
Predicate predicateForBlueColor = criteriaBuilder.equal(itemRoot.get("color"), "red");
|
||||
Predicate predicateForGradeA = criteriaBuilder.equal(itemRoot.get("grade"), "D");
|
||||
Predicate predicateForBlueColorAndGradeA = criteriaBuilder.and(predicateForBlueColor, predicateForGradeA);
|
||||
|
||||
Predicate predicateForRedColor = criteriaBuilder.equal(itemRoot.get("color"), "blue");
|
||||
Predicate predicateForGradeB = criteriaBuilder.equal(itemRoot.get("grade"), "B");
|
||||
Predicate predicateForRedColorAndGradeB = criteriaBuilder.and(predicateForRedColor, predicateForGradeB);
|
||||
|
||||
// final search filter
|
||||
Predicate finalPredicate = criteriaBuilder.or(predicateForBlueColorAndGradeA, predicateForRedColorAndGradeB);
|
||||
|
||||
criteriaQuery.where(finalPredicate);
|
||||
|
||||
List<Item> items = entityManager.createQuery(criteriaQuery)
|
||||
.getResultList();
|
||||
return items;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
package com.baeldung.dao.repositories.impl;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.config.PersistenceProductConfiguration;
|
||||
import com.baeldung.config.PersistenceUserConfiguration;
|
||||
import com.baeldung.dao.repositories.CustomItemRepository;
|
||||
import com.baeldung.domain.Item;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest(excludeAutoConfiguration = { PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class })
|
||||
public class CustomItemRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
CustomItemRepository customItemRepositoryImpl;
|
||||
|
||||
@Autowired
|
||||
EntityManager entityManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
Item firstItem = new Item();
|
||||
firstItem.setColor("blue");
|
||||
firstItem.setGrade("C");
|
||||
firstItem.setId(10l);
|
||||
|
||||
entityManager.persist(firstItem);
|
||||
|
||||
Item secondItem = new Item();
|
||||
secondItem.setColor("red");
|
||||
secondItem.setGrade("C");
|
||||
secondItem.setId(11l);
|
||||
|
||||
entityManager.persist(secondItem);
|
||||
|
||||
Item thirdItem = new Item();
|
||||
thirdItem.setColor("blue");
|
||||
thirdItem.setGrade("A");
|
||||
thirdItem.setId(12l);
|
||||
|
||||
entityManager.persist(thirdItem);
|
||||
|
||||
Item fourthItem = new Item();
|
||||
fourthItem.setColor("red");
|
||||
fourthItem.setGrade("D");
|
||||
fourthItem.setId(13l);
|
||||
|
||||
entityManager.persist(fourthItem);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenItems_whenFindItemsByColorAndGrade_thenReturnItems() {
|
||||
|
||||
List<Item> items = customItemRepositoryImpl.findItemsByColorAndGrade();
|
||||
|
||||
assertFalse("No items found", CollectionUtils.isEmpty(items));
|
||||
assertEquals("There should be only one item", 1, items.size());
|
||||
|
||||
Item item = items.get(0);
|
||||
|
||||
assertEquals("this item do not have blue color", "blue", item.getColor());
|
||||
assertEquals("this item does not belong to A grade", "A", item.getGrade());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenItems_whenFindItemByColorOrGrade_thenReturnItems() {
|
||||
|
||||
List<Item> items = customItemRepositoryImpl.findItemByColorOrGrade();
|
||||
|
||||
assertFalse("No items found", CollectionUtils.isEmpty(items));
|
||||
assertEquals("There should be only one item", 1, items.size());
|
||||
|
||||
Item item = items.get(0);
|
||||
|
||||
assertEquals("this item do not have red color", "red", item.getColor());
|
||||
assertEquals("this item does not belong to D grade", "D", item.getGrade());
|
||||
}
|
||||
|
||||
}
|
|
@ -15,7 +15,6 @@
|
|||
- [Self-Contained Testing Using an In-Memory Database](http://www.baeldung.com/spring-jpa-test-in-memory-database)
|
||||
- [A Guide to Spring AbstractRoutingDatasource](http://www.baeldung.com/spring-abstract-routing-data-source)
|
||||
- [A Guide to Hibernate with Spring 4](http://www.baeldung.com/the-persistence-layer-with-spring-and-jpa)
|
||||
- [Testing REST with multiple MIME types](http://www.baeldung.com/testing-rest-api-with-multiple-media-types)
|
||||
- [Obtaining Auto-generated Keys in Spring JDBC](http://www.baeldung.com/spring-jdbc-autogenerated-keys)
|
||||
- [Transactions with Spring 4 and JPA](http://www.baeldung.com/transaction-configuration-with-jpa-and-spring)
|
||||
- [Use Criteria Queries in a Spring Data Application](https://www.baeldung.com/spring-data-criteria-queries)
|
||||
|
|
8
pom.xml
8
pom.xml
|
@ -412,7 +412,7 @@
|
|||
<module>feign</module>
|
||||
<module>flyway-cdi-extension</module>
|
||||
|
||||
<!-- <module>geotools</module> --> <!-- Hangs the build. Fixing in BAEL-10943 -->
|
||||
<module>geotools</module>
|
||||
<module>google-cloud</module>
|
||||
<module>google-web-toolkit</module>
|
||||
<!-- <module>gradle</module> --> <!-- Not a maven project -->
|
||||
|
@ -542,7 +542,7 @@
|
|||
<module>persistence-modules/spring-data-couchbase-2</module>
|
||||
<module>persistence-modules/spring-data-dynamodb</module>
|
||||
<module>persistence-modules/spring-data-eclipselink</module>
|
||||
<!-- <module>persistence-modules/spring-data-elasticsearch</module> --> <!-- Fixing in BAEL-10995 -->
|
||||
<module>persistence-modules/spring-data-elasticsearch</module>
|
||||
<module>persistence-modules/spring-data-gemfire</module>
|
||||
<module>persistence-modules/spring-data-jpa</module>
|
||||
<module>persistence-modules/spring-data-keyvalue</module>
|
||||
|
@ -1130,7 +1130,7 @@
|
|||
<module>feign</module>
|
||||
<module>flyway-cdi-extension</module>
|
||||
|
||||
<!-- <module>geotools</module> --> <!-- Hangs the build. Fixing in BAEL-10943 -->
|
||||
<module>geotools</module>
|
||||
<module>google-cloud</module>
|
||||
<module>google-web-toolkit</module>
|
||||
<!-- <module>gradle</module> --> <!-- Not a maven project -->
|
||||
|
@ -1260,7 +1260,7 @@
|
|||
<module>persistence-modules/spring-data-couchbase-2</module>
|
||||
<module>persistence-modules/spring-data-dynamodb</module>
|
||||
<module>persistence-modules/spring-data-eclipselink</module>
|
||||
<!-- <module>persistence-modules/spring-data-elasticsearch</module> --> <!-- Fixing in BAEL-10995 -->
|
||||
<module>persistence-modules/spring-data-elasticsearch</module>
|
||||
<module>persistence-modules/spring-data-gemfire</module>
|
||||
<module>persistence-modules/spring-data-jpa</module>
|
||||
<module>persistence-modules/spring-data-keyvalue</module>
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>restx</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>restx-demo</name>
|
||||
<name>restx</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
|
@ -151,6 +151,6 @@
|
|||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<restx.version>0.35-rc4</restx.version>
|
||||
<logback-classic.version>1.2.3</logback-classic.version>
|
||||
<logback-classic.version>1.2.3</logback-classic.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>rxjava-2</artifactId>
|
||||
<name>rxjava-2</name>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.springbootangular</groupId>
|
||||
<artifactId>springbootangular</artifactId>
|
||||
<artifactId>spring-boot-angular</artifactId>
|
||||
<name>spring-boot-angular</name>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
<parent>
|
||||
|
|
|
@ -84,7 +84,7 @@
|
|||
<id>openshift</id>
|
||||
<properties>
|
||||
<spring-cloud-k8s.version>0.3.0.RELEASE</spring-cloud-k8s.version>
|
||||
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
|
||||
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
|
||||
<fabric8.maven.plugin.version>3.5.37</fabric8.maven.plugin.version>
|
||||
</properties>
|
||||
<dependencyManagement>
|
||||
|
@ -166,7 +166,7 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>Finchley.SR1</version>
|
||||
<version>Greenwich.RELEASE</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
@ -214,7 +214,7 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>Finchley.SR1</version>
|
||||
<version>Greenwich.RELEASE</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
|
|
@ -4,12 +4,10 @@ import org.springframework.boot.SpringApplication;
|
|||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
@ServletComponentScan
|
||||
@SpringBootApplication
|
||||
@ComponentScan("com.baeldung")
|
||||
@SpringBootApplication(scanBasePackages = "com.baeldung")
|
||||
@EnableJpaRepositories("com.baeldung.persistence.repo")
|
||||
@EntityScan("com.baeldung.persistence.model")
|
||||
public class Application {
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
<html lang="en">
|
||||
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<title>Error Occurred</title>
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>Error Occurred!</h1>
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<h1>Error Occurred!</h1>
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<b>
|
||||
[<span th:text="${status}">status</span>]
|
||||
<span th:text="${error}">error</span>
|
||||
</b>
|
||||
<p th:text="${message}">message</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</html>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<html>
|
||||
<html xmlns:th="http://www.w3.org/1999/xhtml" lang="en">
|
||||
<head><title>Home Page</title></head>
|
||||
<body>
|
||||
<h1>Hello !</h1>
|
||||
<p>Welcome to <span th:text="${appName}">Our App</span></p>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
|
|
@ -9,3 +9,4 @@ Module for the articles that are part of the Spring REST E-book:
|
|||
7. [Versioning a REST API](http://www.baeldung.com/rest-versioning)
|
||||
8. [Http Message Converters with the Spring Framework](http://www.baeldung.com/spring-httpmessageconverter-rest)
|
||||
9. [ETags for REST with Spring](http://www.baeldung.com/etags-for-rest-with-spring)
|
||||
10. [Testing REST with multiple MIME types](http://www.baeldung.com/testing-rest-api-with-multiple-media-types)
|
||||
|
|
|
@ -27,8 +27,7 @@ public class TestMarshallerFactory implements FactoryBean<IMarshaller> {
|
|||
case "json":
|
||||
return new JacksonMarshaller();
|
||||
case "xml":
|
||||
// If we need to implement xml marshaller we can include spring-rest-full XStreamMarshaller
|
||||
throw new IllegalStateException();
|
||||
return new XStreamMarshaller();
|
||||
default:
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package org.baeldung.test;
|
||||
package com.baeldung.test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
|
@ -60,22 +60,11 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>http://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud.version>Greenwich.M3</spring-cloud.version>
|
||||
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -52,22 +52,11 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>http://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud.version>Greenwich.M3</spring-cloud.version>
|
||||
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>liveness-example</artifactId>
|
||||
<name>liveness-example</name>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>readiness-example</artifactId>
|
||||
<name>readiness-example</name>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung.cloud</groupId>
|
||||
<artifactId>openfeign</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>openfeign</name>
|
||||
<description>OpenFeign project for Spring Boot</description>
|
||||
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<spring-boot.version>2.0.1.RELEASE</spring-boot.version>
|
||||
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-okhttp</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.cloud.openfeign;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
public class ExampleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ExampleApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.cloud.openfeign.client;
|
||||
|
||||
import com.baeldung.cloud.openfeign.config.ClientConfiguration;
|
||||
import com.baeldung.cloud.openfeign.hystrix.JSONPlaceHolderFallback;
|
||||
import com.baeldung.cloud.openfeign.model.Post;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@FeignClient(value = "jplaceholder",
|
||||
url = "https://jsonplaceholder.typicode.com/",
|
||||
configuration = ClientConfiguration.class,
|
||||
fallback = JSONPlaceHolderFallback.class)
|
||||
public interface JSONPlaceHolderClient {
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/posts")
|
||||
List<Post> getPosts();
|
||||
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/posts/{postId}", produces = "application/json")
|
||||
Post getPostById(@PathVariable("postId") Long postId);
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.cloud.openfeign.config;
|
||||
|
||||
import feign.Logger;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.codec.ErrorDecoder;
|
||||
import feign.okhttp.OkHttpClient;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ClientConfiguration {
|
||||
|
||||
@Bean
|
||||
public Logger.Level feignLoggerLevel() {
|
||||
return Logger.Level.FULL;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ErrorDecoder errorDecoder() {
|
||||
return new ErrorDecoder.Default();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OkHttpClient client() {
|
||||
return new OkHttpClient();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RequestInterceptor requestInterceptor() {
|
||||
return requestTemplate -> {
|
||||
requestTemplate.header("user", "ajeje");
|
||||
requestTemplate.header("password", "brazof");
|
||||
requestTemplate.header("Accept", ContentType.APPLICATION_JSON.getMimeType());
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.cloud.openfeign.config;
|
||||
|
||||
import com.baeldung.cloud.openfeign.exception.BadRequestException;
|
||||
import com.baeldung.cloud.openfeign.exception.NotFoundException;
|
||||
import feign.Response;
|
||||
import feign.codec.ErrorDecoder;
|
||||
|
||||
public class CustomErrorDecoder implements ErrorDecoder {
|
||||
@Override
|
||||
public Exception decode(String methodKey, Response response) {
|
||||
|
||||
switch (response.status()){
|
||||
case 400:
|
||||
return new BadRequestException();
|
||||
case 404:
|
||||
return new NotFoundException();
|
||||
default:
|
||||
return new Exception("Generic error");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.cloud.openfeign.exception;
|
||||
|
||||
public class BadRequestException extends Exception {
|
||||
|
||||
public BadRequestException() {
|
||||
}
|
||||
|
||||
public BadRequestException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public BadRequestException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BadRequestException: "+getMessage();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.cloud.openfeign.exception;
|
||||
|
||||
public class NotFoundException extends Exception {
|
||||
|
||||
public NotFoundException() {
|
||||
}
|
||||
|
||||
public NotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public NotFoundException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NotFoundException: "+getMessage();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.cloud.openfeign.hystrix;
|
||||
|
||||
import com.baeldung.cloud.openfeign.client.JSONPlaceHolderClient;
|
||||
import com.baeldung.cloud.openfeign.model.Post;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class JSONPlaceHolderFallback implements JSONPlaceHolderClient {
|
||||
|
||||
@Override
|
||||
public List<Post> getPosts() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post getPostById(Long postId) {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.cloud.openfeign.model;
|
||||
|
||||
public class Post {
|
||||
|
||||
private String userId;
|
||||
private Long id;
|
||||
private String title;
|
||||
private String body;
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
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 getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.cloud.openfeign.service;
|
||||
|
||||
import com.baeldung.cloud.openfeign.model.Post;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface JSONPlaceHolderService {
|
||||
|
||||
List<Post> getPosts();
|
||||
|
||||
Post getPostById(Long id);
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.cloud.openfeign.service.impl;
|
||||
|
||||
import com.baeldung.cloud.openfeign.client.JSONPlaceHolderClient;
|
||||
import com.baeldung.cloud.openfeign.model.Post;
|
||||
import com.baeldung.cloud.openfeign.service.JSONPlaceHolderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class JSONPlaceHolderServiceImpl implements JSONPlaceHolderService {
|
||||
|
||||
@Autowired
|
||||
private JSONPlaceHolderClient jsonPlaceHolderClient;
|
||||
|
||||
@Override
|
||||
public List<Post> getPosts() {
|
||||
return jsonPlaceHolderClient.getPosts();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post getPostById(Long id) {
|
||||
return jsonPlaceHolderClient.getPostById(id);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
spring.application.name= openfeign
|
||||
logging.level.com.baeldung.cloud.openfeign.client: DEBUG
|
||||
feign.hystrix.enabled=true
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.cloud.openfeign;
|
||||
|
||||
import com.baeldung.cloud.openfeign.model.Post;
|
||||
import com.baeldung.cloud.openfeign.service.JSONPlaceHolderService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class OpenfeignUnitTest {
|
||||
|
||||
@Autowired
|
||||
private JSONPlaceHolderService jsonPlaceHolderService;
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetPosts_thenListPostSizeGreaterThanZero() {
|
||||
|
||||
List<Post> posts = jsonPlaceHolderService.getPosts();
|
||||
|
||||
assertFalse(posts.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetPostWithId_thenPostExist() {
|
||||
|
||||
Post post = jsonPlaceHolderService.getPostById(1L);
|
||||
|
||||
assertNotNull(post);
|
||||
}
|
||||
|
||||
}
|
|
@ -65,22 +65,11 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>http://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<spring-cloud.version>Greenwich.M3</spring-cloud.version>
|
||||
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.spring.controller;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
|
||||
@ControllerAdvice
|
||||
public class ConstraintViolationExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
@ExceptionHandler(value = {ConstraintViolationException.class})
|
||||
protected ResponseEntity<Object> handleConstraintViolation(ConstraintViolationException e, WebRequest request) {
|
||||
return handleExceptionInternal(e, e.getMessage(), new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
||||
}
|
||||
}
|
||||
|
|
@ -3,8 +3,8 @@
|
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-remoting-hessian-burlap-client</artifactId>
|
||||
<name>spring-remoting-hessian-burlap-client</name>
|
||||
<artifactId>remoting-hessian-burlap-client</artifactId>
|
||||
<name>remoting-hessian-burlap-client</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>remoting-hessian-burlap</artifactId>
|
||||
|
|
|
@ -158,11 +158,6 @@
|
|||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.thoughtworks.xstream</groupId>
|
||||
<artifactId>xstream</artifactId>
|
||||
<version>${xstream.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- util -->
|
||||
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
package org.baeldung.web.error;
|
||||
|
||||
import org.baeldung.web.exception.MyResourceNotFoundException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
@ControllerAdvice
|
||||
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
public RestResponseEntityExceptionHandler() {
|
||||
super();
|
||||
}
|
||||
|
||||
@ExceptionHandler(value = { MyResourceNotFoundException.class })
|
||||
protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final WebRequest request) {
|
||||
final String bodyOfResponse = "This should be application specific";
|
||||
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
package org.baeldung.web.util;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* Provides some constants and utility methods to build a Link Header to be stored in the {@link HttpServletResponse} object
|
||||
*/
|
||||
public final class LinkUtil {
|
||||
|
||||
public static final String REL_COLLECTION = "collection";
|
||||
public static final String REL_NEXT = "next";
|
||||
public static final String REL_PREV = "prev";
|
||||
public static final String REL_FIRST = "first";
|
||||
public static final String REL_LAST = "last";
|
||||
|
||||
private LinkUtil() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
/**
|
||||
* Creates a Link Header to be stored in the {@link HttpServletResponse} to provide Discoverability features to the user
|
||||
*
|
||||
* @param uri
|
||||
* the base uri
|
||||
* @param rel
|
||||
* the relative path
|
||||
*
|
||||
* @return the complete url
|
||||
*/
|
||||
public static String createLinkHeader(final String uri, final String rel) {
|
||||
return "<" + uri + ">; rel=\"" + rel + "\"";
|
||||
}
|
||||
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
package org.baeldung.common.web;
|
||||
|
||||
import static org.baeldung.Consts.APPLICATION_PORT;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.baeldung.test.IMarshaller;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.net.HttpHeaders;
|
||||
|
||||
public abstract class AbstractLiveTest<T extends Serializable> {
|
||||
|
||||
protected final Class<T> clazz;
|
||||
|
||||
@Autowired
|
||||
protected IMarshaller marshaller;
|
||||
|
||||
public AbstractLiveTest(final Class<T> clazzToSet) {
|
||||
super();
|
||||
|
||||
Preconditions.checkNotNull(clazzToSet);
|
||||
clazz = clazzToSet;
|
||||
}
|
||||
|
||||
// template method
|
||||
|
||||
public abstract void create();
|
||||
|
||||
public abstract String createAsUri();
|
||||
|
||||
protected final void create(final T resource) {
|
||||
createAsUri(resource);
|
||||
}
|
||||
|
||||
protected final String createAsUri(final T resource) {
|
||||
final Response response = createAsResponse(resource);
|
||||
Preconditions.checkState(response.getStatusCode() == 201, "create operation: " + response.getStatusCode());
|
||||
|
||||
final String locationOfCreatedResource = response.getHeader(HttpHeaders.LOCATION);
|
||||
Preconditions.checkNotNull(locationOfCreatedResource);
|
||||
return locationOfCreatedResource;
|
||||
}
|
||||
|
||||
final Response createAsResponse(final T resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
|
||||
final String resourceAsString = marshaller.encode(resource);
|
||||
return RestAssured.given()
|
||||
.contentType(marshaller.getMime())
|
||||
.body(resourceAsString)
|
||||
.post(getURL());
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
protected String getURL() {
|
||||
return "http://localhost:" + APPLICATION_PORT + "/spring-rest-full/auth/foos";
|
||||
}
|
||||
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
package org.baeldung.spring;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.test")
|
||||
public class ConfigIntegrationTest extends WebMvcConfigurerAdapter {
|
||||
|
||||
public ConfigIntegrationTest() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package org.baeldung.test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface IMarshaller {
|
||||
|
||||
<T> String encode(final T entity);
|
||||
|
||||
<T> T decode(final String entityAsString, final Class<T> clazz);
|
||||
|
||||
<T> List<T> decodeList(final String entitiesAsString, final Class<T> clazz);
|
||||
|
||||
String getMime();
|
||||
|
||||
}
|
|
@ -1,81 +0,0 @@
|
|||
package org.baeldung.test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
public final class JacksonMarshaller implements IMarshaller {
|
||||
private final Logger logger = LoggerFactory.getLogger(JacksonMarshaller.class);
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public JacksonMarshaller() {
|
||||
super();
|
||||
|
||||
objectMapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public final <T> String encode(final T resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
String entityAsJSON = null;
|
||||
try {
|
||||
entityAsJSON = objectMapper.writeValueAsString(resource);
|
||||
} catch (final IOException ioEx) {
|
||||
logger.error("", ioEx);
|
||||
}
|
||||
|
||||
return entityAsJSON;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final <T> T decode(final String resourceAsString, final Class<T> clazz) {
|
||||
Preconditions.checkNotNull(resourceAsString);
|
||||
|
||||
T entity = null;
|
||||
try {
|
||||
entity = objectMapper.readValue(resourceAsString, clazz);
|
||||
} catch (final IOException ioEx) {
|
||||
logger.error("", ioEx);
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public final <T> List<T> decodeList(final String resourcesAsString, final Class<T> clazz) {
|
||||
Preconditions.checkNotNull(resourcesAsString);
|
||||
|
||||
List<T> entities = null;
|
||||
try {
|
||||
if (clazz.equals(Foo.class)) {
|
||||
entities = objectMapper.readValue(resourcesAsString, new TypeReference<List<Foo>>() {
|
||||
// ...
|
||||
});
|
||||
} else {
|
||||
entities = objectMapper.readValue(resourcesAsString, List.class);
|
||||
}
|
||||
} catch (final IOException ioEx) {
|
||||
logger.error("", ioEx);
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final String getMime() {
|
||||
return MediaType.APPLICATION_JSON.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
package org.baeldung.test;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Profile("test")
|
||||
public class TestMarshallerFactory implements FactoryBean<IMarshaller> {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public TestMarshallerFactory() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public IMarshaller getObject() {
|
||||
final String testMime = env.getProperty("test.mime");
|
||||
if (testMime != null) {
|
||||
switch (testMime) {
|
||||
case "json":
|
||||
return new JacksonMarshaller();
|
||||
case "xml":
|
||||
return new XStreamMarshaller();
|
||||
default:
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
return new JacksonMarshaller();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<IMarshaller> getObjectType() {
|
||||
return IMarshaller.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -2,9 +2,8 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-soap</artifactId>
|
||||
<name>spring-soap</name>
|
||||
<version>1.0.0</version>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -38,22 +38,11 @@
|
|||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<spring-cloud.version>2.1.0.RC3</spring-cloud.version>
|
||||
<spring-cloud.version>2.1.0.RELEASE</spring-cloud.version>
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<!-- Maven plugins -->
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>http://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
</project>
|
Loading…
Reference in New Issue