java - Spring Boot Jackson mappings are not working -
i using fasterxml jackson json serialization. have written date serializer
public class dateobjectserializer extends jsonserializer<date> { public static final string date_format = "dd.mm.yyyy"; @override public void serialize(date date, jsongenerator jgen, serializerprovider provider) throws ioexception, jsonprocessingexception { system.out.println("from dateobjectserializer"); simpledateformat dateformat = new simpledateformat(date_format); string formatteddate = dateformat.format(date); jgen.writestring(formatteddate); } }
but not being invoked. other jackson serializers working fine.
so added following configuration in application.yaml
spring: jackson: serialization-inclusion: non_null date-format: dd.mm.yyyy
but din't work.
so have added code in springbootconfiguration class.
@override public void configuremessageconverters(list<httpmessageconverter<?>> converters) { final mappingjackson2httpmessageconverter converter = new mappingjackson2httpmessageconverter(); final objectmapper objectmapper = new objectmapper(); simpledateformat dateformat = new simpledateformat(date_format); objectmapper.configure(serializationfeature.write_dates_as_timestamps, false).setdateformat(dateformat); converter.setobjectmapper(objectmapper); converters.add(converter); super.configuremessageconverters(converters); }
now dates being serialized correctly. valid json equivalent strings not being transformed json mentioned here.
@restcontroller public class samplecontroller { @requestmapping(value = "/jsoninfo", method = requestmethod.get, produces = application_json_value) public string jsoninfo() { string string = "{\"name\": \"foo\"}" return string; } }
try this
import com.fasterxml.jackson.databind.objectmapper; : @autowired private objectmapper objectmapper; @restcontroller public class samplecontroller { @requestmapping(value = "/jsoninfo", method = requestmethod.get, produces = application_json_value) public jsonnode jsoninfo() throws jsonprocessingexception, ioexception { string string = "{\"name\": \"foo\"}" return objectmapper.readtree(string); } }
Comments
Post a Comment