Java Jackson ObjectMapper - 指定对象属性的序列化顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27577701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson ObjectMapper - specify serialization order of object properties
提问by Lizzy
I'm implementing a RESTful web service where user has to send a signed verification token along with the request so that I could ensure that the request has not been tampered by a middle man. My current implementation is as follows.
我正在实现一个 RESTful Web 服务,其中用户必须随请求一起发送签名验证令牌,以便我可以确保请求没有被中间人篡改。我目前的实现如下。
Verification token is a VerifData object serialized into a String and then hashed and encrypted.
验证令牌是一个 VerifData 对象序列化为一个字符串,然后散列和加密。
class VerifData {
int prop1;
int prop2;
}
In my service, I put data to be serialized into an instance of VerifData and then serialize it using Hymanson ObjectMapper and passed along to the verification engine along with the verification token.
在我的服务中,我将要序列化的数据放入 VerifData 的实例中,然后使用 Hymanson ObjectMapper 对其进行序列化,并与验证令牌一起传递给验证引擎。
VerfiData verifData = new VerifData(12345, 67890);
ObjectMapper mapper = new ObjectMapper();
String verifCodeGenerated = mapper.writeValueAsString(verifData);
But it seems that each time the application container is started, the order of properties being mapped into a string by ObjectMapper changes.
但是似乎每次启动应用程序容器时,ObjectMapper 映射到字符串的属性顺序都会发生变化。
Ex: one time it would be
例如:有一次是
{"prop1":12345,"prop2":67890}
and another time it would be
还有一次是
{"prop2":67890,"prop1":12345}
So if client has serialized the VerifData instance as into the first String, there is 50% chance of it being failed even though it is correct.
因此,如果客户端将 VerifData 实例序列化为第一个字符串,即使它是正确的,它也有 50% 的机会失败。
Is there a way to get around this? Can I specify the order of properties to map by ObjectMapper (like in ascending order)? Or is there any other way to best implement this verification step. Both client and server implementations are developed by me. I use Java Security API for signing and verifying.
有没有办法解决这个问题?我可以通过 ObjectMapper 指定要映射的属性的顺序(如升序)吗?或者有没有其他方法可以最好地实施此验证步骤。客户端和服务器实现都是由我开发的。我使用 Java Security API 进行签名和验证。
采纳答案by wgitscht
From the Hymanson Annotations documentation:
// ensure that "id" and "name" are output before other properties
@JsonPropertyOrder({ "id", "name" })
// order any properties that don't have explicit setting using alphabetic order
@JsonPropertyOrder(alphabetic=true)
回答by Duncan McGregor
The annotations are useful, but can be a pain to apply everywhere. You can configure your whole ObjectMapper to work this way with
注释很有用,但在任何地方应用可能会很痛苦。您可以将整个 ObjectMapper 配置为以这种方式使用
Current Hymanson versions:
objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
当前的Hyman逊版本:
objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
Older Hymanson versions:
objectMapper.configure(SerializationConfig.Feature.SORT_PROPERTIES_ALPHABETICALLY, true);
较旧的Hyman逊版本:
objectMapper.configure(SerializationConfig.Feature.SORT_PROPERTIES_ALPHABETICALLY, true);
回答by Nemes Nisano
From Duncan McGregor's answer: Its better to use it like this:
来自 Duncan McGregor 的回答:最好像这样使用它:
objectMapper.configure(SerializationConfig.Feature.SORT_PROPERTIES_ALPHABETICALLY, true);
as MapperFeature is for XMLs and comes with Hymanson-databind which is not required...
因为 MapperFeature 适用于 XML,并且带有不需要的 Hymanson-databind ...
回答by Gary Rowe
In Spring Boot you can add this behaviour globally by adding the following to your Application
entry point class:
在 Spring Boot 中,您可以通过将以下内容添加到Application
入口点类来全局添加此行为:
@Bean
public Hymanson2ObjectMapperBuilder objectMapperBuilder() {
Hymanson2ObjectMapperBuilder builder = new Hymanson2ObjectMapperBuilder();
builder.featuresToEnable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
return builder;
}
回答by Wim Deblauwe
There is an easier way in Spring Boot by specifying a property (in application.properties
for example:
通过指定属性(application.properties
例如:
spring.Hymanson.mapper.sort_properties_alphabetically=true
回答by user2089674
In Hymanson 2.x, which you are probably using today, use:
在您今天可能正在使用的 Hymanson 2.x 中,使用:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
If you care about looks, you may also consider SerializationFeature.INDENT_OUTPUT
as well.
如果你在乎外观,你也可以考虑一下SerializationFeature.INDENT_OUTPUT
。
Note that you must serialize Mapsor Objectsfor this to sort correctly. If you serialize a JsonNode
for example (from readTree
), that won't be properly indented.
请注意,您必须序列化Maps或Objects才能正确排序。JsonNode
例如,如果您序列化 a (from readTree
),则不会正确缩进。
Example
例子
import com.fasterxml.Hymanson.databind.*;
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
String input = "{\"hello\": {\"cruel\" : \"world\"} }";
Object pojo = mapper.readValue(input, Object.class);
System.out.println(mapper.writeValueAsString(pojo));
results in:
结果是:
{
"hello" : {
"cruel" : "world"
}
}
回答by Leon
Instead of using flag argument:
而不是使用标志参数:
objectMapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
回答by magiccrafter
The following 2ObjectMapper configuration are required:
需要以下2 个ObjectMapper 配置:
ObjectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
orObjectMapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
ObjectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
或者ObjectMapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
defines the property serialization order used for POJO fields
Note: does notapply tojava.util.Map
serialization!
定义用于POJO属性系列化顺序字段
注:确实不适用java.util.Map
系列化!
and
和
ObjectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
orObjectMapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
ObjectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
或者ObjectMapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
Feature that determines whether
java.util.Map
entries are first sorted by keybefore serialization
确定
java.util.Map
条目在序列化之前是否首先按键排序的功能
Spring Boot config example (yaml):
Spring Boot 配置示例(yaml):
spring:
Hymanson:
mapper:
SORT_PROPERTIES_ALPHABETICALLY: true
serialization:
ORDER_MAP_ENTRIES_BY_KEYS: true