1

I am using fasterxml jackson for json serialization. I have written date serializer as

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 it is not being invoked. However other Jackson Serializers are working fine.

So I added following configuration in application.yaml

spring:
  jackson:
    serialization-inclusion: non_null
    date-format: dd.MM.yyyy

But it din't work.

So I have added this 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 are being serialized correctly. But now valid JSON equivalent strings are not being transformed to JSON as 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;
    }
}
Community
  • 1
  • 1
Amit Kumar Gupta
  • 7,193
  • 12
  • 64
  • 90
  • I think property base configuration is supported from Spring boot 1.4.1. So there os issue 1 – Amit Kumar Gupta Aug 08 '16 at 16:43
  • What makes you think that? – Andy Wilkinson Aug 08 '16 at 19:08
  • their documentation. In 1.4.0 docs they dint talk about property based config. But in 1.4.1 docs they talked about @JsonComponent and property based config. – Amit Kumar Gupta Aug 08 '16 at 19:39
  • `@JsonComponent` is new in 1.4, but has nothing to do with property-based configuration. The properties that you have used have been supported since at least 1.3.0. It'd be much easier to see what's happening if you put together a sample that illustrated the problems. – Andy Wilkinson Aug 08 '16 at 20:13
  • I have edited the question to make it more clear. And removed last issue as I found the reason for that, – Amit Kumar Gupta Aug 09 '16 at 15:46
  • I have found a hack to do this [here](http://stackoverflow.com/questions/15507064/return-literal-json-strings-in-spring-mvc-responsebody) – Amit Kumar Gupta Aug 09 '16 at 18:06

1 Answers1

2

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);
    }
}
Abhijeet Patil
  • 304
  • 2
  • 7