Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No primary or default constructor found for interface org.springframework.data.domain.Pageable

Tags:

I was trying to implement Pageable in my RestController and running into issues with this error message "No primary or default constructor found for interface org.springframework.data.domain.Pageable"

My Controller is

@GetMapping("/rest/category/all/page")
public Page<ItemCategory> getAllItemCategoryByPage(Pageable pageable){
    Page<ItemCategory> categories = itemCategoryService.getAllItemCategoriesByPageable(pageable);
    return categories;
}

What am I doing wrong here. It is a Spring Boot 2.0 Application. Thanks in advance!

like image 657
Sovan Misra Avatar asked Sep 16 '18 15:09

Sovan Misra


2 Answers

The selected solution is a workaround. You can make Spring resolve the parameters automatically using this configuration :

import org.springframework.context.annotation.Configuration;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@EnableSpringDataWebSupport
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add( new PageableHandlerMethodArgumentResolver());
    }
}
like image 65
Clément Poissonnier Avatar answered Sep 19 '22 13:09

Clément Poissonnier


If you use Clément Poissonnier's solution, check if a configuration class do not override another one.

I had the same problem and the solution below could not fix it:

@Configuration
@EnableSpringDataWebSupport
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add( new PageableHandlerMethodArgumentResolver());
    }
}

I still had the message:

No primary or default constructor found for interface org.springframework.data.domain.Pageable

I then realized the project had a Swagger configuration class:

@Configuration
@EnableSwagger2
public class SwaggerConfiguration extends WebMvcConfigurationSupport {
    // Swagger configuration...
}

and that the above WebMvcConfig configuration was ignored.

The solution was to have only one configuration class:

@Configuration
@EnableSwagger2
public class WebMvcConfig extends WebMvcConfigurationSupport {
    // Swagger configuration...

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

You might also no need @EnableSpringDataWebSupport as pointed by John Paul Moore's answer

like image 42
Mathieu Gemard Avatar answered Sep 17 '22 13:09

Mathieu Gemard