泽西岛2中的自定义MOXyJsonProvider无法正常工作吗? [英] Custom MOXyJsonProvider in Jersey 2 not working?
问题描述
我正在阅读 Moxy忽略json中的无效字段的答案并且该方法与我尝试执行的操作匹配,因此我决定尝试一下..我创建了一个功能来禁用默认的ConfigurableMoxyJsonProvider;
I was reading over the answer for Moxy ignore invalid fields in json and the approach matched something I'm trying to do, so I decided to give it a shot.. I created a feature to disable the default ConfigurableMoxyJsonProvider;
@Provider
public class JsonFeature implements Feature {
@Override
public boolean configure(final FeatureContext context) {
final String disableMoxy = CommonProperties.MOXY_JSON_FEATURE_DISABLE +
'.' +
context.getConfiguration().getRuntimeType().name().toLowerCase();
context.property(disableMoxy, true);
return true;
}
}
然后我创建了一个非常简单的自定义提供程序;
And I created a really simple custom provider;
@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class JsonProvider extends MOXyJsonProvider {
@Override
protected void preWriteTo(Object object, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, Marshaller marshaller)
throws JAXBException {
System.out.println("test");
}
@Override
protected void preReadFrom(Class<Object> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
Unmarshaller unmarshaller)
throws JAXBException {
System.out.println("test");
}
}
我都注册了
register(JsonFeature.class);
register(JsonProvider.class);
然后我用一个简单的GET请求试了一下.
And I gave it a shot with a simple GET request;
@GET
@Path("test")
public String getTest() {
return new TestObject();
}
我认为这应该可行,但是preWriteTo和preReadFrom都不会被调用.我还缺少其他步骤吗?我该如何射击?
I believe this should work, but neither preWriteTo nor preReadFrom ever get invoked.. Is there another step I'm missing? How do I get these to fire?
推荐答案
弄清楚了-对于偶然发现它的任何人.关闭默认设置的正确方法是;
Figured it out -- for anyone that stumbles across it.. The correct way to turn off the default is;
@Provider
public class JsonFeature implements Feature {
@Override
public boolean configure(final FeatureContext context) {
context.property(CommonProperties.MOXY_JSON_FEATURE_DISABLE_SERVER, true);
return true;
}
}
然后像这样扩展ConfigurableMoxyJsonProvider;
Then extend ConfigurableMoxyJsonProvider like so;
@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class JsonProvider extends ConfigurableMoxyJsonProvider {
@Override
protected void preWriteTo(Object object, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, Marshaller marshaller)
throws JAXBException {
System.out.println("test");
}
@Override
protected void preReadFrom(Class<Object> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
Unmarshaller unmarshaller)
throws JAXBException {
System.out.println("test");
}
}
这篇关于泽西岛2中的自定义MOXyJsonProvider无法正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!