Spring Mobile: how to add DeviceWebArgumentResolver programmatically?
Asked Answered
B

1

7

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()));

    }

    // ...

}
Bureaucracy answered 25/7, 2012 at 16:13 Comment(0)
J
5

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.

Jonathanjonathon answered 25/7, 2012 at 16:40 Comment(1)
As of the 1.1.0.M1 release, Spring Mobile now includes a DeviceHandlerMethodArgumentResolver to support Java-based configuration.Lindblad

© 2022 - 2024 — McMap. All rights reserved.