2

I am new to JAX-RS and I want to serve my list of items as JSON. My entity model is something like this:

public class Entity {
    private String name;
    private Date date;
    private Float number;
}

This is how I am invoking the service:

@Path("/entities")
public class EntitiesController {
    @GET
    @Produces({"application/json"})
    public List<Entity> getEntities() {
        return EntityDAO.entitiesList();
    }
}

However, the date is not formatted but is displayed as a long.

This answer shows how to format a date using a JsonSerializer. If I extend JsonSerializer, then where do I put that subclass in my project?

Community
  • 1
  • 1
dabadaba
  • 9,064
  • 21
  • 85
  • 155
  • Why do you think it is happening because an attribute is not defined? Jaxrs [should](http://stackoverflow.com/a/13979349/446554) just serialize it to json using `null`. – Cory Klein Oct 15 '15 at 20:30
  • Also, for the date formatting please split it into its own question. (You may also find [this](http://stackoverflow.com/a/11233594/446554) helpful.) – Cory Klein Oct 15 '15 at 20:32
  • @CoryKlein I found out the getter for `number` was returning a `float` instead of a `Float`, that's what was causing the error. Anyway, I am more interested in customization of the JSON as the title suggests. – dabadaba Oct 15 '15 at 20:34
  • @CoryKlein I have seen similar solutions, but since I am new I don't know where to place the class inheriting from `JsonSerializer` in order to put all together. – dabadaba Oct 15 '15 at 20:35
  • I updated your question to more accurately reflect what I perceive to be your intent. Please feel free to revert if you feel that it doesn't reflect your real intent in asking this question. – Cory Klein Oct 15 '15 at 20:39
  • @CoryKlein finally figured it out. Created a `serializers` package, created a `CustomJsonDateSerializer` in it, imported it in my `Entity` class and added the following annotation to my `date` field: `@JsonSerialize(using = CustomJsonDateSerializer.class)` I will accept your answer – dabadaba Oct 15 '15 at 20:47
  • 1
    Even better, answer the question yourself using the box below! – Cory Klein Oct 15 '15 at 20:51

1 Answers1

1

I figured a solution myself:

Under a new serializers package I created the CustomJsonDateSerializer class, which will be delegated the responsibility of formatting the date attribute thanks to the @JsonSerialize(...) annotation.

So I modified my Entity class adding that annotation ontop of the field:

@JsonSerialize(using = CustomJsonDateSerializer.class)
private Date date;

And this is the content of CustomJsonDateSerializer:

package serializers;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class CustomJsonDateSerializer extends JsonSerializer<Date> {
    @Override
    public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonGenerationException {
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyy");
        String format = formatter.format(value);
        jgen.writeString(format);
    }
}
dabadaba
  • 9,064
  • 21
  • 85
  • 155