gpt4 book ai didi

java - 如何用 jackson 序列化声明性链接( Jersey )

转载 作者:行者123 更新时间:2023-11-30 08:19:12 46 4
gpt4 key购买 nike

我在我的项目中使用声明式链接。我的 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);

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

这样的链接
    @InjectLinks({

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

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

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

最佳答案

因此,实现此功能的一种方法是使用客户序列化程序。您必须将此序列化程序的新模块添加到 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<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/"
} ]
}

希望这对您有所帮助。

关于java - 如何用 jackson 序列化声明性链接( Jersey ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26989004/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com