Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Mobile: how to add DeviceWebArgumentResolver programmatically?

Spring mobile documentation suggest adding the following configuration:

<mvc:annotation-driven>
    <mvc:argument-resolvers>
        <beans:bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
    </mvc:argument-resolvers>
</mvc:annotation-driven>

to pass the current Device object as an argument to @Controller methods.

Yet, one can use:

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

}

and bypass <mvc:annotation-driven> configuration.

Then, how to add a DeviceWebArgumentResolver programmatically?

Solution (Luciano):

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addArgumentResolvers(
        List<HandlerMethodArgumentResolver> argumentResolvers) {

        // Adding Spring mobile argument resolvers
        argumentResolvers.add(
            new ServletWebArgumentResolverAdapter(
                new DeviceWebArgumentResolver()));

        argumentResolvers.add(
            new ServletWebArgumentResolverAdapter(
                new SitePreferenceWebArgumentResolver()));

    }

    // ...

}
like image 962
Jérôme Verstrynge Avatar asked Jul 25 '12 16:07

Jérôme Verstrynge


1 Answers

WebMvcConfigurerAdapter contains a method named addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers)

That is the method you need to implement, but first you need to convert a WebArgumentResolver (in this case, DeviceWebArgumentResolver) into a HandlerMethodArgumentResolver. For that, there exists a class named ServletWebArgumentResolverAdapter.

like image 112
Luciano Avatar answered Sep 25 '22 20:09

Luciano