spring-petclinic/spring-petclinic-rest

Custom Serialization

Closed this issue · 6 comments

We are using your project as a showcase for a tool that creates secure RESTful services, protecting them against data integrity attacks, and some of the OWASP Top 10 security issues. We did the same for

https://github.com/spring-petclinic/spring-petclinic-angularjs

We were able to integrate Hdiv tool in your project but there is one detail that makes the integration uglier and I was wondering if there could be another option to do it. Our tools integrates with Jackson serialization and SDR projections to modify serialization/deserialization transparently. however in this project all serialization/deserialization process is being handled manually, my question is, what is the reason for doing that? Wouldn't be possible to do the same with projections in a more tradition SDR way?.

Let us know if we can help on anything, and thanks for the answer in advance

In short: petclinic model have circular references. As example: owner.pets -> pet.owner ->...
I was trying resolve this with @JsonIdentityInfo annotation, but not satisfied with input/output results (JSON format) - too complicate.
So I wrote custom serialization/deserialization.

arey commented

@vfedoriv in spring-petclinic-angularjs, bidirectionnal associations between Pet <--> Owner and Visit <--> Pet have been resolved by using the Jackon @JsonIgnore annotation.

Ok, I can try check this.

I have problem with automatic dates deserialization.
In database - Tue Jan 01 00:00:00 EET 2013
after deserialization - Mon Dec 31 02:00:00 EET 2012
Dates shift (Jackson use time zone when deserialize dates).
How I can disable this behavior ?

P.S.
spring.jackson.deserialization.adjust-dates-to-context-time-zone=false don't work

arey commented

In spring-petclinc-angularjs, there is a @JsonFormat(pattern = "yyyy-MM-dd") I don't know the reason. But maybe a solution?

    static class PetRequest {
        int id;
        @JsonFormat(pattern = "yyyy-MM-dd")
        Date birthDate;
        @Size(min = 1)
        String name;
        int typeId;

  static class PetDetails {

        long id;
        String name;
        String owner;
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        Date birthDate;
        PetType type;

See https://github.com/spring-petclinic/spring-petclinic-angularjs/blob/master/spring-petclinic-server/src/main/java/org/springframework/samples/petclinic/web/PetResource.java

In fact, I was wrong in description - it's serialization problem
Not sure about writing special classes, for serialization only.
I can just write something like:

public class JacksonJsonDateSerializer extends JsonSerializer<Date> {
	
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	@Override
	public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers)
			throws IOException, JsonProcessingException {
		String formattedDate = dateFormat.format(value);
		gen.writeString(formattedDate);
	}
} 

and annotate Date fields with
@JsonSerialize(using=JacksonJsonDateSerializer.class)
It's works, but...
@anderruiz , Hdiv tool have problem with @JsonSerialize ?