This commit is contained in:
egmp777 2014-08-25 14:36:56 -05:00
commit 6272a5383f
69 changed files with 1029 additions and 273 deletions

View File

@ -146,11 +146,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- marshalling -->
<jackson.version>2.4.1</jackson.version>
@ -181,7 +181,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -256,11 +256,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -288,7 +288,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

36
gson/.classpath Normal file
View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ant.AntBuilderLaunchConfigurationType">
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_BUILDER_ENABLED" value="false"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_DISABLED_BUILDER" value="org.eclipse.wst.jsdt.core.javascriptValidator"/>
<mapAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS"/>
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
</launchConfiguration>

13
gson/.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear

36
gson/.project Normal file
View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>jackson</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>

14
gson/.springBeans Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[3.3.0.201307091516-RELEASE]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
<config>src/main/webapp/WEB-INF/api-servlet.xml</config>
</configs>
<configSets>
</configSets>
</beansProjectDescription>

6
gson/README.md Normal file
View File

@ -0,0 +1,6 @@
=========
## GSON Cookbooks and Examples
### Relevant Articles:

151
gson/pom.xml Normal file
View File

@ -0,0 +1,151 @@
<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>org.baeldung</groupId>
<artifactId>gson</artifactId>
<version>0.1-SNAPSHOT</version>
<name>gson</name>
<dependencies>
<!-- utils -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>17.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<!-- web -->
<!-- marshalling -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>gson</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
</plugins>
</build>
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- marshalling -->
<gson.version>2.3</gson.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
<logback.version>1.1.2</logback.version>
<!-- various -->
<hibernate-validator.version>5.1.2.Final</hibernate-validator.version>
<!-- util -->
<guava.version>17.0</guava.version>
<commons-lang3.version>3.3.2</commons-lang3.version>
<!-- testing -->
<org.hamcrest.version>1.3</org.hamcrest.version>
<junit.version>4.11</junit.version>
<mockito.version>1.9.5</mockito.version>
<httpcore.version>4.3.2</httpcore.version>
<httpclient.version>4.3.5</httpclient.version>
<rest-assured.version>2.3.2</rest-assured.version>
<!-- maven plugins -->
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>
</project>

View File

@ -0,0 +1,20 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd" >
</beans>

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Spring MVC Application</display-name>
<!-- Spring root -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>org.baeldung.config</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring child -->
<servlet>
<servlet-name>api</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>api</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file />
</welcome-file-list>
</web-app>

View File

@ -0,0 +1,51 @@
package org.baeldung.gson.deserialization;
public class Foo {
public int intValue;
public String stringValue;
public Foo(final int intValue, final String stringValue) {
this.intValue = intValue;
this.stringValue = stringValue;
}
public Foo() {
super();
}
// API
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + intValue;
result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Foo other = (Foo) obj;
if (intValue != other.intValue)
return false;
if (stringValue == null) {
if (other.stringValue != null)
return false;
} else if (!stringValue.equals(other.stringValue))
return false;
return true;
}
@Override
public String toString() {
return "TargetClass{" + "intValue= " + intValue + ", stringValue= " + stringValue + '}';
}
}

View File

@ -0,0 +1,26 @@
package org.baeldung.gson.deserialization;
import java.lang.reflect.Type;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
public class FooDeserializer implements JsonDeserializer<Foo[]> {
@Override
public Foo[] deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
final JsonArray jArray = json.getAsJsonArray();
final Foo[] scArray = new Foo[jArray.size()];
int index = 0;
for (final JsonElement jElement : jArray) {
final int i = jElement.getAsJsonObject().get("intValue").getAsInt();
final String s = jElement.getAsJsonObject().get("stringValue").getAsString();
scArray[index++] = new Foo(i, s);
}
return scArray;
}
}

View File

@ -0,0 +1,21 @@
package org.baeldung.gson.deserialization;
import java.lang.reflect.Type;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
public class FooDeserializerFromJsonWithDifferentFields implements JsonDeserializer<Foo> {
@Override
public Foo deserialize(final JsonElement jElement, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
final JsonObject jObject = jElement.getAsJsonObject();
final int intValue = jObject.get("valueInt").getAsInt();
final String stringValue = jObject.get("valueString").getAsString();
return new Foo(intValue, stringValue);
}
}

View File

@ -0,0 +1,18 @@
package org.baeldung.gson.deserialization;
public class GenericFoo<T> {
public T theValue;
public GenericFoo(final T value) {
theValue = value;
}
//
@Override
public final String toString() {
return "GenericTargetClass{" + "intField=" + theValue + '}';
}
}

View File

@ -0,0 +1,111 @@
package org.baeldung.gson.deserialization.test;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import org.baeldung.gson.deserialization.Foo;
import org.baeldung.gson.deserialization.FooDeserializerFromJsonWithDifferentFields;
import org.baeldung.gson.deserialization.GenericFoo;
import org.junit.Test;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
public class GsonDeserializationTest {
// tests - single element
@Test
public final void whenDeserializingToSimpleObject_thenCorrect() {
final String json = "{\"intValue\":1,\"stringValue\":\"one\"}";
final Foo targetObject = new Gson().fromJson(json, Foo.class);
assertEquals(targetObject.intValue, 1);
assertEquals(targetObject.stringValue, "one");
}
@Test
public final void givenJsonHasExtraValues_whenDeserializing_thenCorrect() {
final String json = "{\"intValue\":1,\"stringValue\":\"one\",\"extraString\":\"two\",\"extraFloat\":2.2}";
final Foo targetObject = new Gson().fromJson(json, Foo.class);
assertEquals(targetObject.intValue, 1);
assertEquals(targetObject.stringValue, "one");
}
@Test
public final void givenJsonHasNonMatchingFields_whenDeserializingWithCustomDeserializer_thenCorrect() {
final String json = "{\"valueInt\":7,\"valueString\":\"seven\"}";
final GsonBuilder gsonBldr = new GsonBuilder();
gsonBldr.registerTypeAdapter(Foo.class, new FooDeserializerFromJsonWithDifferentFields());
final Foo targetObject = gsonBldr.create().fromJson(json, Foo.class);
assertEquals(targetObject.intValue, 7);
assertEquals(targetObject.stringValue, "seven");
}
@Test
public final void whenDeserializingToGenericObject_thenCorrect() {
final Type typeToken = new TypeToken<GenericFoo<Integer>>() {
}.getType();
final String json = "{\"theValue\":1}";
final GenericFoo<Integer> targetObject = new Gson().fromJson(json, typeToken);
assertEquals(targetObject.theValue, new Integer(1));
}
// tests - multiple elements
@Test
public final void givenJsonArrayOfFoos_whenDeserializingToArray_thenCorrect() {
final String json = "[{\"intValue\":1,\"stringValue\":\"one\"}," + "{\"intValue\":2,\"stringValue\":\"two\"}]";
final Foo[] targetArray = new GsonBuilder().create().fromJson(json, Foo[].class);
assertThat(Lists.newArrayList(targetArray), hasItem(new Foo(1, "one")));
assertThat(Lists.newArrayList(targetArray), hasItem(new Foo(2, "two")));
assertThat(Lists.newArrayList(targetArray), not(hasItem(new Foo(1, "two"))));
}
@Test
public final void givenJsonArrayOfFoos_whenDeserializingCollection_thenCorrect() {
final String json = "[{\"intValue\":1,\"stringValue\":\"one\"},{\"intValue\":2,\"stringValue\":\"two\"}]";
final Type targetClassType = new TypeToken<ArrayList<Foo>>() {
}.getType();
final Collection<Foo> targetCollection = new Gson().fromJson(json, targetClassType);
assertThat(targetCollection, instanceOf(ArrayList.class));
}
//
@Test
public void whenDeserializingJsonIntoElements_thenCorrect() {
final String jsonSourceObject = "{\"valueInt\":7,\"valueString\":\"seven\"}";
final JsonParser jParser = new JsonParser();
final JsonElement jElement = jParser.parse(jsonSourceObject);
final JsonObject jObject = jElement.getAsJsonObject();
final int intValue = jObject.get("valueInt").getAsInt();
final String stringValue = jObject.get("valueString").getAsString();
final Foo targetObject = new Foo(intValue, stringValue);
assertEquals(targetObject.intValue, 7);
assertEquals(targetObject.stringValue, "seven");
}
}

13
gson/src/test/resources/.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear

View File

@ -95,11 +95,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -127,7 +127,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -150,11 +150,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -182,7 +182,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -1,4 +1,5 @@
<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">
<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>org.baeldung</groupId>
<artifactId>jackson</artifactId>
@ -13,7 +14,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>16.0.1</version>
<version>17.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
@ -43,6 +44,12 @@
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
<!-- test scoped -->
<dependency>
@ -108,11 +115,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- marshalling -->
<jackson.version>2.4.1</jackson.version>
@ -143,7 +150,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -0,0 +1,10 @@
package org.baeldung.gson;
public class GenericSourceClass {
int intField;
public GenericSourceClass(final int i) {
intField = i;
}
}

View File

@ -0,0 +1,32 @@
package org.baeldung.gson;
import org.junit.Before;
import org.junit.Test;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class GsonDeserializationUnitTest {
private Gson gson;
@Before
public final void before() {
gson = new Gson();
}
// tests
@Test
public void givenUsingGson_whenDeserializingGeneric_thenCorrect() {
final java.lang.reflect.Type genericSourceClassType = new TypeToken<GenericSourceClass>() {
}.getType();
final GenericSourceClass sourceObject = new GenericSourceClass(1);
final String serializedSourceObject = gson.toJson(sourceObject, genericSourceClassType);
final GenericSourceClass targetObject = gson.fromJson(serializedSourceObject, genericSourceClassType);
System.out.println(targetObject);
}
}

View File

@ -90,11 +90,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -122,7 +122,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -134,7 +134,7 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.3.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>

View File

@ -193,13 +193,13 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<javassist.version>3.18.1-GA</javassist.version>
<jstl.version>1.2</jstl.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -227,7 +227,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -205,13 +205,13 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<javassist.version>3.18.1-GA</javassist.version>
<jstl.version>1.2</jstl.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<tomcat-dbcp.version>7.0.42</tomcat-dbcp.version>
<!-- logging -->
@ -241,7 +241,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -164,12 +164,12 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<javassist.version>3.18.1-GA</javassist.version>
<!-- persistence -->
<hibernate.version>3.6.10.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<tomcat-dbcp.version>7.0.47</tomcat-dbcp.version>
<!-- logging -->
@ -197,7 +197,7 @@
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -171,12 +171,12 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<javassist.version>3.18.1-GA</javassist.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<tomcat-dbcp.version>7.0.42</tomcat-dbcp.version>
<!-- logging -->
@ -204,7 +204,7 @@
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -166,11 +166,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -197,7 +197,7 @@
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
<!-- <maven-war-plugin.version>2.4</maven-war-plugin.version> -->
</properties>

View File

@ -142,11 +142,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -174,7 +174,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -166,7 +166,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -168,7 +168,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -8,7 +8,7 @@
<body>
<h3>Welcome, Enter The Employee Details</h3>
<form:form method="POST" action="/spring-mvc-xml/addEmployee" commandName="employee">
<form:form method="POST" action="/spring-mvc-xml/addEmployee" modelAttribute="employee">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>

View File

@ -203,11 +203,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- marshalling -->
@ -238,7 +238,7 @@
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -227,11 +227,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -259,7 +259,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -10,7 +10,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.4.RELEASE</version>
<version>1.1.5.RELEASE</version>
</parent>
<dependencies>
<!-- Spring -->
@ -125,7 +125,7 @@
<properties>
<java-version>1.7</java-version>
<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<!-- logging -->
<org.slf4j.version>1.7.6</org.slf4j.version>
@ -135,7 +135,7 @@
<!-- Inject -->
<javax.inject.version>1</javax.inject.version>
<!-- Spring Data Jpa -->
<spring-data-jpa.version>1.4.1.RELEASE</spring-data-jpa.version>
<spring-data-jpa.version>1.4.5.RELEASE</spring-data-jpa.version>
<!-- guava -->
<guava.version>17.0</guava.version>
</properties>

View File

@ -18,9 +18,12 @@ public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(targetEntity = User.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private User user;
@Column(name = "role")
private Integer role;

View File

@ -20,36 +20,44 @@ public class UserDto {
@NotNull
@NotEmpty
private String username;
private Integer role;
public Integer getRole() {
return role;
}
public void setRole(Integer role) {
this.role = role;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@ -59,11 +67,11 @@ public class UserDto {
public void setMatchingPassword(String matchingPassword) {
this.matchingPassword = matchingPassword;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("User [firstName=").append(firstName).append("]").
append("[lastName=").append(lastName).append("]").append("[username").append(username).append("]").append("[password").append(password).append("]");
builder.append("User [firstName=").append(firstName).append("]").append("[lastName=").append(lastName).append("]").append("[username").append(username).append("]").append("[password").append(password).append("]");
return builder.toString();
}
}

View File

@ -0,0 +1,33 @@
package org.baeldung.persistence.service;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class UserValidator implements Validator {
private Pattern pattern;
private Matcher matcher;
private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
@Override
public boolean supports(Class<?> clazz) {
return UserDto.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object obj, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "message.firstName", "Firstname is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "message.lastName", "LastName is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "message.password", "LastName is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "message.username", "UserName is required.");
}
public boolean validateEmail(String email) {
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(email);
return matcher.matches();
}
}

View File

@ -21,11 +21,8 @@ import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@ComponentScan(basePackages = {
"org.baeldung.web.controller", "org.baeldung.persistence.service", "org.baeldung.persistence.dao"
})
@ComponentScan(basePackages = { "org.baeldung.web.controller", "org.baeldung.persistence.service", "org.baeldung.persistence.dao" })
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {
@ -60,8 +57,7 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/","/resources/");
registry.addResourceHandler("/resources/**").addResourceLocations("/", "/resources/");
}
@Override
@ -93,11 +89,11 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
UsernameValidator userNameValidator = new UsernameValidator();
return userNameValidator;
}
@Bean
public PasswordMatchesValidator passwordMatchesValidator() {
PasswordMatchesValidator passwordMatchesValidator = new PasswordMatchesValidator();
return passwordMatchesValidator;
}
}

View File

@ -66,4 +66,5 @@ public class RegistrationController {
}
return registered;
}
}

View File

@ -1,6 +1,5 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="sec"
uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<html>
<sec:authorize ifAnyGranted="ROLE_USER">
@ -9,6 +8,10 @@
<head>
<link href="<c:url value="/resources/bootstrap.css" />" rel="stylesheet">
</head>
<<<<<<< HEAD
=======
>>>>>>> FETCH_HEAD
<body>
<sec:authorize ifAnyGranted="ROLE_ADMIN">
<H1>Hello Admin</H1>

View File

@ -1,7 +1,7 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="security"
uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"%>
<html>
<head>
<link href="<c:url value="/resources/bootstrap.css" />" rel="stylesheet">
</head>
@ -18,4 +18,5 @@
<a href="<c:url value="/j_spring_security_logout" />">Logout</a>
<a href="<c:url value="/admin.html" />">Administrator Page</a>
</body>
</html>

View File

@ -1,6 +1,7 @@
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="true"%>
<html>
<head>
<link href="<c:url value="/resources/bootstrap.css" />" rel="stylesheet">
<title>Home</title>
@ -8,4 +9,5 @@
<body>
<h1>Welcome back home!</h1>
</body>
</html>

View File

@ -1,6 +1,10 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<<<<<<< HEAD
<%@ taglib prefix="sec"
uri="http://www.springframework.org/security/tags"%>
=======
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
>>>>>>> FETCH_HEAD
<%@ page session="true"%>
<html>
<head>
@ -23,4 +27,8 @@
<a href="<c:url value="/home.html" />">Home</a>
<a href="<c:url value="/admin.html" />">Administrator Page</a>
</body>
<<<<<<< HEAD
=======
>>>>>>> FETCH_HEAD
</html>

View File

@ -10,4 +10,5 @@
<spring:message code="message.sessionExpired"></spring:message>
</h1>
</body>
</html>

View File

