如何使用jackson序列化声明性链接(泽西) [英] How to serialize declarative links (jersey) with jackson

查看:128
本文介绍了如何使用jackson序列化声明性链接(泽西)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using declarative linking in my project. My jackson mapper configuration is

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);

因为我禁用了任何类型的自动检测,所以注入了像

As I have disabled any kind of auto detection, injected links like

    @InjectLinks({

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

被序列化为空的JSON对象(因为Link中的所有字段都没有注明 @JsonProperty )。

are serialized to an empty JSON object (because none of the fields in "Link" is annotated with @JsonProperty).

如何为字段 rel href 而不更改我的全局映射器配置?

How to enable serialization for Links only for the fields rel and href without changing my global mapper configuration?

推荐答案

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

So one way to make this work is to use a customer serializer. You would have to add a new module for this serializer to the ObjectMapper, but this should not effect the rest of the configurations.

这是序列化程序

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<Link>{

    @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<Link> 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序列化声明性链接(泽西)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