0

I am just not able to figure what's wrong with the following piece of code. It does not output the date field as a String. I googled and found that people are doing the same way and they are getting the desired output. Am I missing some sort of additional configuration? Please help.

package hello;

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

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {

    @RequestMapping("/test")
    public @ResponseBody Client getClient() {
        System.out.println("heeeeee");
        return new Client();
    }

}

class Client {

    @JsonSerialize(using = CustomDateSerializer.class)
    private Date d = new Date();

    @JsonSerialize(using = CustomDateSerializer.class)
    public Date getD() {
        System.out.println("CP 2");
        return d;
    }

    public void setD(Date d) {
        this.d = d;
    }

}

class CustomDateSerializer extends JsonSerializer<Date> {

    @Override
    public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider)
            throws IOException, JsonGenerationException {

        System.out.println("CP 0");

        //SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'ZZZ (z)");
        //String format = formatter.format(value);
        //jgen.writeString(format);
        jgen.writeString("asdfasfdasdfasdf asdlkfja sdfljasd flkasdjf");
    }

}
dopeddude
  • 4,943
  • 3
  • 33
  • 41
  • You will need to register the custom serializer with your mapper class. Can you share how you defined the mapper? – Dheerendra Kulkarni Mar 27 '15 at 07:45
  • How do we register the custom serializer with the mapper class.. as in this application I have not used any explicit mapper declaration or initialization.. – dopeddude Mar 27 '15 at 07:47
  • atleast share how you defined jackson as your json provider? so I can provide a more targtetted soln. – Dheerendra Kulkarni Mar 27 '15 at 07:49
  • Sorry, but this is a spring boot application and when on any method I annotate it with @ResponseBody it automatically converts the POJO returned by the method to a JSON object using the default serializer library which is Jackson in this case. – dopeddude Mar 27 '15 at 07:57
  • Hey @DopedDude did you find out the answer to this I've the exact same situation. – Schokea May 21 '15 at 12:04
  • @Schokea I will have to check my project what solution had I applied that time. I would be able to tell you the solution on Monday when I get to office. Currently I am OOO. – dopeddude May 21 '15 at 12:25
  • @DopedDude ye sure I'll obviously try to find the solution between now and then but if you get a chance on Monday I'll definitely be interested in hearing it. Thank you. – Schokea May 21 '15 at 13:09

1 Answers1

2

You use org.codehaus.jackson library. Try this one com.fasterxml.jackson. Here is the same problem is solved.

Community
  • 1
  • 1
razorman
  • 21
  • 2