How to restrict jackson from parsing millis to LocalDate in json request - java

I need to validate LocalDate fields in json requests. What i want is to prevent deserializing numbers as miilis to LocalDate. Here is example:
I have an entity:
public class Test {
#NotNull
#JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate birthDate;
//getter and setter of course
}
Jackson2ObjectMapperBuilder config:
#Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.serializationInclusion(JsonInclude.Include.NON_EMPTY);
builder.featuresToEnable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
builder.featuresToEnable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
builder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
builder.modulesToInstall(new JavaTimeModule());
return builder;
}
Now if i'm receiveing:
{
"birthDate": 1
}
the result is birthDate=1970-01-02
I'm able to do so by setting leniency to false:
objectMapper.configOverride(LocalDate.class).setFormat(JsonFormat.Value.forLeniency(false));
objectMapper.configOverride(LocalDateTime.class).setFormat(JsonFormat.Value.forLeniency(false));
And then it's working by throwing MismatchedInputException
But it's a little brutal to backward compatibility of our service, because we need to change all our date patterns from "yyyy-MM-dd" to "uuuu-MM-dd" and i wonder is there some solution to say jackson "If you see numbers or anything different from the pattern while deserialization, throw an exception"

You could write a custom LocalDateDeserializer:
public class MyLocalDateDeserializer extends JsonDeserializer<LocalDate> implements ContextualDeserializer {
private LocalDateDeserializer defaultDeserializer = new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
public MyLocalDateDeserializer() {
super();
}
public MyLocalDateDeserializer(LocalDateDeserializer defaultDeserializer) {
super();
this.defaultDeserializer = defaultDeserializer;
}
#Override
public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException
{
if (StringUtils.isNumeric(parser.getText())) {
throw JsonMappingException.from(parser, "Not a String representation of Date ");
}
return defaultDeserializer.deserialize(parser, context);
}
#Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException
{
JsonFormat.Value format = findFormatOverrides(ctxt, property, handledType());
return (format == null) ? this : new MyLocalDateDeserializer(new LocalDateDeserializer(DateTimeFormatter.ofPattern(format.getPattern())));
}
protected JsonFormat.Value findFormatOverrides(DeserializationContext ctxt,
BeanProperty prop, Class<?> typeForDefaults)
{
if (prop != null) {
return prop.findPropertyFormat(ctxt.getConfig(), typeForDefaults);
}
// even without property or AnnotationIntrospector, may have type-specific defaults
return ctxt.getDefaultPropertyFormat(typeForDefaults);
}
}
and register it when needed.
Here my simple Tests:
#Test()
public void testObjectMapperForLocalDate() throws IOException {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addDeserializer(LocalDate.class, new MyLocalDateDeserializer());
builder.modulesToInstall(javaTimeModule);
ObjectMapper objectMapper = builder.build();
DateContainer container = objectMapper.readValue("{\r\n" +
" \"birthDate\": \"1999-01-01\"\r\n" +
"}", DateContainer.class);
System.out.println(container.getBirthDate());
}
#Test()
public void testFailObjectMapperForLocalDate() throws IOException {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addDeserializer(LocalDate.class, new MyLocalDateDeserializer());
builder.modulesToInstall(javaTimeModule);
ObjectMapper objectMapper = builder.build();
assertThrows(JsonMappingException.class, () -> {
DateContainer container = objectMapper.readValue("{\r\n" +
" \"birthDate\": 1\r\n" +
"}", DateContainer.class);
System.out.println(container.getBirthDate());
});
}
EDIT
Deserializer uses Pattern

Related

Jackson2ObjectMapperBuilder doesn't serialize object

