Java 如何在泽西岛的Moxy注册ValidationEventHandler?
讨论如何在Java 如何在泽西岛的Moxy注册ValidationEventHandler?,java,rest,jaxb,jersey,moxy,Java,Rest,Jaxb,Jersey,Moxy,讨论如何在解组器对象上设置验证处理程序 但是,使用Jersey,MOXy/JAXB是为我初始化的,我不知道如何访问解组器 目前,我像这样引导Jersey,类路径上有Jersey media moxy @ApplicationPath("rest") public class ApplicationConfig extends ResourceConfig { Logger logger = LoggerFactory.getLogger(ApplicationConfig.class);
解组器
对象上设置验证处理程序
但是,使用Jersey,MOXy/JAXB是为我初始化的,我不知道如何访问解组器
目前,我像这样引导Jersey,类路径上有Jersey media moxy
@ApplicationPath("rest")
public class ApplicationConfig extends ResourceConfig {
Logger logger = LoggerFactory.getLogger(ApplicationConfig.class);
public ApplicationConfig() {
// Scan classes in this package and subpackages
logger.info("Registering REST Application");
packages("rest");
register(new AbstractBinder() {
@Override
protected void configure() {
// used to automatically inject a Connection instance and close it
bindFactory(ConnectionFactory.class).to(Connection.class)
.proxy(true).proxyForSameScope(false).in(RequestScoped.class);
}
});
}
}
使用这种方法,如何在解组器上注册一个
验证EventHandler
?我不知道这是否是正确的方法(我从来没有这样做过),但是做一些深入的研究,您可以扩展ConfigurableMoxyJsonProvider
并覆盖
我看没有其他方法可以做到这一点。前面提到的类扩展了。您可以在中看到,当创建unmarhsaller
时,您实际上无法对其进行太多操作。您所能做的就是从外部设置属性。但是没有任何东西可以让您访问实际的解组器
。因此,也许只有扩展提供者才能访问它
您可能还需要禁用默认的MOXy提供程序。比如说
@Consumes("application/json")
@Produces("application/json")
public class ValidatingMoxyProvider extends ConfigurableMoxyJsonProvider {
private final ValidationEventHandler handler = event -> {
System.out.println(event.getLinkedException());
System.out.println(event.getMessage());
return false;
};
@Override
protected void preReadFrom(final Class<Object> type,
final Type genericType,
final Annotation[] annotations,
final MediaType mediaType,
final MultivaluedMap<String, String> httpHeaders,
final Unmarshaller unmarshaller) throws JAXBException {
super.preReadFrom(type, genericType, annotations,
mediaType, httpHeaders, unmarshaller);
unmarshaller.setEventHandler(handler);
}
}
public ApplicationConfig() {
register(ValidatingMoxyProvider.class);
property(ServerProperties.MOXY_JSON_FEATURE_DISABLE, true);
}
@消耗(“应用程序/json”)
@生成(“应用程序/json”)
公共类ValidatingMoxyProvider扩展了ConfigurableMoxyJsonProvider{
private final ValidationEventHandler=事件->{
System.out.println(event.getLinkedException());
System.out.println(event.getMessage());
返回false;
};
@凌驾
受保护的void preReadFrom(最终类类型,
最终类型genericType,
最终注释[]注释,
最终MediaType MediaType,
最终多值MAP HttpHeader,
最终解组器(解组器)抛出JAXBEException{
super.preReadFrom(类型、genericType、注释、,
mediaType、HttpHeader、解组器);
unmarshaller.setEventHandler(handler);
}
}
公共应用程序配置(){
注册(ValidatingMoxyProvider.class);
属性(ServerProperties.MOXY\u JSON\u FEATURE\u DISABLE,true);
}
如果您担心您可能会禁用的功能,请查看。你不会错过任何东西,除非你正在使用