jackson-annotations -> jackson
This commit is contained in:
parent
565b0a9034
commit
5e60c50851
|
@ -1,222 +0,0 @@
|
|||
<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>jackson-annotations</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>jackson-annotations</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- utils -->
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--jackson for xml-->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- marshalling -->
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-joda</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.module</groupId>
|
||||
<artifactId>jackson-module-jsonSchema</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>${gson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>rest-assured</artifactId>
|
||||
<version>${rest-assured.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>json-schema-validator</artifactId>
|
||||
<version>${rest-assured.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>json-path</artifactId>
|
||||
<version>${rest-assured.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.fge</groupId>
|
||||
<artifactId>json-schema-validator</artifactId>
|
||||
<version>${json-schema-validator.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- logging -->
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>jackson</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>${maven-compiler-plugin.source}</source>
|
||||
<target>${maven-compiler-plugin.source}</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>
|
||||
<!-- marshalling -->
|
||||
<jackson.version>2.8.5</jackson.version>
|
||||
|
||||
<!-- logging -->
|
||||
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||
<logback.version>1.1.7</logback.version>
|
||||
|
||||
<!-- various -->
|
||||
<joda-time.version>2.9.6</joda-time.version>
|
||||
<gson.version>2.8.0</gson.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
|
||||
<!-- util -->
|
||||
<guava.version>20.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-io.version>2.5</commons-io.version>
|
||||
|
||||
<!-- testing -->
|
||||
<junit.version>4.12</junit.version>
|
||||
<rest-assured.version>3.0.1</rest-assured.version>
|
||||
<json-schema-validator.version>2.2.6</json-schema-validator.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven-compiler-plugin.source>1.8</maven-compiler-plugin.source>
|
||||
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -88,6 +88,27 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>json-schema-validator</artifactId>
|
||||
<version>${rest-assured.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>json-path</artifactId>
|
||||
<version>${rest-assured.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
|
@ -186,7 +207,8 @@
|
|||
<org.hamcrest.version>1.3</org.hamcrest.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mockito.version>1.10.19</mockito.version>
|
||||
<rest-assured.version>2.9.0</rest-assured.version>
|
||||
<rest-assured.version>3.0.1</rest-assured.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
|
|
|
@ -5,12 +5,6 @@ import com.baeldung.jacksonannotation.domain.Item;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Author extends Person {
|
||||
|
||||
List<Item> items = new ArrayList<>();
|
|
@ -4,12 +4,6 @@ import com.fasterxml.jackson.annotation.JacksonInject;
|
|||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Person {
|
||||
|
||||
@JacksonInject
|
|
@ -2,20 +2,12 @@ package com.baeldung.jacksonannotation.deserialization.jsonanysetter;
|
|||
|
||||
import com.baeldung.jacksonannotation.domain.Author;
|
||||
import com.baeldung.jacksonannotation.domain.Item;
|
||||
import com.fasterxml.jackson.annotation.JsonAnyGetter;
|
||||
import com.fasterxml.jackson.annotation.JsonAnySetter;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
* @see JsonAnyGetter
|
||||
*/
|
||||
public class Inventory {
|
||||
|
||||
private Map<Author, Item> stock = new HashMap<>();
|
|
@ -4,19 +4,11 @@ package com.baeldung.jacksonannotation.deserialization.jsoncreator;
|
|||
import com.baeldung.jacksonannotation.domain.Item;
|
||||
import com.baeldung.jacksonannotation.domain.Person;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonGetter;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
* @see JsonGetter
|
||||
*/
|
||||
public class Author extends Person {
|
||||
|
||||
List<Item> items = new ArrayList<>();
|
|
@ -6,12 +6,6 @@ import com.baeldung.jacksonannotation.domain.Person;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Author extends Person {
|
||||
|
||||
List<Item> items = new ArrayList<>();
|
|
@ -5,12 +5,6 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
|||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class Book extends Item {
|
||||
|
||||
private String ISBN;
|
|
@ -1,7 +1,6 @@
|
|||
package com.baeldung.jacksonannotation.deserialization.jsondeserialize;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
|
||||
|
@ -10,12 +9,6 @@ import java.text.ParseException;
|
|||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class CustomDateDeserializer extends StdDeserializer<Date> {
|
||||
|
||||
private static SimpleDateFormat formatter =
|
||||
|
@ -31,7 +24,7 @@ public class CustomDateDeserializer extends StdDeserializer<Date> {
|
|||
|
||||
@Override
|
||||
public Date deserialize(JsonParser jsonparser, DeserializationContext context)
|
||||
throws IOException, JsonProcessingException {
|
||||
throws IOException {
|
||||
String date = jsonparser.getText();
|
||||
try {
|
||||
return formatter.parse(date);
|
|
@ -1,40 +1,40 @@
|
|||
package com.baeldung.jacksonannotation.format;
|
||||
|
||||
import java.util.Date;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
import com.baeldung.jacksonannotation.domain.Person;
|
||||
|
||||
/**
|
||||
* @author Jay Sridhar
|
||||
* @version 1.0
|
||||
*/
|
||||
public class User extends Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING,
|
||||
pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")
|
||||
private Date createdDate;
|
||||
|
||||
public User(String firstName,String lastName) {
|
||||
super(firstName, lastName);
|
||||
this.createdDate = new Date();
|
||||
}
|
||||
|
||||
public Date getCreatedDate() {
|
||||
return createdDate;
|
||||
}
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING,
|
||||
pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ",
|
||||
locale = "en_GB")
|
||||
public Date getCurrentDate() {
|
||||
return new Date();
|
||||
}
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
|
||||
public Date getDateNum() {
|
||||
return new Date();
|
||||
}
|
||||
}
|
||||
package com.baeldung.jacksonannotation.format;
|
||||
|
||||
import java.util.Date;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
import com.baeldung.jacksonannotation.domain.Person;
|
||||
|
||||
/**
|
||||
* @author Jay Sridhar
|
||||
* @version 1.0
|
||||
*/
|
||||
public class User extends Person {
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING,
|
||||
pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ")
|
||||
private Date createdDate;
|
||||
|
||||
public User(String firstName,String lastName) {
|
||||
super(firstName, lastName);
|
||||
this.createdDate = new Date();
|
||||
}
|
||||
|
||||
public Date getCreatedDate() {
|
||||
return createdDate;
|
||||
}
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING,
|
||||
pattern = "yyyy-MM-dd@HH:mm:ss.SSSZ",
|
||||
locale = "en_GB")
|
||||
public Date getCurrentDate() {
|
||||
return new Date();
|
||||
}
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
|
||||
public Date getDateNum() {
|
||||
return new Date();
|
||||
}
|
||||
}
|
|
@ -1,39 +1,39 @@
|
|||
package com.baeldung.jacksonannotation.format;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.data.Percentage.withPercentage;
|
||||
|
||||
/**
|
||||
* @author Jay Sridhar
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonFormatTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializedDateFormat_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
User user = new User("Jay", "Sridhar");
|
||||
|
||||
String result = new ObjectMapper().writeValueAsString(user);
|
||||
|
||||
// Expected to match: "2016-12-19@09:34:42.628+0000"
|
||||
assertThat(from(result).getString("createdDate"))
|
||||
.matches("\\d{4}\\-\\d{2}\\-\\d{2}@\\d{2}:\\d{2}:\\d{2}\\.\\d{3}\\+\\d{4}");
|
||||
|
||||
// Expected to be close to current time
|
||||
long now = new Date().getTime();
|
||||
assertThat(from(result).getLong("dateNum"))
|
||||
.isCloseTo(now, withPercentage(10.0));
|
||||
|
||||
}
|
||||
}
|
||||
package com.baeldung.jacksonannotation.format;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static io.restassured.path.json.JsonPath.from;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.data.Percentage.withPercentage;
|
||||
|
||||
/**
|
||||
* @author Jay Sridhar
|
||||
* @version 1.0
|
||||
*/
|
||||
public class JsonFormatTest {
|
||||
|
||||
@Test
|
||||
public void whenSerializedDateFormat_thenCorrect() throws JsonProcessingException {
|
||||
|
||||
User user = new User("Jay", "Sridhar");
|
||||
|
||||
String result = new ObjectMapper().writeValueAsString(user);
|
||||
|
||||
// Expected to match: "2016-12-19@09:34:42.628+0000"
|
||||
assertThat(from(result).getString("createdDate"))
|
||||
.matches("\\d{4}\\-\\d{2}\\-\\d{2}@\\d{2}:\\d{2}:\\d{2}\\.\\d{3}\\+\\d{4}");
|
||||
|
||||
// Expected to be close to current time
|
||||
long now = new Date().getTime();
|
||||
assertThat(from(result).getLong("dateNum"))
|
||||
.isCloseTo(now, withPercentage(10.0));
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue