[BAEL-14274] - Fixed article code for https://www.baeldung.com/jackson-annotations
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package com.baeldung.jackson.annotation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonGetter;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import com.fasterxml.jackson.annotation.JsonSetter;
|
||||
|
||||
@JsonInclude(Include.NON_NULL)
|
||||
@JsonPropertyOrder({ "name", "id" })
|
||||
@@ -18,4 +20,14 @@ public class MyBean {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@JsonGetter("name")
|
||||
public String getTheName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@JsonSetter("name")
|
||||
public void setTheName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,17 +4,17 @@ import java.util.Date;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
public class EventWithFormat {
|
||||
public class Event {
|
||||
public String name;
|
||||
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
|
||||
public Date eventDate;
|
||||
|
||||
public EventWithFormat() {
|
||||
public Event() {
|
||||
super();
|
||||
}
|
||||
|
||||
public EventWithFormat(final String name, final Date eventDate) {
|
||||
public Event(final String name, final Date eventDate) {
|
||||
this.name = name;
|
||||
this.eventDate = eventDate;
|
||||
}
|
||||
@@ -32,7 +32,7 @@ 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.Event;
|
||||
import com.baeldung.jackson.date.EventWithSerializer;
|
||||
import com.baeldung.jackson.dtos.MyMixInForIgnoreType;
|
||||
import com.baeldung.jackson.dtos.withEnum.DistanceEnumWithValue;
|
||||
@@ -270,7 +270,7 @@ public class JacksonAnnotationUnitTest {
|
||||
|
||||
final String toParse = "20-12-2014 02:30:00";
|
||||
final Date date = df.parse(toParse);
|
||||
final EventWithFormat event = new EventWithFormat("party", date);
|
||||
final Event event = new Event("party", date);
|
||||
|
||||
final String result = new ObjectMapper().writeValueAsString(event);
|
||||
assertThat(result, containsString(toParse));
|
||||
|
||||
Reference in New Issue
Block a user