Reputation: 2372
I have a Spring Boot application where I want (for a start) to detect whether my device is mobile or browser. Therefor I thought Spring mobile is a good idea. The problem is that I can't get it going.
CODE:
Based on the official docs here I implemented the following:
public class DeviceConfig implements WebMvcConfigurer{
@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());
}
}
and in my Controller
@RequestMapping("/user/mobile")
@PreAuthorize("hasRole('ROLE_USER') and hasRole('ROLE_MOBILE')")
public ModelAndView routeToUserInterfaceServiceMobile(final RedirectAttributes redirectAttrs,Device device) {
System.out.println(device.isMobile());
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
redirectAttrs.addFlashAttribute("session_userId", userAuthRepository.findByUsername(auth.getName()).getId());
return new ModelAndView("redirect:"+MyConstants.PATH_USER_MOBILE_SERVICE);
}
and the pom.xml additions:
<dependency>
<groupId>org.springframework.mobile</groupId>
<artifactId>spring-mobile-device</artifactId>
<version>2.0.0.M3</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
Problem:
I can run the application but when I call this endpoint I get the following error:
No primary or default constructor found for interface org.springframework.mobile.device.Device java.lang.IllegalStateException: No primary or default constructor found for interface org.springframework.mobile.device.Device at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:219) at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:84) at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:139) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:165) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) at
Is there something else to add? Or is there something to add to spring properties?
Upvotes: 0
Views: 1132