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");
}
}