@ -1,6 +1,5 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="sec"
uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<fmt:setBundle basename="messages" />
@ -9,6 +8,7 @@
<fmt:message key="message.password" var="noPass" />
<fmt:message key="message.username" var="noUser" />
<html>
<head>
<link href="<c:url value="/resources/bootstrap.css" />" rel="stylesheet">
<c:if test="${param.error != null}">
@ -29,7 +29,6 @@
document.f.j_username.focus();
return false;
}
if (document.f.j_password.value == "") {
alert("${noPass}");
document.f.j_password.focus();
@ -42,8 +41,7 @@
<h1>Login</h1>
<a href="?lang=en">English</a> |
<a href="?lang=es_ES">Spanish</a>
<form name='f' action="j_spring_security_check" method='POST'
onsubmit="return validate();">
<form name='f' action="j_spring_security_check" method='POST' onsubmit="return validate();">
<table>
<tr>
<td>User:</td>
@ -57,9 +55,11 @@
<td><input name="submit" type="submit" value="submit" /></td>
</tr>
</table>
</form>
<br> Current Locale : ${pageContext.response.locale}
<br>
<a href="<c:url value="/user/registration" />">Sign Up</a>
</body>
</html>

View File

@ -1,9 +1,9 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="sec"
uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<html>
<head>
<link href="<c:url value="/resources/bootstrap.css" />" rel="stylesheet">
<c:if test="${not empty SPRING_SECURITY_LAST_EXCEPTION}">
@ -11,15 +11,17 @@
<spring:message code="message.logoutError"></spring:message>
</div>
</c:if>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Logged Out</title>
</head>
<body>
<c:if test="${param.logSucc == true}">
<div id="success">
<spring:message code="message.logoutSucc"></spring:message>
</div>
</c:if>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Logged Out</title>
</head>
<body>
<a href="login.html">Login</a>
</body>
</html>

View File

@ -11,6 +11,7 @@
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Registration</title>
</head>
<body>
<H1>This is the registration page</H1>
<form:form modelAttribute="user" method="POST" enctype="utf8" role="form">
@ -46,4 +47,5 @@
<br>
<a href="<c:url value="login.html" />">Back to Login</a>
</body>
</html>

View File

@ -1,22 +1,22 @@
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="sec"
uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<fmt:setBundle basename="messages" />
<%@ page session="true"%>
<html>
<head>
<link href="<c:url value="/resources/bootstrap.css" />" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Registration Success</title>
</head>
<body>
<div id="success">
<h1>
<spring:message code="message.regSucc"></spring:message>
</div>
<a href="<c:url value="login.html" />"><spring:message
code="label.login"></spring:message></a>
</h1>
<a href="<c:url value="login.html" />"><spring:message code="label.login"></spring:message></a>
</body>
</html>

View File

@ -232,11 +232,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -264,7 +264,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -227,11 +227,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -259,7 +259,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -224,11 +224,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -256,7 +256,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -261,11 +261,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -293,7 +293,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -232,11 +232,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -264,7 +264,7 @@
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.6</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -288,11 +288,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- http -->
<httpcore.version>4.3.2</httpcore.version>
@ -320,7 +320,7 @@
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -252,11 +252,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -283,7 +283,7 @@
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -89,7 +89,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.2</version>
<version>${jackson.version}</version>
</dependency>
<!-- http -->
@ -276,11 +276,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- http -->
<httpcore.version>4.3.2</httpcore.version>
@ -290,6 +290,9 @@
<org.slf4j.version>1.7.7</org.slf4j.version>
<logback.version>1.1.2</logback.version> <!-- do not upgrade - see http://jira.qos.ch/browse/LOGBACK-851 -->
<!-- marshalling -->
<jackson.version>2.4.2</jackson.version>
<!-- various -->
<hibernate-validator.version>5.1.2.Final</hibernate-validator.version>
@ -308,7 +311,7 @@
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -4,6 +4,7 @@ import java.net.URI;
import org.apache.http.HttpHost;
import org.apache.http.client.AuthCache;
import org.apache.http.client.HttpClient;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.DigestScheme;
import org.apache.http.impl.client.BasicAuthCache;
@ -15,8 +16,8 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
public class HttpComponentsClientHttpRequestFactoryDigestAuth extends HttpComponentsClientHttpRequestFactory {
HttpHost host;
public HttpComponentsClientHttpRequestFactoryDigestAuth(final HttpHost host) {
super();
public HttpComponentsClientHttpRequestFactoryDigestAuth(final HttpHost host, final HttpClient httpClient) {
super(httpClient);
this.host = host;
}

View File

@ -1,9 +1,13 @@
package org.baeldung.client.spring;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.baeldung.client.HttpComponentsClientHttpRequestFactoryDigestAuth;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -12,6 +16,8 @@ import org.springframework.web.client.RestTemplate;
@Configuration
public class ClientConfig {
private static final String DEFAULT_USER = "user1";
private static final String DEFAULT_PASS = "user1Pass";
public ClientConfig() {
super();
@ -22,7 +28,9 @@ public class ClientConfig {
@Bean
public RestTemplate restTemplate() {
final HttpHost host = new HttpHost("localhost", 8080, "http");
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host);
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider()).useSystemProperties().build();
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client);
final RestTemplate restTemplate = new RestTemplate(requestFactory);
final int timeout = 5;
@ -43,9 +51,16 @@ public class ClientConfig {
// httpClient.getParams().setParameter("http.protocol.head-body-timeout", timeout * 1000);
// - note: timeout via the API
final HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout
HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout
// final HttpParams httpParams = httpClient.getParams();
// HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout
// HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout
}
private final CredentialsProvider provider() {
final CredentialsProvider provider = new BasicCredentialsProvider();
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
provider.setCredentials(AuthScope.ANY, credentials);
return provider;
}
}

View File

@ -0,0 +1,64 @@
package org.baeldung.client;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.baeldung.web.dto.Foo;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
public class ClientNoSpringLiveTest {
private static final String DEFAULT_USER = "user1";
private static final String DEFAULT_PASS = "user1Pass";
// tests - no Spring
@Test
public final void givenUsingCustomHttpRequestFactory_whenSecuredRestApiIsConsumed_then200OK() {
final HttpHost host = new HttpHost("localhost", 8080, "http");
final CredentialsProvider credentialsProvider = provider();
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).useSystemProperties().build();
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client);
final RestTemplate restTemplate = new RestTemplate(requestFactory);
// credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1";
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
System.out.println(responseEntity.getStatusCode());
}
@Test
public final void givenUsingStandardRequestFactory_whenSecuredRestApiIsConsumed_then200OK() {
final CredentialsProvider credentialsProvider = provider();
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).useSystemProperties().build();
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client);
final RestTemplate restTemplate = new RestTemplate(requestFactory);
// credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1";
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
System.out.println(responseEntity.getStatusCode());
}
// UTIL
private final CredentialsProvider provider() {
final CredentialsProvider provider = new BasicCredentialsProvider();
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
provider.setCredentials(AuthScope.ANY, credentials);
return provider;
}
}

