3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Jacksonでnullのときのシリアライズの設定方法

Posted at

nullのときは空文字を返すための設定

nullのときのシリアライザークラスを作成

NullValueSerializer
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;

public static class NullValueSerializer extends JsonSerializer<Object> {
        @Override
        public void serialize(Object t, JsonGenerator jsonGenerator, SerializerProvider sp)
        throws IOException, JsonProcessingException {
          jsonGenerator.writeString("");
        }
      }

あとはSerializerProviderに上記をセットしたものを、ObjectMapperのインスタンスにセットする。


import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.ser.StdSerializerProvider;

/ *  */

final ObjectMapper mapper= new ObjectMapper();

StdSerializerProvider serializeProvider = new StdSerializerProvider(); 
serializeProvider.setNullValueSerializer(new NullValueSerializer());

mapper.setSerializerProvider(serializeProvider);
  • 参考

3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?