Merge pull request #8227 from catalin-burcea/BAEL-19673
[BAEL-19673] - Move articles out of jackson part 1
This commit is contained in:
commit
543fb9d398
|
@ -0,0 +1,11 @@
|
||||||
|
## Jackson Annotations
|
||||||
|
|
||||||
|
This module contains articles about Jackson annotations.
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Guide to @JsonFormat in Jackson](https://www.baeldung.com/jackson-jsonformat)
|
||||||
|
- [More Jackson Annotations](https://www.baeldung.com/jackson-advanced-annotations)
|
||||||
|
- [Jackson Annotation Examples](https://www.baeldung.com/jackson-annotations)
|
||||||
|
- [Jackson – Bidirectional Relationships](https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion)
|
||||||
|
- [Jackson – Change Name of Field](https://www.baeldung.com/jackson-name-of-property)
|
||||||
|
- [Jackson Ignore Properties on Marshalling](https://www.baeldung.com/jackson-ignore-properties-on-serialization)
|
|
@ -0,0 +1,60 @@
|
||||||
|
<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>
|
||||||
|
<artifactId>jackson-annotations</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>jackson-annotations</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-java</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../parent-java</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>${jackson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||||
|
<artifactId>jackson-dataformat-xml</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>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>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>jackson-annotations</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<assertj.version>3.11.0</assertj.version>
|
||||||
|
<rest-assured.version>3.1.1</rest-assured.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.bidirection;
|
package com.baeldung.jackson.annotation.bidirection;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||||
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
|
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.bidirection;
|
package com.baeldung.jackson.annotation.bidirection;
|
||||||
|
|
||||||
public class ItemWithIgnore {
|
public class ItemWithIgnore {
|
||||||
public int id;
|
public int id;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.bidirection;
|
package com.baeldung.jackson.annotation.bidirection;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.bidirection;
|
package com.baeldung.jackson.annotation.bidirection;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.bidirection;
|
package com.baeldung.jackson.annotation.bidirection;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.bidirection;
|
package com.baeldung.jackson.annotation.bidirection;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.date;
|
package com.baeldung.jackson.annotation.date;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.date;
|
package com.baeldung.jackson.annotation.date;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.date;
|
package com.baeldung.jackson.annotation.date;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.date;
|
package com.baeldung.jackson.annotation.date;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.baeldung.jackson.annotation.deserialization;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.baeldung.jackson.annotation.dtos.ItemWithSerializer;
|
||||||
|
import com.baeldung.jackson.annotation.dtos.User;
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||||
|
import com.fasterxml.jackson.databind.node.IntNode;
|
||||||
|
|
||||||
|
public class ItemDeserializerOnClass extends StdDeserializer<ItemWithSerializer> {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 5579141241817332594L;
|
||||||
|
|
||||||
|
public ItemDeserializerOnClass() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemDeserializerOnClass(final Class<?> vc) {
|
||||||
|
super(vc);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {"id":1,"itemNr":"theItem","owner":2}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ItemWithSerializer deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||||
|
final JsonNode node = jp.getCodec()
|
||||||
|
.readTree(jp);
|
||||||
|
final int id = (Integer) ((IntNode) node.get("id")).numberValue();
|
||||||
|
final String itemName = node.get("itemName")
|
||||||
|
.asText();
|
||||||
|
final int userId = (Integer) ((IntNode) node.get("owner")).numberValue();
|
||||||
|
|
||||||
|
return new ItemWithSerializer(id, itemName, new User(userId, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.jackson.annotation.dtos;
|
||||||
|
|
||||||
|
public class Item {
|
||||||
|
public int id;
|
||||||
|
public String itemName;
|
||||||
|
public User owner;
|
||||||
|
|
||||||
|
public Item() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Item(final int id, final String itemName, final User owner) {
|
||||||
|
this.id = id;
|
||||||
|
this.itemName = itemName;
|
||||||
|
this.owner = owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getItemName() {
|
||||||
|
return itemName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User getOwner() {
|
||||||
|
return owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.baeldung.jackson.annotation.dtos;
|
||||||
|
|
||||||
|
import com.baeldung.jackson.annotation.deserialization.ItemDeserializerOnClass;
|
||||||
|
import com.baeldung.jackson.annotation.serialization.ItemSerializerOnClass;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
|
||||||
|
@JsonSerialize(using = ItemSerializerOnClass.class)
|
||||||
|
@JsonDeserialize(using = ItemDeserializerOnClass.class)
|
||||||
|
public class ItemWithSerializer {
|
||||||
|
public final int id;
|
||||||
|
public final String itemName;
|
||||||
|
public final User owner;
|
||||||
|
|
||||||
|
public ItemWithSerializer(final int id, final String itemName, final User owner) {
|
||||||
|
this.id = id;
|
||||||
|
this.itemName = itemName;
|
||||||
|
this.owner = owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getItemName() {
|
||||||
|
return itemName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User getOwner() {
|
||||||
|
return owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.baeldung.jackson.annotation.dtos;
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
public int id;
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
public User() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(final int id, final String name) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.baeldung.jackson.annotation.dtos.withEnum;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonValue;
|
||||||
|
|
||||||
|
public enum DistanceEnumWithValue {
|
||||||
|
KILOMETER("km", 1000), MILE("miles", 1609.34), METER("meters", 1), INCH("inches", 0.0254), CENTIMETER("cm", 0.01), MILLIMETER("mm", 0.001);
|
||||||
|
|
||||||
|
private String unit;
|
||||||
|
private final double meters;
|
||||||
|
|
||||||
|
private DistanceEnumWithValue(String unit, double meters) {
|
||||||
|
this.unit = unit;
|
||||||
|
this.meters = meters;
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonValue
|
||||||
|
public double getMeters() {
|
||||||
|
return meters;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUnit() {
|
||||||
|
return unit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUnit(String unit) {
|
||||||
|
this.unit = unit;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.exception;
|
package com.baeldung.jackson.annotation.exception;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.exception;
|
package com.baeldung.jackson.annotation.exception;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.jsonview;
|
package com.baeldung.jackson.annotation.jsonview;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonView;
|
import com.fasterxml.jackson.annotation.JsonView;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.jsonview;
|
package com.baeldung.jackson.annotation.jsonview;
|
||||||
|
|
||||||
public class Views {
|
public class Views {
|
||||||
public static class Public {
|
public static class Public {
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.jackson.annotation.serialization;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.baeldung.jackson.annotation.dtos.Item;
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
|
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||||
|
|
||||||
|
public class ItemSerializer extends StdSerializer<Item> {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 6739170890621978901L;
|
||||||
|
|
||||||
|
public ItemSerializer() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemSerializer(final Class<Item> t) {
|
||||||
|
super(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void serialize(final Item value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||||
|
jgen.writeStartObject();
|
||||||
|
jgen.writeNumberField("id", value.id);
|
||||||
|
jgen.writeStringField("itemName", value.itemName);
|
||||||
|
jgen.writeNumberField("owner", value.owner.id);
|
||||||
|
jgen.writeEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.jackson.annotation.serialization;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.baeldung.jackson.annotation.dtos.ItemWithSerializer;
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
|
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||||
|
|
||||||
|
public class ItemSerializerOnClass extends StdSerializer<ItemWithSerializer> {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -1760959597313610409L;
|
||||||
|
|
||||||
|
public ItemSerializerOnClass() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ItemSerializerOnClass(final Class<ItemWithSerializer> t) {
|
||||||
|
super(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void serialize(final ItemWithSerializer value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||||
|
jgen.writeStartObject();
|
||||||
|
jgen.writeNumberField("id", value.id);
|
||||||
|
jgen.writeStringField("itemName", value.itemName);
|
||||||
|
jgen.writeNumberField("owner", value.owner.id);
|
||||||
|
jgen.writeEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package com.baeldung.jackson.bidirection;
|
package com.baeldung.jackson.bidirection;
|
||||||
|
|
||||||
import com.baeldung.jackson.jsonview.Views;
|
import com.baeldung.jackson.bidirection.jsonview.Views;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonView;
|
import com.fasterxml.jackson.annotation.JsonView;
|
||||||
|
|
|
@ -3,7 +3,7 @@ package com.baeldung.jackson.bidirection;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.baeldung.jackson.jsonview.Views;
|
import com.baeldung.jackson.bidirection.jsonview.Views;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonView;
|
import com.fasterxml.jackson.annotation.JsonView;
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung.jackson.bidirection.jsonview;
|
||||||
|
|
||||||
|
public class Views {
|
||||||
|
public static class Public {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Internal extends Public {
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.baeldung.jackson.domain;
|
||||||
|
|
||||||
|
public class Person {
|
||||||
|
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
public Person(String firstName, String lastName) {
|
||||||
|
super();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.annotation.extra;
|
package com.baeldung.jackson.advancedannotations;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.containsString;
|
import static org.hamcrest.CoreMatchers.containsString;
|
||||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||||
|
@ -12,10 +12,10 @@ import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.baeldung.jackson.annotation.extra.AppendBeans.BeanWithAppend;
|
import com.baeldung.jackson.advancedannotations.AppendBeans.BeanWithAppend;
|
||||||
import com.baeldung.jackson.annotation.extra.AppendBeans.BeanWithoutAppend;
|
import com.baeldung.jackson.advancedannotations.AppendBeans.BeanWithoutAppend;
|
||||||
import com.baeldung.jackson.annotation.extra.IdentityReferenceBeans.BeanWithIdentityReference;
|
import com.baeldung.jackson.advancedannotations.IdentityReferenceBeans.BeanWithIdentityReference;
|
||||||
import com.baeldung.jackson.annotation.extra.IdentityReferenceBeans.BeanWithoutIdentityReference;
|
import com.baeldung.jackson.advancedannotations.IdentityReferenceBeans.BeanWithoutIdentityReference;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
|
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
|
||||||
|
@ -23,7 +23,7 @@ import com.fasterxml.jackson.databind.ObjectWriter;
|
||||||
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
|
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
|
||||||
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;
|
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;
|
||||||
|
|
||||||
public class ExtraAnnotationUnitTest {
|
public class AdvancedAnnotationsUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void whenNotUsingJsonIdentityReferenceAnnotation_thenCorrect() throws JsonProcessingException {
|
public void whenNotUsingJsonIdentityReferenceAnnotation_thenCorrect() throws JsonProcessingException {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.annotation.extra;
|
package com.baeldung.jackson.advancedannotations;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonAppend;
|
import com.fasterxml.jackson.databind.annotation.JsonAppend;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.annotation.extra;
|
package com.baeldung.jackson.advancedannotations;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
|
||||||
import com.fasterxml.jackson.annotation.JsonIdentityReference;
|
import com.fasterxml.jackson.annotation.JsonIdentityReference;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.annotation.extra;
|
package com.baeldung.jackson.advancedannotations;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.annotation.extra;
|
package com.baeldung.jackson.advancedannotations;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
|
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.annotation.extra;
|
package com.baeldung.jackson.advancedannotations;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
|
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.annotation.extra;
|
package com.baeldung.jackson.advancedannotations;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonTypeId;
|
import com.fasterxml.jackson.annotation.JsonTypeId;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.annotation.extra;
|
package com.baeldung.jackson.advancedannotations;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.test;
|
package com.baeldung.jackson.annotation;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
|
@ -14,32 +14,18 @@ import java.util.TimeZone;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.jackson.annotation.AliasBean;
|
import com.baeldung.jackson.annotation.bidirection.ItemWithIdentity;
|
||||||
import com.baeldung.jackson.annotation.BeanWithCreator;
|
import com.baeldung.jackson.annotation.bidirection.ItemWithRef;
|
||||||
import com.baeldung.jackson.annotation.BeanWithCustomAnnotation;
|
import com.baeldung.jackson.annotation.bidirection.UserWithIdentity;
|
||||||
import com.baeldung.jackson.annotation.BeanWithFilter;
|
import com.baeldung.jackson.annotation.bidirection.UserWithRef;
|
||||||
import com.baeldung.jackson.annotation.BeanWithGetter;
|
import com.baeldung.jackson.annotation.date.EventWithFormat;
|
||||||
import com.baeldung.jackson.annotation.BeanWithIgnore;
|
import com.baeldung.jackson.annotation.date.EventWithSerializer;
|
||||||
import com.baeldung.jackson.annotation.BeanWithInject;
|
import com.baeldung.jackson.ignore.dtos.MyMixInForIgnoreType;
|
||||||
import com.baeldung.jackson.annotation.ExtendableBean;
|
import com.baeldung.jackson.annotation.dtos.withEnum.DistanceEnumWithValue;
|
||||||
import com.baeldung.jackson.annotation.MyBean;
|
import com.baeldung.jackson.annotation.exception.UserWithRoot;
|
||||||
import com.baeldung.jackson.annotation.PrivateBean;
|
import com.baeldung.jackson.annotation.exception.UserWithRootNamespace;
|
||||||
import com.baeldung.jackson.annotation.RawBean;
|
import com.baeldung.jackson.annotation.jsonview.Item;
|
||||||
import com.baeldung.jackson.annotation.UnwrappedUser;
|
import com.baeldung.jackson.annotation.jsonview.Views;
|
||||||
import com.baeldung.jackson.annotation.UserWithIgnoreType;
|
|
||||||
import com.baeldung.jackson.annotation.Zoo;
|
|
||||||
import com.baeldung.jackson.bidirection.ItemWithIdentity;
|
|
||||||
import com.baeldung.jackson.bidirection.ItemWithRef;
|
|
||||||
import com.baeldung.jackson.bidirection.UserWithIdentity;
|
|
||||||
import com.baeldung.jackson.bidirection.UserWithRef;
|
|
||||||
import com.baeldung.jackson.date.EventWithFormat;
|
|
||||||
import com.baeldung.jackson.date.EventWithSerializer;
|
|
||||||
import com.baeldung.jackson.dtos.MyMixInForIgnoreType;
|
|
||||||
import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithValue;
|
|
||||||
import com.baeldung.jackson.exception.UserWithRoot;
|
|
||||||
import com.baeldung.jackson.exception.UserWithRootNamespace;
|
|
||||||
import com.baeldung.jackson.jsonview.Item;
|
|
||||||
import com.baeldung.jackson.jsonview.Views;
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.InjectableValues;
|
import com.fasterxml.jackson.databind.InjectableValues;
|
||||||
import com.fasterxml.jackson.databind.MapperFeature;
|
import com.fasterxml.jackson.databind.MapperFeature;
|
||||||
|
@ -352,13 +338,13 @@ public class JacksonAnnotationUnitTest {
|
||||||
// @Ignore("Jackson 2.7.1-1 seems to have changed the API regarding mixins")
|
// @Ignore("Jackson 2.7.1-1 seems to have changed the API regarding mixins")
|
||||||
@Test
|
@Test
|
||||||
public void whenSerializingUsingMixInAnnotation_thenCorrect() throws JsonProcessingException {
|
public void whenSerializingUsingMixInAnnotation_thenCorrect() throws JsonProcessingException {
|
||||||
final com.baeldung.jackson.dtos.Item item = new com.baeldung.jackson.dtos.Item(1, "book", null);
|
final com.baeldung.jackson.annotation.dtos.Item item = new com.baeldung.jackson.annotation.dtos.Item(1, "book", null);
|
||||||
|
|
||||||
String result = new ObjectMapper().writeValueAsString(item);
|
String result = new ObjectMapper().writeValueAsString(item);
|
||||||
assertThat(result, containsString("owner"));
|
assertThat(result, containsString("owner"));
|
||||||
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
mapper.addMixIn(com.baeldung.jackson.dtos.User.class, MyMixInForIgnoreType.class);
|
mapper.addMixIn(com.baeldung.jackson.annotation.dtos.User.class, MyMixInForIgnoreType.class);
|
||||||
|
|
||||||
result = mapper.writeValueAsString(item);
|
result = mapper.writeValueAsString(item);
|
||||||
assertThat(result, not(containsString("owner")));
|
assertThat(result, not(containsString("owner")));
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.test;
|
package com.baeldung.jackson.bidirection;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.hamcrest.Matchers.not;
|
import static org.hamcrest.Matchers.not;
|
||||||
|
@ -9,19 +9,7 @@ import java.io.IOException;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.jackson.bidirection.Item;
|
import com.baeldung.jackson.bidirection.jsonview.Views;
|
||||||
import com.baeldung.jackson.bidirection.ItemWithIdentity;
|
|
||||||
import com.baeldung.jackson.bidirection.ItemWithIgnore;
|
|
||||||
import com.baeldung.jackson.bidirection.ItemWithRef;
|
|
||||||
import com.baeldung.jackson.bidirection.ItemWithSerializer;
|
|
||||||
import com.baeldung.jackson.bidirection.ItemWithView;
|
|
||||||
import com.baeldung.jackson.bidirection.User;
|
|
||||||
import com.baeldung.jackson.bidirection.UserWithIdentity;
|
|
||||||
import com.baeldung.jackson.bidirection.UserWithIgnore;
|
|
||||||
import com.baeldung.jackson.bidirection.UserWithRef;
|
|
||||||
import com.baeldung.jackson.bidirection.UserWithSerializer;
|
|
||||||
import com.baeldung.jackson.bidirection.UserWithView;
|
|
||||||
import com.baeldung.jackson.jsonview.Views;
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
@ -10,15 +10,15 @@ import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.jackson.dtos.MyDto;
|
import com.baeldung.jackson.ignore.dtos.MyDto;
|
||||||
import com.baeldung.jackson.dtos.MyDtoIncludeNonDefault;
|
import com.baeldung.jackson.ignore.dtos.MyDtoIncludeNonDefault;
|
||||||
import com.baeldung.jackson.dtos.MyDtoWithFilter;
|
import com.baeldung.jackson.ignore.dtos.MyDtoWithFilter;
|
||||||
import com.baeldung.jackson.dtos.MyDtoWithSpecialField;
|
import com.baeldung.jackson.ignore.dtos.MyDtoWithSpecialField;
|
||||||
import com.baeldung.jackson.dtos.MyMixInForIgnoreType;
|
import com.baeldung.jackson.ignore.dtos.MyMixInForIgnoreType;
|
||||||
import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreField;
|
import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreField;
|
||||||
import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreFieldByName;
|
import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreFieldByName;
|
||||||
import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreNull;
|
import com.baeldung.jackson.ignore.dtos.MyDtoIgnoreNull;
|
||||||
import com.baeldung.jackson.serialization.MyDtoNullKeySerializer;
|
import com.baeldung.jackson.ignore.dtos.MyDtoNullKeySerializer;
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
import com.fasterxml.jackson.core.JsonParseException;
|
import com.fasterxml.jackson.core.JsonParseException;
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.baeldung.jackson.ignore.dtos;
|
||||||
|
|
||||||
|
public class MyDto {
|
||||||
|
|
||||||
|
private String stringValue;
|
||||||
|
private int intValue;
|
||||||
|
private boolean booleanValue;
|
||||||
|
|
||||||
|
public MyDto() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyDto(final String stringValue, final int intValue, final boolean booleanValue) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.stringValue = stringValue;
|
||||||
|
this.intValue = intValue;
|
||||||
|
this.booleanValue = booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
public String getStringValue() {
|
||||||
|
return stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStringValue(final String stringValue) {
|
||||||
|
this.stringValue = stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIntValue() {
|
||||||
|
return intValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIntValue(final int intValue) {
|
||||||
|
this.intValue = intValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBooleanValue() {
|
||||||
|
return booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBooleanValue(final boolean booleanValue) {
|
||||||
|
this.booleanValue = booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.baeldung.jackson.ignore.dtos;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||||
|
|
||||||
|
@JsonInclude(Include.NON_DEFAULT)
|
||||||
|
public class MyDtoIncludeNonDefault {
|
||||||
|
|
||||||
|
private String stringValue;
|
||||||
|
private int intValue;
|
||||||
|
private boolean booleanValue;
|
||||||
|
|
||||||
|
public MyDtoIncludeNonDefault() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
public String getStringValue() {
|
||||||
|
return stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStringValue(final String stringValue) {
|
||||||
|
this.stringValue = stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIntValue() {
|
||||||
|
return intValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIntValue(final int intValue) {
|
||||||
|
this.intValue = intValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBooleanValue() {
|
||||||
|
return booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBooleanValue(final boolean booleanValue) {
|
||||||
|
this.booleanValue = booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,27 +1,27 @@
|
||||||
package com.baeldung.jackson.serialization;
|
package com.baeldung.jackson.ignore.dtos;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||||
|
|
||||||
public class MyDtoNullKeySerializer extends StdSerializer<Object> {
|
public class MyDtoNullKeySerializer extends StdSerializer<Object> {
|
||||||
|
|
||||||
private static final long serialVersionUID = -4478531309177369056L;
|
private static final long serialVersionUID = -4478531309177369056L;
|
||||||
|
|
||||||
public MyDtoNullKeySerializer() {
|
public MyDtoNullKeySerializer() {
|
||||||
this(null);
|
this(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public MyDtoNullKeySerializer(final Class<Object> t) {
|
public MyDtoNullKeySerializer(final Class<Object> t) {
|
||||||
super(t);
|
super(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void serialize(final Object value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
public void serialize(final Object value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonProcessingException {
|
||||||
jgen.writeFieldName("");
|
jgen.writeFieldName("");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
package com.baeldung.jackson.ignore.dtos;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||||
|
|
||||||
|
@JsonFilter("myFilter")
|
||||||
|
public class MyDtoWithFilter {
|
||||||
|
|
||||||
|
private String stringValue;
|
||||||
|
private int intValue;
|
||||||
|
private boolean booleanValue;
|
||||||
|
|
||||||
|
public MyDtoWithFilter() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyDtoWithFilter(final String stringValue, final int intValue, final boolean booleanValue) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.stringValue = stringValue;
|
||||||
|
this.intValue = intValue;
|
||||||
|
this.booleanValue = booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
public String getStringValue() {
|
||||||
|
return stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStringValue(final String stringValue) {
|
||||||
|
this.stringValue = stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIntValue() {
|
||||||
|
return intValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIntValue(final int intValue) {
|
||||||
|
this.intValue = intValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBooleanValue() {
|
||||||
|
return booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBooleanValue(final boolean booleanValue) {
|
||||||
|
this.booleanValue = booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.baeldung.jackson.ignore.dtos;
|
||||||
|
|
||||||
|
public class MyDtoWithSpecialField {
|
||||||
|
|
||||||
|
private String[] stringValue;
|
||||||
|
private int intValue;
|
||||||
|
private boolean booleanValue;
|
||||||
|
|
||||||
|
public MyDtoWithSpecialField() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyDtoWithSpecialField(final String[] stringValue, final int intValue, final boolean booleanValue) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.stringValue = stringValue;
|
||||||
|
this.intValue = intValue;
|
||||||
|
this.booleanValue = booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
public String[] getStringValue() {
|
||||||
|
return stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStringValue(final String[] stringValue) {
|
||||||
|
this.stringValue = stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIntValue() {
|
||||||
|
return intValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIntValue(final int intValue) {
|
||||||
|
this.intValue = intValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBooleanValue() {
|
||||||
|
return booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBooleanValue(final boolean booleanValue) {
|
||||||
|
this.booleanValue = booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.baeldung.jackson.ignore.dtos;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnoreType;
|
||||||
|
|
||||||
|
@JsonIgnoreType
|
||||||
|
public class MyMixInForIgnoreType {
|
||||||
|
//
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.baeldung.jackson.jsonproperty;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.hamcrest.Matchers.not;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||||
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerationException;
|
||||||
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
|
|
||||||
|
public class JsonPropertyUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
final String dtoAsString = mapper.writeValueAsString(new MyDto());
|
||||||
|
|
||||||
|
assertThat(dtoAsString, containsString("intValue"));
|
||||||
|
assertThat(dtoAsString, containsString("stringValue"));
|
||||||
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenNameOfFieldIsChangedViaAnnotationOnGetter_whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
||||||
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
final MyDtoFieldNameChanged dtoObject = new MyDtoFieldNameChanged();
|
||||||
|
dtoObject.setStringValue("a");
|
||||||
|
|
||||||
|
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
||||||
|
|
||||||
|
assertThat(dtoAsString, not(containsString("stringValue")));
|
||||||
|
assertThat(dtoAsString, containsString("strVal"));
|
||||||
|
System.out.println(dtoAsString);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.baeldung.jackson.jsonproperty;
|
||||||
|
|
||||||
|
public class MyDto {
|
||||||
|
|
||||||
|
private String stringValue;
|
||||||
|
private int intValue;
|
||||||
|
private boolean booleanValue;
|
||||||
|
|
||||||
|
public MyDto() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyDto(final String stringValue, final int intValue, final boolean booleanValue) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.stringValue = stringValue;
|
||||||
|
this.intValue = intValue;
|
||||||
|
this.booleanValue = booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
public String getStringValue() {
|
||||||
|
return stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStringValue(final String stringValue) {
|
||||||
|
this.stringValue = stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIntValue() {
|
||||||
|
return intValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIntValue(final int intValue) {
|
||||||
|
this.intValue = intValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isBooleanValue() {
|
||||||
|
return booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBooleanValue(final boolean booleanValue) {
|
||||||
|
this.booleanValue = booleanValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "MyDto [stringValue=" + stringValue + ", intValue=" + intValue + ", booleanValue=" + booleanValue + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.dtos;
|
package com.baeldung.jackson.jsonproperty;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
## Jackson Exceptions
|
||||||
|
|
||||||
|
This module contains articles about Jackson exceptions.
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Jackson Exceptions – Problems and Solutions](https://www.baeldung.com/jackson-exception)
|
||||||
|
- [Jackson – JsonMappingException (No serializer found for class)](https://www.baeldung.com/jackson-jsonmappingexception)
|
|
@ -0,0 +1,33 @@
|
||||||
|
<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>
|
||||||
|
<artifactId>jackson-exceptions</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>jackson-exceptions</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-java</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../parent-java</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
<version>${jackson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>jackson-exceptions</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.baeldung.exceptions;
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
public int id;
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
public User() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public User(final int id, final String name) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.exception;
|
package com.baeldung.exceptions;
|
||||||
|
|
||||||
public class UserWithConflict {
|
public class UserWithConflict {
|
||||||
public int id;
|
public int id;
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.baeldung.exceptions;
|
||||||
|
|
||||||
|
public class UserWithNoDefaultConstructor {
|
||||||
|
|
||||||
|
private int id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public UserWithNoDefaultConstructor(final int id, final String name) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.exception;
|
package com.baeldung.exceptions;
|
||||||
|
|
||||||
public class UserWithPrivateFields {
|
public class UserWithPrivateFields {
|
||||||
int id;
|
int id;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.exception;
|
package com.baeldung.exceptions;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonRootName;
|
import com.fasterxml.jackson.annotation.JsonRootName;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.exception;
|
package com.baeldung.exceptions;
|
||||||
|
|
||||||
public class Zoo {
|
public class Zoo {
|
||||||
public Animal animal;
|
public Animal animal;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.exception;
|
package com.baeldung.exceptions;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.dtos;
|
package com.baeldung.mappingexception;
|
||||||
|
|
||||||
public class MyDtoNoAccessors {
|
public class MyDtoNoAccessors {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.dtos;
|
package com.baeldung.mappingexception;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||||
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.exception.test;
|
package com.baeldung.exceptions;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
@ -9,11 +9,11 @@ import java.util.List;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.jackson.exception.User;
|
import com.baeldung.exceptions.User;
|
||||||
import com.baeldung.jackson.exception.UserWithPrivateFields;
|
import com.baeldung.exceptions.UserWithPrivateFields;
|
||||||
import com.baeldung.jackson.exception.UserWithRoot;
|
import com.baeldung.exceptions.UserWithRoot;
|
||||||
import com.baeldung.jackson.exception.Zoo;
|
import com.baeldung.exceptions.Zoo;
|
||||||
import com.baeldung.jackson.exception.ZooConfigured;
|
import com.baeldung.exceptions.ZooConfigured;
|
||||||
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||||
import com.fasterxml.jackson.core.JsonFactory;
|
import com.fasterxml.jackson.core.JsonFactory;
|
||||||
|
@ -78,7 +78,7 @@ public class JacksonExceptionsUnitTest {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
mapper.reader()
|
mapper.reader()
|
||||||
.forType(User.class)
|
.forType(UserWithNoDefaultConstructor.class)
|
||||||
.readValue(json);
|
.readValue(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,8 +87,8 @@ public class JacksonExceptionsUnitTest {
|
||||||
final String json = "{\"id\":1,\"name\":\"John\"}";
|
final String json = "{\"id\":1,\"name\":\"John\"}";
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
final com.baeldung.jackson.dtos.User user = mapper.reader()
|
final User user = mapper.reader()
|
||||||
.forType(com.baeldung.jackson.dtos.User.class)
|
.forType(User.class)
|
||||||
.readValue(json);
|
.readValue(json);
|
||||||
assertEquals("John", user.name);
|
assertEquals("John", user.name);
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ public class JacksonExceptionsUnitTest {
|
||||||
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
|
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
|
||||||
|
|
||||||
mapper.reader()
|
mapper.reader()
|
||||||
.forType(com.baeldung.jackson.dtos.User.class)
|
.forType(User.class)
|
||||||
.readValue(json);
|
.readValue(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,7 +126,7 @@ public class JacksonExceptionsUnitTest {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
mapper.reader()
|
mapper.reader()
|
||||||
.forType(com.baeldung.jackson.dtos.User.class)
|
.forType(User.class)
|
||||||
.readValue(json);
|
.readValue(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,8 +135,8 @@ public class JacksonExceptionsUnitTest {
|
||||||
final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]";
|
final String json = "[{\"id\":1,\"name\":\"John\"},{\"id\":2,\"name\":\"Adam\"}]";
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
final List<com.baeldung.jackson.dtos.User> users = mapper.reader()
|
final List<User> users = mapper.reader()
|
||||||
.forType(new TypeReference<List<com.baeldung.jackson.dtos.User>>() {
|
.forType(new TypeReference<List<User>>() {
|
||||||
})
|
})
|
||||||
.readValue(json);
|
.readValue(json);
|
||||||
|
|
||||||
|
@ -150,7 +150,7 @@ public class JacksonExceptionsUnitTest {
|
||||||
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
mapper.reader()
|
mapper.reader()
|
||||||
.forType(com.baeldung.jackson.dtos.User.class)
|
.forType(User.class)
|
||||||
.readValue(json);
|
.readValue(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,8 +161,8 @@ public class JacksonExceptionsUnitTest {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||||
|
|
||||||
final com.baeldung.jackson.dtos.User user = mapper.reader()
|
final User user = mapper.reader()
|
||||||
.forType(com.baeldung.jackson.dtos.User.class)
|
.forType(User.class)
|
||||||
.readValue(json);
|
.readValue(json);
|
||||||
assertEquals("John", user.name);
|
assertEquals("John", user.name);
|
||||||
}
|
}
|
||||||
|
@ -174,7 +174,7 @@ public class JacksonExceptionsUnitTest {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
mapper.reader()
|
mapper.reader()
|
||||||
.forType(com.baeldung.jackson.dtos.User.class)
|
.forType(User.class)
|
||||||
.readValue(json);
|
.readValue(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,8 +186,8 @@ public class JacksonExceptionsUnitTest {
|
||||||
factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
|
factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
|
||||||
final ObjectMapper mapper = new ObjectMapper(factory);
|
final ObjectMapper mapper = new ObjectMapper(factory);
|
||||||
|
|
||||||
final com.baeldung.jackson.dtos.User user = mapper.reader()
|
final User user = mapper.reader()
|
||||||
.forType(com.baeldung.jackson.dtos.User.class)
|
.forType(User.class)
|
||||||
.readValue(json);
|
.readValue(json);
|
||||||
assertEquals("John", user.name);
|
assertEquals("John", user.name);
|
||||||
}
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.baeldung.mappingexception;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.hamcrest.Matchers.not;
|
||||||
|
import static org.hamcrest.Matchers.notNullValue;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
||||||
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerationException;
|
||||||
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
|
public class JacksonMappingExceptionUnitTest {
|
||||||
|
|
||||||
|
@Test(expected = JsonMappingException.class)
|
||||||
|
public final void givenObjectHasNoAccessors_whenSerializing_thenException() throws JsonParseException, IOException {
|
||||||
|
final String dtoAsString = new ObjectMapper().writeValueAsString(new MyDtoNoAccessors());
|
||||||
|
|
||||||
|
assertThat(dtoAsString, notNullValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenObjectHasNoAccessors_whenSerializingWithPrivateFieldsVisibility_thenNoException() throws JsonParseException, IOException {
|
||||||
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
|
||||||
|
final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessors());
|
||||||
|
|
||||||
|
assertThat(dtoAsString, containsString("intValue"));
|
||||||
|
assertThat(dtoAsString, containsString("stringValue"));
|
||||||
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenObjectHasNoAccessorsButHasVisibleFields_whenSerializing_thenNoException() throws JsonParseException, IOException {
|
||||||
|
final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessorsAndFieldVisibility());
|
||||||
|
|
||||||
|
assertThat(dtoAsString, containsString("intValue"));
|
||||||
|
assertThat(dtoAsString, containsString("stringValue"));
|
||||||
|
assertThat(dtoAsString, containsString("booleanValue"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,9 +7,6 @@ This module contains articles about Jackson that are also part of the Jackson Eb
|
||||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Jackson Ignore Properties on Marshalling](https://www.baeldung.com/jackson-ignore-properties-on-serialization)
|
|
||||||
- [Jackson Unmarshalling json with Unknown Properties](https://www.baeldung.com/jackson-deserialize-json-unknown-properties)
|
- [Jackson Unmarshalling json with Unknown Properties](https://www.baeldung.com/jackson-deserialize-json-unknown-properties)
|
||||||
- [Jackson Annotation Examples](https://www.baeldung.com/jackson-annotations)
|
|
||||||
- [Intro to the Jackson ObjectMapper](https://www.baeldung.com/jackson-object-mapper-tutorial)
|
- [Intro to the Jackson ObjectMapper](https://www.baeldung.com/jackson-object-mapper-tutorial)
|
||||||
- [Ignore Null Fields with Jackson](https://www.baeldung.com/jackson-ignore-null-fields)
|
- [Ignore Null Fields with Jackson](https://www.baeldung.com/jackson-ignore-null-fields)
|
||||||
- [Jackson – Change Name of Field](https://www.baeldung.com/jackson-name-of-property)
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.jackson.dtos.withEnum;
|
package com.baeldung.jackson.annotation.dtos.withEnum;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonValue;
|
import com.fasterxml.jackson.annotation.JsonValue;
|
||||||
|
|
||||||
|
|
|
@ -8,13 +8,10 @@ import static org.junit.Assert.assertThat;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.baeldung.jackson.dtos.MyDtoFieldNameChanged;
|
|
||||||
import com.baeldung.jackson.dtos.User;
|
import com.baeldung.jackson.dtos.User;
|
||||||
import com.baeldung.jackson.dtos.Item;
|
import com.baeldung.jackson.dtos.Item;
|
||||||
import com.baeldung.jackson.dtos.ItemWithSerializer;
|
import com.baeldung.jackson.dtos.ItemWithSerializer;
|
||||||
import com.baeldung.jackson.dtos.MyDto;
|
import com.baeldung.jackson.dtos.MyDto;
|
||||||
import com.baeldung.jackson.dtos.MyDtoNoAccessors;
|
|
||||||
import com.baeldung.jackson.dtos.MyDtoNoAccessorsAndFieldVisibility;
|
|
||||||
import com.baeldung.jackson.serialization.ItemSerializer;
|
import com.baeldung.jackson.serialization.ItemSerializer;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -29,61 +26,6 @@ import com.google.common.collect.Lists;
|
||||||
|
|
||||||
public class JacksonSerializationUnitTest {
|
public class JacksonSerializationUnitTest {
|
||||||
|
|
||||||
// tests - single entity to json
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
|
||||||
final String dtoAsString = mapper.writeValueAsString(new MyDto());
|
|
||||||
|
|
||||||
assertThat(dtoAsString, containsString("intValue"));
|
|
||||||
assertThat(dtoAsString, containsString("stringValue"));
|
|
||||||
assertThat(dtoAsString, containsString("booleanValue"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void givenNameOfFieldIsChangedViaAnnotationOnGetter_whenSerializing_thenCorrect() throws JsonParseException, IOException {
|
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
|
||||||
final MyDtoFieldNameChanged dtoObject = new MyDtoFieldNameChanged();
|
|
||||||
dtoObject.setStringValue("a");
|
|
||||||
|
|
||||||
final String dtoAsString = mapper.writeValueAsString(dtoObject);
|
|
||||||
|
|
||||||
assertThat(dtoAsString, not(containsString("stringValue")));
|
|
||||||
assertThat(dtoAsString, containsString("strVal"));
|
|
||||||
System.out.println(dtoAsString);
|
|
||||||
}
|
|
||||||
|
|
||||||
// tests - serialize via accessors/fields
|
|
||||||
|
|
||||||
@Test(expected = JsonMappingException.class)
|
|
||||||
public final void givenObjectHasNoAccessors_whenSerializing_thenException() throws JsonParseException, IOException {
|
|
||||||
final String dtoAsString = new ObjectMapper().writeValueAsString(new MyDtoNoAccessors());
|
|
||||||
|
|
||||||
assertThat(dtoAsString, notNullValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void givenObjectHasNoAccessors_whenSerializingWithPrivateFieldsVisibility_thenNoException() throws JsonParseException, IOException {
|
|
||||||
final ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
|
|
||||||
final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessors());
|
|
||||||
|
|
||||||
assertThat(dtoAsString, containsString("intValue"));
|
|
||||||
assertThat(dtoAsString, containsString("stringValue"));
|
|
||||||
assertThat(dtoAsString, containsString("booleanValue"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public final void givenObjectHasNoAccessorsButHasVisibleFields_whenSerializing_thenNoException() throws JsonParseException, IOException {
|
|
||||||
final ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
final String dtoAsString = objectMapper.writeValueAsString(new MyDtoNoAccessorsAndFieldVisibility());
|
|
||||||
|
|
||||||
assertThat(dtoAsString, containsString("intValue"));
|
|
||||||
assertThat(dtoAsString, containsString("stringValue"));
|
|
||||||
assertThat(dtoAsString, containsString("booleanValue"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// tests - multiple entities to json
|
// tests - multiple entities to json
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -11,21 +11,16 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||||
- [Jackson – Unmarshall to Collection/Array](https://www.baeldung.com/jackson-collection-array)
|
- [Jackson – Unmarshall to Collection/Array](https://www.baeldung.com/jackson-collection-array)
|
||||||
- [Jackson – Custom Serializer](https://www.baeldung.com/jackson-custom-serialization)
|
- [Jackson – Custom Serializer](https://www.baeldung.com/jackson-custom-serialization)
|
||||||
- [Getting Started with Custom Deserialization in Jackson](https://www.baeldung.com/jackson-deserialization)
|
- [Getting Started with Custom Deserialization in Jackson](https://www.baeldung.com/jackson-deserialization)
|
||||||
- [Jackson Exceptions – Problems and Solutions](https://www.baeldung.com/jackson-exception)
|
|
||||||
- [Jackson Date](https://www.baeldung.com/jackson-serialize-dates)
|
- [Jackson Date](https://www.baeldung.com/jackson-serialize-dates)
|
||||||
- [Jackson – Bidirectional Relationships](https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion)
|
|
||||||
- [Jackson JSON Tutorial](https://www.baeldung.com/jackson)
|
- [Jackson JSON Tutorial](https://www.baeldung.com/jackson)
|
||||||
- [Jackson – Working with Maps and nulls](https://www.baeldung.com/jackson-map-null-values-or-null-key)
|
- [Jackson – Working with Maps and nulls](https://www.baeldung.com/jackson-map-null-values-or-null-key)
|
||||||
- [Jackson – Decide What Fields Get Serialized/Deserialized](https://www.baeldung.com/jackson-field-serializable-deserializable-or-not)
|
- [Jackson – Decide What Fields Get Serialized/Deserialized](https://www.baeldung.com/jackson-field-serializable-deserializable-or-not)
|
||||||
- [Jackson vs Gson](https://www.baeldung.com/jackson-vs-gson)
|
- [Jackson vs Gson](https://www.baeldung.com/jackson-vs-gson)
|
||||||
- [XML Serialization and Deserialization with Jackson](https://www.baeldung.com/jackson-xml-serialization-and-deserialization)
|
- [XML Serialization and Deserialization with Jackson](https://www.baeldung.com/jackson-xml-serialization-and-deserialization)
|
||||||
- [More Jackson Annotations](https://www.baeldung.com/jackson-advanced-annotations)
|
|
||||||
- [Inheritance with Jackson](https://www.baeldung.com/jackson-inheritance)
|
- [Inheritance with Jackson](https://www.baeldung.com/jackson-inheritance)
|
||||||
- [Guide to @JsonFormat in Jackson](https://www.baeldung.com/jackson-jsonformat)
|
|
||||||
- [Using Optional with Jackson](https://www.baeldung.com/jackson-optional)
|
- [Using Optional with Jackson](https://www.baeldung.com/jackson-optional)
|
||||||
- [Map Serialization and Deserialization with Jackson](https://www.baeldung.com/jackson-map)
|
- [Map Serialization and Deserialization with Jackson](https://www.baeldung.com/jackson-map)
|
||||||
- [Jackson Streaming API](https://www.baeldung.com/jackson-streaming-api)
|
- [Jackson Streaming API](https://www.baeldung.com/jackson-streaming-api)
|
||||||
- [Jackson – JsonMappingException (No serializer found for class)](https://www.baeldung.com/jackson-jsonmappingexception)
|
|
||||||
- [How To Serialize and Deserialize Enums with Jackson](https://www.baeldung.com/jackson-serialize-enums)
|
- [How To Serialize and Deserialize Enums with Jackson](https://www.baeldung.com/jackson-serialize-enums)
|
||||||
- [Jackson – Marshall String to JsonNode](https://www.baeldung.com/jackson-json-to-jsonnode)
|
- [Jackson – Marshall String to JsonNode](https://www.baeldung.com/jackson-json-to-jsonnode)
|
||||||
- [Jackson – Unmarshall to Collection/Array](https://www.baeldung.com/jackson-collection-array)
|
- [Jackson – Unmarshall to Collection/Array](https://www.baeldung.com/jackson-collection-array)
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
package com.baeldung.jackson.exception;
|
|
||||||
|
|
||||||
public class User {
|
|
||||||
public int id;
|
|
||||||
public String name;
|
|
||||||
|
|
||||||
public User(final int id, final String name) {
|
|
||||||
this.id = id;
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
}
|
|
4
pom.xml
4
pom.xml
|
@ -499,6 +499,8 @@
|
||||||
<module>jackson</module>
|
<module>jackson</module>
|
||||||
<module>jackson-2</module>
|
<module>jackson-2</module>
|
||||||
<module>jackson-simple</module>
|
<module>jackson-simple</module>
|
||||||
|
<module>jackson-annotations</module>
|
||||||
|
<module>jackson-exceptions</module>
|
||||||
<module>java-collections-conversions</module>
|
<module>java-collections-conversions</module>
|
||||||
<module>java-collections-conversions-2</module>
|
<module>java-collections-conversions-2</module>
|
||||||
<module>java-collections-maps</module>
|
<module>java-collections-maps</module>
|
||||||
|
@ -1266,6 +1268,8 @@
|
||||||
<module>jackson</module>
|
<module>jackson</module>
|
||||||
<module>jackson-2</module>
|
<module>jackson-2</module>
|
||||||
<module>jackson-simple</module>
|
<module>jackson-simple</module>
|
||||||
|
<module>jackson-annotations</module>
|
||||||
|
<module>jackson-exceptions</module>
|
||||||
<module>java-collections-conversions</module>
|
<module>java-collections-conversions</module>
|
||||||
<module>java-collections-conversions-2</module>
|
<module>java-collections-conversions-2</module>
|
||||||
<module>java-collections-maps</module>
|
<module>java-collections-maps</module>
|
||||||
|
|
Loading…
Reference in New Issue