I am using Spring Boot and it has it's own Jackson's default serialization.
It works not correctly in scope of my task.
So I want override Jackson's default serialization with my own custom serializator.
Here is my code:
#Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
return new Jackson2ObjectMapperBuilder() {
#Override
public void configure(ObjectMapper objectMapper) {
super.configure(objectMapper);
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeCustomSerializer());
objectMapper.registerModule(simpleModule);
objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
}
}.serializerByType(ZonedDateTime.class, new JsonSerializer<ZonedDateTime>() {
#Override
public void serialize(ZonedDateTime value,
JsonGenerator gen,
SerializerProvider serializerProvider) throws IOException {
gen.writeString(value.getNano() + "");
}
});
}
private class ZonedDateTimeCustomSerializer extends JsonSerializer<ZonedDateTime> {
#Override
public void serialize(ZonedDateTime value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeString(value.getNano() + "");
}
}
As you can see I tried some cases such as
registering custom serializator through SimpleModule
overriding Jackson2ObjectMapperBuilder#serialize
Please tip me how to override default Jackson serializator
#Slowpokebreakingnews
If point is to add custom serializers - how about to customize Jackson2ObjectMapperBuilde by Jackson2ObjectMapperBuilderCustomizer:
#Bean
public Jackson2ObjectMapperBuilderCustomizer customizer()
{
return new Jackson2ObjectMapperBuilderCustomizer()
{
#Override
public void customize(Jackson2ObjectMapperBuilder builder)
{
builder.serializerByType(CustomPojo.class,
new CustomSerializator<CustomPojo>());
//affects to all dates in all pojos (I hope :) )
builder.indentOutput(true).dateFormat(new SimpleDateFormat
("yyyy-MM-dd'T'HH:mm:ssXXX"));
}
};
}
For without-Spring-boot-configuration I override configureMessageConverters() :
#Configuration
#EnableWebMvc
...
public class WebConfig extends WebMvcConfigurerAdapter
{
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
{
Jackson2ObjectMapperBuilder builder = new CustomObjectMapperBuilder();
converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
}
}
and define my CustomJackson2ObjectMapperBuilder:
public class CustomObjectMapperBuilder extends Jackson2ObjectMapperBuilder
{
#Override
public void configure(ObjectMapper objectMapper)
{
super.configure(objectMapper);
SimpleModule module = new SimpleModule();
module.addSerializer(Selective.class, new SelectiveSerializer());
objectMapper.registerModule(module)
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.configure(JsonParser.Feature.ALLOW_COMMENTS, true)
.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"));
}
}
For serializing date format you can do something like this
#Component
public class JsonDateSerializer extends JsonSerializer<Date>{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");//yuor desired format goes here
#Override
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
throws IOException, JsonProcessingException {
String formattedDate = dateFormat.format(date);
gen.writeString(formattedDate);
}
}
You can do like your bean too.
And you have another option doing in your POJO like this
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm a z")
private Date date;

Jax-rs alter serialization response based on request with jackson

Currently facing an issue where I would like to alter the response of jax-rs resources based on some information passed into the response. The change that need to be made is the format of some of the json. Currently I am registering an ObjectMapper using jax #Provider and ContextResolver. However the getContext() method is only invoked a single time for each resource class. It is not possible to alter this based on every request.
Is it possible to inject or access the ObjectMapper in a ContainerResponseFilter?
#Provider
public class ObjectMapperResolver implements ContextResolver<ObjectMapper> {
#Context
private HttpServletRequest httpRequest;
private final ObjectMapper mapper;
public ObjectMapperContextResolver() {
mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Date.class, new DateDeserializer("some data format"));
mapper.registerModule(module);
}
#Override
public ObjectMapper getContext(Class<?> type) {
return getMapperBasedOnRequest();
}
private ObjectMapper getMapperBasedOnRequest() {
if (true) {
return mapper;
} else {
return //something else;
}
}
private boolean containsLegacyHeader() {
return //get some information from the request headers or body
}
}
I decided to solve this issue by using a Filter which would be invoked based on a parameter provided in the request, in this case a header.
public class DemoFilter implements ContainerResponseFilter {
private final String UTC = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
private final String OTHER = "yyyy-MM-dd";
public static final String DATE_UTC = "utc";
private String getMapperBasedOnRequest(ContainerRequestContext requestContext) {
if (checkHeader(requestContext)) {
return OTHER;
} else {
return UTC;
}
}
private boolean checkHeader(ContainerRequestContext requestContext) {
return //check header
}
#Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
ObjectWriterInjector.set(new DateMod(getMapperBasedOnRequest(requestContext)));
}
public static class DateMod extends ObjectWriterModifier {
private String df;
public DateMod(String df) {
this.df = df;
}
#Override
public ObjectWriter modify(EndpointConfigBase<?> endpointConfigBase, MultivaluedMap<String, Object> multivaluedMap, Object o, ObjectWriter objectWriter, JsonGenerator jsonGenerator) throws IOException {
return objectWriter.with(new SimpleDateFormat(dateFormat));
}
}
}

Spring Boot Jackson mappings are not working

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

SPRING REST: Removing empty objects from response in JSON format

