我有一个类似的类:
class TestJsonClass {
private String propertyA;
private String propertyB;
private String propertyC;
}
现在在运行时我希望为每个属性提供不同的属性名称,而不是使用@JsonProperty(“sample”)的静态属性
我如何做到这一点?我正在使用Jackson库广告Spring MVC
提前致谢...
答案 0 :(得分:3)
您可以为此目的使用Modules。这是解决您问题的最简单方法。这是一个例子:
一个简单的类,可以为每个请求提供属性名称映射:
public class PropertyNameMapper {
// The class for which the mappings need to take place.
public Class<?> classToFilter;
// The mappings property names. Key would be the existing property name
// value would be name you want in the ouput.
public Map<String, String> nameMappings = Collections.emptyMap();
public PropertyNameMapper(Class<?> classToFilter, Map<String, String> nameMappings) {
this.classToFilter = classToFilter;
this.nameMappings = nameMappings;
}
}
自定义BeanPropertyWriter,用于指定属性的输出名称。
public class MyBeanPropertyWriter extends BeanPropertyWriter {
// We would just use the copy-constructor rather than modifying the
// protected properties. This is more in line with the current design
// of the BeanSerializerModifier class (according to its documentation).
protected MyBeanPropertyWriter(BeanPropertyWriter base, String targetName) {
super(base, new SerializedString(targetName));
}
}
现在,每次调用一个自定义BeanSerializerModifier,允许您修改序列化属性。
public class MySerializerModifier extends BeanSerializerModifier {
public List<BeanPropertyWriter> changeProperties(
SerializationConfig config, BeanDescription beanDesc,
List<BeanPropertyWriter> beanProperties) {
List<PropertyNameMapper> propertyMappings = getNameMappingsFromRequest();
PropertyNameMapper mapping = mappingsForClass(propertyMappings,
beanDesc.getBeanClass());
if (mapping == null) {
return beanProperties;
}
List<BeanPropertyWriter> propsToWrite = new ArrayList<BeanPropertyWriter>();
for (BeanPropertyWriter propWriter : beanProperties) {
String propName = propWriter.getName();
String outputName = mapping.nameMappings.get(propName);
if (outputName != null) {
BeanPropertyWriter modifiedWriter = new MyBeanPropertyWriter(
propWriter, outputName);
propsToWrite.add(modifiedWriter);
} else {
propsToWrite.add(propWriter);
}
}
return propsToWrite;
}
private List<PropertyNameMapper> getNameMappingsFromRequest() {
RequestAttributes requestAttribs = RequestContextHolder
.getRequestAttributes();
List<PropertyNameMapper> nameMappings = (List<PropertyNameMapper>) requestAttribs
.getAttribute("nameMappings",
RequestAttributes.SCOPE_REQUEST);
return nameMappings;
}
private PropertyNameMapper mappingsForClass(
List<PropertyNameMapper> nameMappings, Class<?> beanClass) {
for (PropertyNameMapper mapping : nameMappings) {
if (mapping.classToFilter.equals(beanClass)) {
return mapping;
}
}
return null;
}
}
现在,您需要自定义Module才能使用上面创建的BeanSerializerModifier自定义输出:
public class MyModule extends Module {
@Override
public String getModuleName() {
return "Test Module";
}
@Override
public void setupModule(SetupContext context) {
context.addBeanSerializerModifier(new MySerializerModifier());
}
@Override
public Version version() {
// Modify if you need to.
return Version.unknownVersion();
}
}
现在使用ObjectMapper注册此模块。您可以从Spring应用程序上下文中获取Jackson HTTP消息转换器,并获取其对象映射器。
// Figure out a way to get the ObjectMapper.
MappingJackson2HttpMessageConverter converter = ... // get the jackson-mapper;
converter.getObjectMapper().registerModule(new MyModule())
就是这样。这是动态自定义属性序列化的最简单方法。
要使用此功能,请创建List
PropertyNameMappers
并将其添加为当前请求中的属性(在此示例中名为“nameMappings”)。
这是一个示例,而不是生产就绪代码。您可能需要添加空检查和类似的事情。此外,根据您使用的库版本,可能需要进行一些小的调整。
如果解决方案不适合您,请告诉我您遇到的问题。
答案 1 :(得分:1)
您可以将自定义PropertyNamingStrategy注入到反序列化中使用的ObjectMapper中。
只需在运行时将字段设置到PropertyNamingStrategy中,假设您可以将它们映射到默认的JsonPropertyName(例如propertyA,propertyB,propertyC)。
public class MyNamingStrategy extends PropertyNamingStrategy {
String propertyAName, propertyBName, propertyCName;
public MyNamingStrategy(String propANm, String propBNm, String propCNm) {
this.propertyAName = propANm;
//finish
}
@Override
public String nameForField(MapperConfig<?> config, AnnotatedField field,
String defaultName) {
return convert(defaultName);
}
@Override
public String nameForGetterMethod(MapperConfig<?> config,
AnnotatedMethod method, String defaultName) {
return convert(defaultName);
}
@Override
public String nameForSetterMethod(MapperConfig<?> config,
AnnotatedMethod method, String defaultName) {
return convert(defaultName);
}
public String convert(String defaultName ){
return defaultName.replace("propertyA", propertyAName).replace( //finish
}
最后,您要创建一个实例并在运行时注入它。 objectMapper.setNamingStrategy(myNamingStrategyInstance));
有关PropertyNamingStrategy的更多信息,请参阅此Cowtowncoder帖子:
Jackson 1.8: custom property naming strategies
或者此文档:
github.com/FasterXML/jackson-docs/wiki/PropertyNamingStrategy