简体   繁体   English

为基于注释的Spring MVC控制器设置混合配置

[英]Setting up a mixed configuration for annotation-based Spring MVC controllers

I have a number of controllers with various request handlers in my Spring 3.x project (all annotation-based, using @Controller and @RequestMapping ). 我的Spring 3.x项目中有许多带有各种请求处理程序的控制器(所有基于注释的,使用@Controller@RequestMapping )。

Currently, the application context just defines DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans. 目前,应用程序上下文只定义了DefaultAnnotationHandlerMappingAnnotationMethodHandlerAdapter bean。 If I understand it correctly, these could also be replaced with <mvc:annotation-driven/> . 如果我理解正确,也可以用<mvc:annotation-driven/>替换它们。
The existing controllers mostly fill a model that is passed in via the parameters and then return a view name as a string. 现有的控制器主要填充通过参数传入的模型,然后将视图名称作为字符串返回。 The mapping is done by standard DefaultRequestToViewNameTranslator and InternalResourceViewResolver beans. 映射由标准DefaultRequestToViewNameTranslatorInternalResourceViewResolver bean完成。

Now I would like to introduce a new controller, which needs an HttpMessageConverter (it will be a MappingJacksonHttpMessageConverter ) and a HandlerExceptionResolver specific to this controller. 现在我想介绍一个新的控制器,它需要一个HttpMessageConverter (它将是一个MappingJacksonHttpMessageConverter )和一个特定于该控制器的HandlerExceptionResolver

The existing controllers should not be affected in any way. 不应以任何方式影响现有控制器。 Neither should their requests and responses be converted by the message converter, nor should any exceptions be handled by the exception resolver. 它们的请求和响应都不应由消息转换器转换,异常解析器也不应处理任何异常。


Is there a way to do this without dropping annotation-based configuration for the new controller? 有没有办法在不删除新控制器的基于注释的配置的情况下执行此操作? Is there a way to set the message converter and the exception resolver specifically for one controller, without giving up the URL routing based on @RequestMapping ? 有没有办法专门为一个控制器设置消息转换器和异常解析器,而不放弃基于@RequestMapping的URL路由?

Or is there maybe a way to choose a converter/resolver configuration using an annotation on the controller? 或者有没有办法在控制器上使用注释选择转换器/解析器配置?

If not, what's the next best approach to do this? 如果没有,那么下一个最佳方法是什么?

If you annotate your method with @ExceptionHandler as it is stated here it will only handle the exceptions thrown by the methods of the controller where it is placed. 如果您使用@ExceptionHandler注释您的方法,因为它在此处说明它将仅处理放置它的控制器方法抛出的异常。 So it won't affect the other ones. 所以它不会影响其他的。

Regarding HttpMessageConverter, I'm not sure if what I'm going to say can be applied to HttpMessageConverter (because I have never used it and I'm not sure if it can be treated as other converters), but if you can create a conversionService with it you could do something like this in the controller: 关于HttpMessageConverter,我不确定我要说的是否可以应用于HttpMessageConverter(因为我从未使用它,我不确定它是否可以被视为其他转换器),但是如果你可以创建一个用它来转换你可以在控制器中做这样的事情:

@Autowired
private ConversionService conversionService;

@InitBinder
public void initBinder(WebDataBinder binder){
    binder.setConversionService(conversionService);
}

and the conversionService will only be applied to the controller method of this initBinder. 而conversionService只会应用于此initBinder的控制器方法。

In the special case that there's no overlapping of media types between your controllers (eg one accepts and responds only with JSON, all others receive/respond with XML), you can rely on the Content-Type and Accept header matching of Spring to do the mapping to the corresponding HttpMessageConverter for you. 在特殊情况下,控制器之间没有媒体类型的重叠(例如,一个接受并仅响应JSON,所有其他接收/响应XML),您可以依赖于Spring的Content-TypeAccept头匹配来执行映射到相应的HttpMessageConverter。

This does not resolve the original question, though. 但这并不能解决原始问题。 It is just a workaround if you're lucky enough to be in this special situation. 如果您足够幸运能够处于这种特殊情况,这只是一种解决方法。 You also can't prevent one controller to unterstand/respond with a media type it wasn't supposed to support this way. 您也无法阻止一个控制器取消/响应它不应该支持这种方式的媒体类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FactoryBeans和Spring 3.0中基于注释的配置 - FactoryBeans and the annotation-based configuration in Spring 3.0 基于Spring注释的配置中的mvc:default-servlet-handler的等价物? - Equivalent of mvc:default-servlet-handler in Spring annotation-based configuration? 如果它在 jar 文件中,则基于 Spring Annotation 的控制器不起作用 - Spring Annotation-based controllers not working if it is inside jar file 在Spring MVC中设置基于注释的SpyMemcached配置 - Setting annotation based SpyMemcached configuration in spring mvc 如何在使用基于spring注释的配置时配置MappingJacksonHttpMessageConverter? - How to configure MappingJacksonHttpMessageConverter while using spring annotation-based configuration? 缩小Spring MVC基于注释的控制器和@RequestMapping的问题 - Narrowing problem with Spring MVC annotation-based controller and @RequestMapping 基于spring注释的配置 - 内存消耗过高? - spring annotation-based configuration - memory consumption too high? Spring 基于注释的 DI 与 xml 配置? - Spring annotation-based DI vs xml configuration? 使用基于SpringMVC注释的配置进行发音 - Enunciate with SpringMVC Annotation-based configuration 具有超类的基于Spring框架注释的事务 - Spring framework annotation-based transactions with superclass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM