gpt4 book ai didi

java - Jersey 2 中的自定义 MOXyJsonProvider 不起作用?

转载 作者:搜寻专家 更新时间:2023-11-01 03:04:01 25 4
gpt4 key购买 nike

我正在阅读 Moxy ignore invalid fields in json 的答案并且该方法与我正在尝试做的事情相匹配,所以我决定试一试。我创建了一个功能来禁用默认的 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;
}
}

然后我创建了一个非常简单的自定义提供程序;

@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 请求试了一下;

@GET
@Path("test")
public String getTest() {
return new TestObject();
}

我相信这应该可行,但 preWriteTo 和 preReadFrom 都不会被调用。我还缺少另一个步骤吗?我怎样才能让这些开火?

最佳答案

弄明白了——对于任何偶然发现它的人来说。关闭默认设置的正确方法是;

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

@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");
}
}

关于java - Jersey 2 中的自定义 MOXyJsonProvider 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29081101/

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