View File

@ -1,8 +1,5 @@
package org.baeldung.client;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.DefaultHttpClient;
import org.baeldung.client.spring.ClientConfig;
import org.baeldung.web.dto.Foo;
import org.junit.Test;
@ -10,7 +7,6 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ -18,21 +14,18 @@ import org.springframework.web.client.RestTemplate;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ClientConfig.class }, loader = AnnotationConfigContextLoader.class)
public class ClientLiveTest {
public class ClientWithSpringLiveTest {
@Autowired
private RestTemplate restTemplate;
// tests
// tests - no Spring
@Test
public final void whenSecuredRestApiIsConsumed_then200OK() {
final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
final DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();
httpClient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1";
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
System.out.println(responseEntity.getStatusCode());
}

View File

@ -372,11 +372,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<spring-data-jpa.version>1.6.2.RELEASE</spring-data-jpa.version>
<!-- marshalling -->
@ -408,7 +408,7 @@
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>

View File

@ -239,11 +239,11 @@
<properties>
<!-- Spring -->
<org.springframework.version>4.0.6.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.4.RELEASE</org.springframework.security.version>
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
<!-- persistence -->
<hibernate.version>4.3.6.Final</hibernate.version>
<mysql-connector-java.version>5.1.31</mysql-connector-java.version>
<mysql-connector-java.version>5.1.32</mysql-connector-java.version>
<!-- logging -->
<org.slf4j.version>1.7.7</org.slf4j.version>
@ -270,7 +270,7 @@
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.4</maven-war-plugin.version>
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
</properties>