如何使用jackson序列化声明性链接(泽西)

我在我的项目中使用声明性链接。 我的jackson映射器配置是

final ObjectMapper mapper = new ObjectMapper(); mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); mapper.configure(MapperFeature.AUTO_DETECT_FIELDS, false); mapper.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false); mapper.configure(MapperFeature.AUTO_DETECT_GETTERS, false); mapper.configure(MapperFeature.AUTO_DETECT_SETTERS, false); mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); 

因为我已禁用任何类型的自动检测,注入的链接就像

  @InjectLinks({ @InjectLink(rel = "bookmark", resource = ConnectionsResource.class, style = Style.ABSOLUTE_PATH) }) @JsonProperty("links") Link[] links; 

被序列化为一个空的JSON对象(因为“Link”中的所有字段都没有用@JsonProperty注释)。

如何在不更改全局映射器配置的情况下为字段relhref启用链接的序列化?

因此,实现此function的一种方法是使用客户序列化程序。 您必须将此序列化程序的新模块添加到ObjectMapper ,但这不应影响其余配置。

这是序列化器

 import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import java.io.IOException; import javax.ws.rs.core.Link; public class LinkSerializer extends JsonSerializer{ @Override public void serialize(Link link, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException { jg.writeStartObject(); jg.writeStringField("rel", link.getRel()); jg.writeStringField("href", link.getUri().toString()); jg.writeEndObject(); } } 

这是一个测试类

 public class TestClass { @JsonProperty("links") protected List links; protected String name; protected String id; // getter and setters } 

并且测试运行

 public static void main(String[] args) throws Exception{ ObjectMapper mapper = new ObjectMapper(); mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false); mapper.configure(MapperFeature.AUTO_DETECT_FIELDS, false); mapper.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false); mapper.configure(MapperFeature.AUTO_DETECT_GETTERS, false); mapper.configure(MapperFeature.AUTO_DETECT_SETTERS, false); mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); SimpleModule simpleModule = new SimpleModule(); simpleModule.addSerializer(Link.class, new LinkSerializer()); mapper.registerModule(simpleModule); Link link1 = Link.fromUri(URI.create("http://localhost:8080/")).rel("one").build(); Link link2 = Link.fromUri(URI.create("http://localhost:8080/")).rel("two").build(); TestClass test = new TestClass(); test.getLinks().add(link1); test.getLinks().add(link2); String json = mapper.writeValueAsString(test); System.out.println(json); } 

产生这个结果

 { "links" : [ { "rel" : "one", "href" : "http://localhost:8080/" }, { "rel" : "two", "href" : "http://localhost:8080/" } ] } 

希望这可以帮助。

以下是使用Jackson mixin注释对Link对象进行序列化和反序列化的示例,包括所有属性:

 @JsonAutoDetect( fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE) @JsonDeserialize(using = LinkMixin.LinkDeserializer.class) public abstract class LinkMixin extends Link { private static final String HREF = "href"; @JsonProperty(HREF) @Override public abstract URI getUri(); @JsonAnyGetter public abstract Map getParams(); public static class LinkDeserializer extends JsonDeserializer { @Override public Link deserialize( final JsonParser p, final DeserializationContext ctxt) throws IOException { final Map params = p.readValueAs( new TypeReference>() {}); if (params == null) { return null; } final String uri = params.remove(HREF); if (uri == null) { return null; } final Builder builder = Link.fromUri(uri); params.forEach(builder::param); return builder.build(); } } }