问题
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;
are serialized to an empty JSON object (because none of the fields in "Link" is annotated with @JsonProperty).
How to enable serialization for Links only for the fields rel and href without changing my global mapper configuration?
回答1:
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.
Here's the serializer
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();
}
}
Here a test class
public class TestClass {
@JsonProperty("links")
protected List<Link> links;
protected String name;
protected String id;
// getter and setters
}
And the test run
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);
}
produces this result
{
"links" : [ {
"rel" : "one",
"href" : "http://localhost:8080/"
}, {
"rel" : "two",
"href" : "http://localhost:8080/"
} ]
}
Hope this helps.
回答2:
Here is an example of using the Jackson mixin annotation for serializing and deserializing the Link object including all the properties:
@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<String, String> getParams();
public static class LinkDeserializer extends JsonDeserializer<Link> {
@Override
public Link deserialize(
final JsonParser p,
final DeserializationContext ctxt) throws IOException {
final Map<String, String> params = p.readValueAs(
new TypeReference<Map<String, String>>() {});
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();
}
}
}
来源:https://stackoverflow.com/questions/26989004/how-to-serialize-declarative-links-jersey-with-jackson