Add Spring MVC form and binding example

This commit is contained in:
Alex Theedom
2016-12-06 13:30:12 +00:00
parent 6cca052251
commit 99ad241f9d
88 changed files with 4219 additions and 0 deletions
+249
View File
@@ -0,0 +1,249 @@
<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>
<url>http://www.baeldung.com</url>
<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>
<!-- web -->
<!-- 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>3.0.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.2.6</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>3.6.1</version>
<scope>test</scope>
</dependency>
<!-- logging -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<!-- <scope>runtime</scope> -->
</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>
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
</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>
<!-- persistence -->
<hibernate.version>4.3.11.Final</hibernate.version>
<mysql-connector-java.version>5.1.38</mysql-connector-java.version>
<!-- 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 -->
<hibernate-validator.version>5.1.3.Final</hibernate-validator.version>
<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 -->
<org.hamcrest.version>1.3</org.hamcrest.version>
<junit.version>4.12</junit.version>
<mockito.version>2.2.26</mockito.version>
<httpcore.version>4.4.1</httpcore.version>
<httpclient.version>4.5</httpclient.version>
<rest-assured.version>2.9.0</rest-assured.version>
<!-- maven plugins -->
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-war-plugin.version>2.6</maven-war-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-compiler-plugin.source>1.8</maven-compiler-plugin.source>
</properties>
</project>
@@ -0,0 +1,32 @@
package com.baeldung.jacksonannotation.deserialization.jacksoninject;
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<>();
public Author(){
super();
}
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
@@ -0,0 +1,49 @@
package com.baeldung.jacksonannotation.deserialization.jacksoninject;
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
private UUID id;
private String firstName;
private String lastName;
public Person(){
}
public Person(String firstName, String lastName) {
this.id = UUID.randomUUID();
this.firstName = firstName;
this.lastName = lastName;
}
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 UUID getId() {
return id;
}
}
@@ -0,0 +1,38 @@
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<>();
private Map<String, Float> countryDeliveryCost = new HashMap<>();
@JsonIgnore
public Map<Author, Item> getStock() {
return stock;
}
public Map<String, Float> getCountryDeliveryCost() {
return countryDeliveryCost;
}
@JsonAnySetter
public void addCountryDeliveryCost(String country, Float cost) {
countryDeliveryCost.put(country, cost);
}
}
@@ -0,0 +1,38 @@
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<>();
@JsonCreator
public Author(
@JsonProperty("christianName") String firstName,
@JsonProperty("surname") String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
@@ -0,0 +1,30 @@
package com.baeldung.jacksonannotation.deserialization.jsondeserialize;
import com.baeldung.jacksonannotation.domain.Item;
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<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
@@ -0,0 +1,53 @@
package com.baeldung.jacksonannotation.deserialization.jsondeserialize;
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;
@JsonDeserialize(using = CustomDateDeserializer.class)
private Date published;
private BigDecimal pages;
public Book() {
super();
}
public Book(String title, Author author) {
super(title, author);
}
public String getISBN() {
return ISBN;
}
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}
public Date getPublished() {
return published;
}
public void setPublished(Date published) {
this.published = published;
}
public BigDecimal getPages() {
return pages;
}
public void setPages(BigDecimal pages) {
this.pages = pages;
}
}
@@ -0,0 +1,42 @@
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;
import java.io.IOException;
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 =
new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
public CustomDateDeserializer() {
this(null);
}
public CustomDateDeserializer(Class<?> vc) {
super(vc);
}
@Override
public Date deserialize(JsonParser jsonparser, DeserializationContext context)
throws IOException, JsonProcessingException {
String date = jsonparser.getText();
try {
return formatter.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,61 @@
package com.baeldung.jacksonannotation.deserialization.jsondeserialize;
import com.baeldung.jacksonannotation.domain.Person;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Item {
private UUID id;
private String title;
private List<Person> authors = new ArrayList<>();
private float price;
public Item(){}
public Item(String title, Author author) {
this.id = UUID.randomUUID();
this.title = title;
this.authors.add(author);
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Person> getAuthors() {
return authors;
}
public void setAuthors(List<Person> authors) {
this.authors = authors;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
@@ -0,0 +1,38 @@
package com.baeldung.jacksonannotation.deserialization.jsonsetter;
import com.baeldung.jacksonannotation.domain.Item;
import com.baeldung.jacksonannotation.domain.Person;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSetter;
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 {
private List<Item> items = new ArrayList<>();
public Author(){
super();
}
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
@JsonIgnore
public List<Item> getItems() {
return items;
}
@JsonSetter("publications")
public void setItems(List<Item> items) {
this.items = items;
}
}
@@ -0,0 +1,27 @@
package com.baeldung.jacksonannotation.domain;
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 {
private List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
@@ -0,0 +1,48 @@
package com.baeldung.jacksonannotation.domain;
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;
private Date published;
private BigDecimal pages;
public Book(){
}
public Book(String title, Author author) {
super(title, author);
}
public String getISBN() {
return ISBN;
}
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}
public Date getPublished() {
return published;
}
public void setPublished(Date published) {
this.published = published;
}
public BigDecimal getPages() {
return pages;
}
public void setPages(BigDecimal pages) {
this.pages = pages;
}
}
@@ -0,0 +1,71 @@
package com.baeldung.jacksonannotation.domain;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Course extends Item {
public enum Medium {CLASSROOM, ONLINE}
public enum Level {
BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3);
private String name;
private int number;
Level(String name, int number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
}
private float duration;
private Medium medium;
private Level level;
private List<Course> prerequisite;
public Course(String title, Author author) {
super(title, author);
}
public float getDuration() {
return duration;
}
public void setDuration(float duration) {
this.duration = duration;
}
public Medium getMedium() {
return medium;
}
public void setMedium(Medium medium) {
this.medium = medium;
}
public Level getLevel() {
return level;
}
public void setLevel(Level level) {
this.level = level;
}
public List<Course> getPrerequisite() {
return prerequisite;
}
public void setPrerequisite(List<Course> prerequisite) {
this.prerequisite = prerequisite;
}
}
@@ -0,0 +1,24 @@
package com.baeldung.jacksonannotation.domain;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Customer extends Person {
private String configuration;
public Customer(String firstName, String lastName) {
super(firstName, lastName);
}
public String getConfiguration() {
return configuration;
}
public void setConfiguration(String configuration) {
this.configuration = configuration;
}
}
@@ -0,0 +1,29 @@
package com.baeldung.jacksonannotation.domain;
import java.util.HashMap;
import java.util.Map;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Inventory {
private Map<Author, Item> stock;
private Map<String, Float> countryDeliveryCost = new HashMap<>();
public Map<Author, Item> getStock() {
return stock;
}
public void setStock(Map<Author, Item> stock) {
this.stock = stock;
}
public Map<String, Float> getCountryDeliveryCost() {
return countryDeliveryCost;
}
}
@@ -0,0 +1,59 @@
package com.baeldung.jacksonannotation.domain;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Item {
private UUID id;
private String title;
private List<Person> authors = new ArrayList<>();
private float price;
public Item(){}
public Item(String title, Author author) {
this.id = UUID.randomUUID();
this.title = title;
this.authors.add(author);
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Person> getAuthors() {
return authors;
}
public void setAuthors(List<Person> authors) {
this.authors = authors;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
@@ -0,0 +1,50 @@
package com.baeldung.jacksonannotation.domain;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Order {
private UUID id;
private Type type;
private int internalAudit;
public static class Type {
public long id;
public String name;
}
public Order() {
this.id = UUID.randomUUID();
}
public Order(Type type) {
this();
this.type = type;
}
public Order(int internalAudit) {
this();
this.type = new Type();
this.type.id = 20;
this.type.name = "Order";
this.internalAudit = internalAudit;
}
public UUID getId() {
return id;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
}
@@ -0,0 +1,44 @@
package com.baeldung.jacksonannotation.domain;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Person {
private UUID id;
private String firstName;
private String lastName;
public Person(){}
public Person(String firstName, String lastName) {
this.id = UUID.randomUUID();
this.firstName = firstName;
this.lastName = lastName;
}
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 UUID getId() {
return id;
}
}
@@ -0,0 +1,32 @@
package com.baeldung.jacksonannotation.general.jsonfilter;
import com.baeldung.jacksonannotation.domain.Item;
import com.baeldung.jacksonannotation.domain.Person;
import com.fasterxml.jackson.annotation.JsonFilter;
import java.util.ArrayList;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
@JsonFilter("authorFilter")
public class Author extends Person {
private List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
@@ -0,0 +1,56 @@
package com.baeldung.jacksonannotation.general.jsonformat;
import com.baeldung.jacksonannotation.domain.Author;
import com.baeldung.jacksonannotation.domain.Item;
import com.fasterxml.jackson.annotation.JsonFormat;
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;
@JsonFormat(
shape = JsonFormat.Shape.STRING,
pattern = "dd-MM-yyyy HH:mm:ss")
private Date published;
private BigDecimal pages;
public Book() {
}
public Book(String title, Author author) {
super(title, author);
}
public String getISBN() {
return ISBN;
}
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}
public Date getPublished() {
return published;
}
public void setPublished(Date published) {
this.published = published;
}
public BigDecimal getPages() {
return pages;
}
public void setPages(BigDecimal pages) {
this.pages = pages;
}
}
@@ -0,0 +1,33 @@
package com.baeldung.jacksonannotation.general.jsonidentityinfo;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import java.util.ArrayList;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Author extends Person {
private List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
@@ -0,0 +1,72 @@
package com.baeldung.jacksonannotation.general.jsonidentityinfo;
import java.util.List;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Course extends Item {
public enum Medium {CLASSROOM, ONLINE}
public enum Level {
BEGINNER("Beginner", 1), INTERMEDIATE("Intermediate", 2), ADVANCED("Advanced", 3);
private String name;
private int number;
Level(String name, int number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
}
private float duration;
private Medium medium;
private Level level;
private List<Course> prerequisite;
public Course(String title, Author author) {
super(title, author);
}
public float getDuration() {
return duration;
}
public void setDuration(float duration) {
this.duration = duration;
}
public Medium getMedium() {
return medium;
}
public void setMedium(Medium medium) {
this.medium = medium;
}
public Level getLevel() {
return level;
}
public void setLevel(Level level) {
this.level = level;
}
public List<Course> getPrerequisite() {
return prerequisite;
}
public void setPrerequisite(List<Course> prerequisite) {
this.prerequisite = prerequisite;
}
}
@@ -0,0 +1,65 @@
package com.baeldung.jacksonannotation.general.jsonidentityinfo;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
public class Item {
private UUID id;
private String title;
private List<Person> authors = new ArrayList<>();
private float price;
public Item(){}
public Item(String title, Author author) {
this.id = UUID.randomUUID();
this.title = title;
this.authors.add(author);
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Person> getAuthors() {
return authors;
}
public void setAuthors(List<Person> authors) {
this.authors = authors;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
@@ -0,0 +1,44 @@
package com.baeldung.jacksonannotation.general.jsonidentityinfo;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Person {
private UUID id;
private String firstName;
private String lastName;
public Person(){}
public Person(String firstName, String lastName) {
this.id = UUID.randomUUID();
this.firstName = firstName;
this.lastName = lastName;
}
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 UUID getId() {
return id;
}
}
@@ -0,0 +1,29 @@
package com.baeldung.jacksonannotation.general.jsonproperty;
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 {
private List<Item> items = new ArrayList<>();
public Author(String firstName, String lastName) {
super(firstName, lastName);
}
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
@@ -0,0 +1,61 @@
package com.baeldung.jacksonannotation.general.jsonproperty;
import com.fasterxml.jackson.annotation.JsonProperty;
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;
private Date published;
private BigDecimal pages;
private String binding;
public Book() {
}
public Book(String title, Author author) {
super(title, author);
}
public String getISBN() {
return ISBN;
}
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}
public Date getPublished() {
return published;
}
public void setPublished(Date published) {
this.published = published;
}
public BigDecimal getPages() {
return pages;
}
public void setPages(BigDecimal pages) {
this.pages = pages;
}
@JsonProperty("binding")
public String coverBinding() {
return binding;
}
@JsonProperty("binding")
public void configureBinding(String binding) {
this.binding = binding;
}
}
@@ -0,0 +1,61 @@
package com.baeldung.jacksonannotation.general.jsonproperty;
import com.baeldung.jacksonannotation.domain.Person;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Item {
private UUID id;
private String title;
private List<Person> authors = new ArrayList<>();
private float price;
public Item(){}
public Item(String title, Author author) {
this.id = UUID.randomUUID();
this.title = title;
this.authors.add(author);
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Person> getAuthors() {
return authors;
}
public void setAuthors(List<Person> authors) {
this.authors = authors;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
@@ -0,0 +1,54 @@
package com.baeldung.jacksonannotation.general.jsonunwrapped;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Order {
private UUID id;
@JsonUnwrapped
private Type type;
private int internalAudit;
public static class Type {
public long id;
public String name;
}
public Order() {
this.id = UUID.randomUUID();
}
public Order(Type type) {
this();
this.type = type;
}
public Order(int internalAudit) {
this();
this.type = new Type();
this.type.id = 20;
this.type.name = "Order";
this.internalAudit = internalAudit;
}
public UUID getId() {
return id;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
}
@@ -0,0 +1,57 @@
package com.baeldung.jacksonannotation.general.jsonview;
import com.fasterxml.jackson.annotation.JsonView;
import java.util.UUID;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Order {
@JsonView(Views.Public.class)
private UUID id;
@JsonView(Views.Public.class)
private Type type;
@JsonView(Views.Internal.class)
private int internalAudit;
public static class Type {
public long id;
public String name;
}
public Order() {
this.id = UUID.randomUUID();
}
public Order(Type type) {
this();
this.type = type;
}
public Order(int internalAudit) {
this();
this.type = new Type();
this.type.id = 20;
this.type.name = "Order";
this.internalAudit = internalAudit;
}
public UUID getId() {
return id;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
}
@@ -0,0 +1,15 @@
package com.baeldung.jacksonannotation.general.jsonview;
/**
* Source code github.com/readlearncode
*
* @author Alex Theedom www.readlearncode.com
* @version 1.0
*/
public class Views {
public static class Public {
}
public static class Internal extends Public {
}
}

Some files were not shown because too many files have changed in this diff Show More