2
我嘗試使用自定義Jackson序列化程序,但不幸的是序列化程序不會被觸發。Jackson自定義序列化程序
在我的應用程序添加傑克遜:
JacksonJaxbJsonProvider jacksonProvider = new JacksonJaxbJsonProvider();
jacksonProvider.setMapper(mapper);
s.add(jacksonProvider);
return s;
我添加了一個串行我的課TestType:
@JsonSerialize(using = TestSerializer.class)
public class TestType {
private String test;
public String getTest(){
if (test==null || test.isEmpty()){
test="test";
}
return test;
}
public void setTest(String test) {
this.test = test;
}
}
public class TestSerializer extends JsonSerializer<TestType> {
@Override
public void serialize(TestType value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeStringField("special serialization", value.getTest());
jgen.writeEndObject();
}
}
的API:
@GET
@Produces({"application/json; charset=UTF-8"})
public Response getTest(@HeaderParam(RestHelper.HEADER_PARAM_ORIGIN) String origin, @Context HttpServletRequest request, @Context HttpHeaders headers) {
TestType test = new TestType();
test.setTest("test");
return Response.status(200).entity(test).build();
}
我錯過任何一步註冊串行器?
問候, 海波