问题
We have developed a REST service using Jersey JAX-RS and Jackson (version 2.1.5) for JSON serialization.
As the application is supposed to be a drop-in replacement for the older legacy service acting as a backend to an existing mobile app, we need to do some tweaking to the way Jackson serializes boolean values.
Existing mobile app expects boolean values to be expressed as strings of "true" and "false" like this:
{"Foo":"true","Bar":"false"}
So I have searched for a way to influence the Jackson serialization to output booleans as strings, but I have no success.
Oh, and btw - since our model classes have been generated from xml schemas using JAXB class generation, we can not annotate the classes with json annotations.
I have tried to register a module with ObjectMapper, that provides a customized serializer for boolean objects, but it did not seem to work.
回答1:
Jackson 2.16 Custom Serializer for primitive data type . you should write your own serializer. example code for boolean data type
// create a module with a custom Boolean serializer
class BooleanSerializer extends JsonSerializer<Boolean> {
private final static Logger logger = LoggerFactory.getLogger(BooleanSerializer.class);
@Override
public void serialize(Boolean value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException {
logger.info("serializing boolean value as a Strng {}",value);
jgen.writeString(value.toString());
}
}
//register custom BooleanSerializer class with ObjectMapper.
// Here's where we configure the object mapper
ObjectMapper mapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule("BooleanAsString", new Version(1, 0, 0, null, null, null));
simpleModule.addSerializer(Boolean.class,new BooleanSerializer());
simpleModule.addSerializer(boolean.class,new BooleanSerializer());
mapper.registerModule(module);
回答2:
Okay, it seems that either my IDE or Maven was acting up and refused to build or reference the changes I made in my ObjectMapper
configuration.
For the sake of the future visitors, here is the gist of the solution to the issue of making Jackson data binding to spit out boolean values as strings:
In my customized ObjectMapper context resolver, I just had to add special serializer for boolean object types:
// create a module with a custom Boolean serializer
SimpleModule module = new SimpleModule("BooleanAsString", new Version(1, 0, 0, null, null, null));
module.addSerializer(new NonTypedScalarSerializerBase<Boolean>(Boolean.class){
@Override
public void serialize(Boolean value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException {
ObjectMapperProvider.log.debug("serializing boolean value as a Strng");
jgen.writeString(value.toString());
}
});
// Here's where we configure the object mapper
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
That's it. If you know how to configure your ObjectMapper, then this should be enough to get you going.
来源:https://stackoverflow.com/questions/19678340/how-to-serialize-boolean-to-json-as-strings-using-jackson