Skip to content

Custom JsonSerialize with DateObject not working #41

Closed
@prakash290

Description

@prakash290

Custom Serialize for Date Object Class is not working.

POJO Class :

public class Client {
	
	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="Client")		
	private Integer client_id;

      @JsonSerialize(using=JacksonDateTimeSerializer.class)	
	public Date getCreation_date() {
		return creation_date;
	}

}

JacksonDateTimeSerializer Class

@Component
public class JacksonDateTimeSerializer extends StdSerializer<Date>{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		 
		    public JacksonDateTimeSerializer() {
		        this(null);
		    }
		 
		    @SuppressWarnings({ "rawtypes", "unchecked" })
			public JacksonDateTimeSerializer(Class t) {
		        super(t);
		    }
		     
		    @Override
		    public void serialize (Date value, JsonGenerator gen, SerializerProvider arg2)
		      throws IOException, JsonProcessingException {
		    	System.out.println("Am Executing");
		        gen.writeString(formatter.format(value));
		    }
}
    @Bean(name="ObjectMapper")
	public ObjectMapper objectMapper()
	{
		return new ObjectMapper();
	}
	
	@Bean(name="jsonViewObjectMapper")
	public ObjectMapper jsonViewObjectMapper()
	{
		ObjectMapper mapper = new ObjectMapper();
    	SimpleModule module = new SimpleModule();
    	module.addSerializer(JsonView.class, new JsonViewSerializer());    	
    	mapper.registerModule(module);
    	return mapper;
	}

Using ObjectMapper instance I can able to Serialize Date using JacksonDateTimeSerializer class.
But using jsonViewObjectMapper instance i can't Serialize using JacksonDateTimeSerializer class.

How to use custom Serialize class for Date object ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions