Spring Boot 2 NoSuchMethodException: org.springframework.mobile.device.Device.<init>()

删除回忆录丶 提交于 2019-12-05 10:20:21

Spring Boot Starter Mobile is now deprecated is not part of Spring Boot 2. It's mentioned in the Migration Guide under "Removed Features".

I have configured manually and resolved this issue

Device detection

Add the following Java-based configuration to enable device detection in a Spring web application:

@Bean
public DeviceResolverHandlerInterceptor 
        deviceResolverHandlerInterceptor() {
    return new DeviceResolverHandlerInterceptor();
}

@Bean
public DeviceHandlerMethodArgumentResolver 
        deviceHandlerMethodArgumentResolver() {
    return new DeviceHandlerMethodArgumentResolver();
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(deviceResolverHandlerInterceptor());
}

@Override
public void addArgumentResolvers(
       List<HandlerMethodArgumentResolver> argumentResolvers) {
   argumentResolvers.add(deviceHandlerMethodArgumentResolver());
}

Inject the Device into your controller

@Controller
public class HomeController {

private static final Logger logger = 
        LoggerFactory.getLogger(HomeController.class);

@RequestMapping("/")
public void home(Device device) {
    if (device.isMobile()) {
        logger.info("Hello mobile user!");
    } else if (device.isTablet()) {
        logger.info("Hello tablet user!");
    } else {
        logger.info("Hello desktop user!");         
    }
}

}

http://projects.spring.io/spring-mobile/

As @erhanasikoglu pointed out;

Since WebMvcConfigurerAdapter is deprecated, only need to implements WebMvcConfigurer interface inside that configuration class. (https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.html)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!