I don't have option of spring.xml so i went by annotated method.
I have below REST Interfaces in package : com.dpk.cm.impl.ecommerce.rest
and implementation in com.dpk.cm.impl.ecommerce.rest.services
I created one spring config class: but seems like i am still seeing in my JSON response empty objects.
Below is my code :
#Configuration
#ComponentScan(basePackages = "com.dpk.cm.impl.ecommerce.rest")
#EnableWebMvc
public class SpringConfig extends WebMvcConfigurerAdapter {
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
final ObjectMapper objectMapper = new ObjectMapper();
// objectMapper.setSerializationInclusion(Inclusion.NON_EMPTY);
objectMapper.setSerializationInclusion(Include.NON_EMPTY);
converter.setObjectMapper(objectMapper);
converters.add(converter);
super.configureMessageConverters(converters);
}
}
How to remove the Empty Objects from the JSON Reponse Object.
I had similar requirement, but though I use CXF framework on spring boot, there spring boot was creating ObjectMapper which was overriding configuration. Hence I was manually create ObjectMapper as shown below.
#Bean(name = "objectMapper")
public ObjectMapper getObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(
SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, false);
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY,
true);
mapper.setSerializationInclusion(Include.NON_NULL);
return mapper;
}
You can create your custom serializer where you can add a condition on serialization of the object.
Model
#JsonSerialize(using = IgnoreEmptyPersonSerializer.class)
public class Person {
private String name;
private String address;
public Person(String name, String address){
this.name = name;
this.address = address;
}
...
//setters and getters
...
}
Custom Serializer
public class IgnoreEmptyPersonSerializer extends JsonSerializer<Person> {
#Override
public void serialize(Person value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
String name = value.getName();
String address = value.getAddress();
//Dont serialize it if it is empty
if((name == null || name.trim().equals("")) &&
(address == null || address.trim().equals(""))){
return;
}
jgen.writeStartObject();
jgen.writeFieldName("name");
jgen.writeString(value.getName());
jgen.writeFieldName("address");
jgen.writeString(value.getAddress());
jgen.writeEndObject();
}
}

Jackson custom date serializer

I need to set format for class' date serialization.
I have the version of Jackson, which doesn't have #JsonFormat. That's Why I wrote custom class:
public class CDJsonDateSerializer extends JsonSerializer<Date>{
#Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String dateString = dateFormat.format(date);
jsonGenerator.writeString(dateString);
}
}
And used it:
#JsonSerialize(using = CDJsonDateSerializer.class)
private Date startDate;
But, I have another fields which have different date's formats and I don't want to create another classes for serialization. Can I add all needed formats like constants to CDJsonDateSerializer class and set needed format with annotation #JsonSerialize ?
Something like this:
#JsonSerialize(using = CDJsonDateSerializer.class, CDJsonDateSerializer.FIRST_FORMAT).
AFTER THE ANSWER BELOW:
It works after some corrections. I've changed the way of getting annotation in createContextual method:
#Override
public JsonSerializer createContextual(SerializationConfig serializationConfig, BeanProperty beanProperty) {
return new CustomDateSerializer(beanProperty.getAnnotation(JsonDateFormat.class).value());
}
And I've added #JacksonAnnotation to my created new annotation JsonDateFormat:
#Retention(RetentionPolicy.RUNTIME)
#JacksonAnnotation
public #interface JsonDateFormat {
String value();
}
If you cannot use #JsonFormat from Jackson 2, I'd recommend you to introduce your own custom annotation which will contain the format field. Your serailizer should then implement the ContextualSerializer interface to get access to the annotation value.
Here is an example for Jackson 1.9.X:
public class JacksonDateFormat {
#Retention(RetentionPolicy.RUNTIME)
public static #interface MyJsonFormat {
String value();
}
public static class Bean {
#MyJsonFormat("dd.MM.yyyy") #JsonSerialize(using = MyDateSerializer.class)
public final Date date1;
#MyJsonFormat("yyyy-MM-dd") #JsonSerialize(using = MyDateSerializer.class)
public final Date date2;
public Bean(final Date date1, final Date date2) {
this.date1 = date1;
this.date2 = date2;
}
}
public static class MyDateSerializer extends JsonSerializer<Date>
implements ContextualSerializer {
private final String format;
private MyDateSerializer(final String format) {this.format = format;}
public MyDateSerializer() {this.format = null;}
#Override
public void serialize(
final Date value, final JsonGenerator jgen, final SerializerProvider provider)
throws IOException {
jgen.writeString(new SimpleDateFormat(format).format(value));
}
#Override
public JsonSerializer createContextual(
final SerializationConfig serializationConfig, final BeanProperty beanProperty)
throws JsonMappingException {
final AnnotatedElement annotated = beanProperty.getMember().getAnnotated();
return new MyDateSerializer(annotated.getAnnotation(MyJsonFormat.class).value());
}
}
public static void main(String[] args) throws IOException {
final ObjectMapper mapper = new ObjectMapper();
final Bean value = new Bean(new Date(), new Date());
System.out.println(mapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(value));
}
}
Output:
{
"date1" : "02.12.2014",
"date2" : "2014-12-02"
}
If you have access to the ObjectMapper you can register your custom serializer for all the Date types, so you want longer need to put #JsonSerialize annotation.
Here is an example:
final ObjectMapper mapper = new ObjectMapper();
final SimpleModule module = new SimpleModule("", Version.unknownVersion());
module.addSerializer(Date.class, new MyDateSerializer(null));
mapper.registerModule(module);

Categories

Resources