在与messageconverter相关的基于Java的配置中配置RequestMappingHandlerAdapter

时间:2019-02-14 17:46:20

标签: java json spring-mvc jackson

我正在开发RestFul Web服务,我是初学者,我想在我的基于Java的配置中分别对应以下基于xml的配置RequestMappingHandlerAdapter

<!-- Configure to plugin JSON as request and response in method handler -->
    <beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <beans:property name="messageConverters">
            <beans:list>
                <beans:ref bean="jsonMessageConverter"/>
            </beans:list>
        </beans:property>
    </beans:bean>

    <!-- Configure bean to convert JSON to POJO and vice versa -->
    <beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </beans:bean>

基于Java的配置如下:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.mzk.mascot")
public class DispatcherServletConfiguration implements WebMvcConfigurer {
    @Bean
    public MappingJackson2HttpMessageConverter converter() {
        MappingJackson2HttpMessageConverter httpMessageConverter = new MappingJackson2HttpMessageConverter();
        return httpMessageConverter;
    }



@Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(converter());

    }
  }

根据XML,我需要RequestMappingHandlerAdapter的等效配置

0 个答案:

没有答案