使用 jax2bMarshaller 在 spring mvc 上自动 marshalling/unmarshalling 验证
Automatic marshalling/unmarshalling validation on spring mvc with jax2bMarshaller
在我的 spring mvc 项目中,我想解组和验证 xml 请求并编组和验证 xml 响应。现在在我的 spring 配置文件中我做了:
@Bean
public MarshallingHttpMessageConverter marshallingMessageConverter() throws Exception {
MarshallingHttpMessageConverter marshallingHttpMessageConverter = new MarshallingHttpMessageConverter();
marshallingHttpMessageConverter.setMarshaller(jaxb2Marshaller());
marshallingHttpMessageConverter.setUnmarshaller(jaxb2Marshaller());
return marshallingHttpMessageConverter;
}
@Bean
public Jaxb2Marshaller jaxb2Marshaller() throws Exception {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(BillDataRequest.class);
marshaller.setSchema(new ClassPathResource("file.xsd"));
marshaller.setValidationEventHandler(new MyValidationEventHandler());
marshaller.afterPropertiesSet();
return marshaller;
}
当在我的控制器中注入 jaxMarshaller 并调用 unmarshall 方法时,一切正常。有没有办法告诉 Spring 自动解组和验证请求?谢谢
您可以创建自己的拦截器。创建一个实现 HandlerInterceptor.
的 class
在您的 WebConfig 中将拦截器添加到方法中:
@Override
public void addInterceptors(InterceptorRegistry registry)
或者您可以使用 Spring AOP 来做到这一点。
您可以向 RequestMappingHandlerAdapter 提供转换器。将带有转换器的 RequestMappingHandlerAdapter 添加到您的 spring 配置文件:
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter(MarshallingHttpMessageConverter marshallingHttpMessageConverter) {
RequestMappingHandlerAdapter requestMappingHandlerAdapter = new RequestMappingHandlerAdapter();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(marshallingHttpMessageConverter);
requestMappingHandlerAdapter.setMessageConverters(messageConverters);
return requestMappingHandlerAdapter;
}
在我的 spring mvc 项目中,我想解组和验证 xml 请求并编组和验证 xml 响应。现在在我的 spring 配置文件中我做了:
@Bean
public MarshallingHttpMessageConverter marshallingMessageConverter() throws Exception {
MarshallingHttpMessageConverter marshallingHttpMessageConverter = new MarshallingHttpMessageConverter();
marshallingHttpMessageConverter.setMarshaller(jaxb2Marshaller());
marshallingHttpMessageConverter.setUnmarshaller(jaxb2Marshaller());
return marshallingHttpMessageConverter;
}
@Bean
public Jaxb2Marshaller jaxb2Marshaller() throws Exception {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(BillDataRequest.class);
marshaller.setSchema(new ClassPathResource("file.xsd"));
marshaller.setValidationEventHandler(new MyValidationEventHandler());
marshaller.afterPropertiesSet();
return marshaller;
}
当在我的控制器中注入 jaxMarshaller 并调用 unmarshall 方法时,一切正常。有没有办法告诉 Spring 自动解组和验证请求?谢谢
您可以创建自己的拦截器。创建一个实现 HandlerInterceptor.
的 class在您的 WebConfig 中将拦截器添加到方法中:
@Override
public void addInterceptors(InterceptorRegistry registry)
或者您可以使用 Spring AOP 来做到这一点。
您可以向 RequestMappingHandlerAdapter 提供转换器。将带有转换器的 RequestMappingHandlerAdapter 添加到您的 spring 配置文件:
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter(MarshallingHttpMessageConverter marshallingHttpMessageConverter) {
RequestMappingHandlerAdapter requestMappingHandlerAdapter = new RequestMappingHandlerAdapter();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(marshallingHttpMessageConverter);
requestMappingHandlerAdapter.setMessageConverters(messageConverters);
return requestMappingHandlerAdapter;
}