簡體   English   中英

找不到接口 org.springframework.data.domain.Pageable 的主要或默認構造函數

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

我試圖在我的 RestController 中實現 Pageable 並遇到此錯誤消息“No primary or default constructor found for interface org.springframework.data.domain.Pageable”的問題

我的控制器是

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

我在這里做錯了什么。 它是一個 Spring Boot 2.0 應用程序。 提前致謝!

選擇的解決方案是一種解決方法。 您可以使用此配置使 Spring 自動解析參數:

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

如果您使用Clément Poissonnier 的解決方案請檢查配置類是否不覆蓋另一個配置類

我遇到了同樣的問題,下面的解決方案無法解決:

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

我仍然有消息:

找不到接口 org.springframework.data.domain.Pageable 的主要或默認構造函數

然后我意識到該項目有一個 Swagger 配置類

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

並且上面的WebMvcConfig 配置被忽略了。

解決方案是只有一個配置類:

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

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

正如約翰保羅摩爾的回答所指出的那樣,您可能也不需要@EnableSpringDataWebSupport

我在使用 WebFlux 應用時遇到了同樣的問題。 Spring Boot 2.4.0 缺少響應式應用程序的相應@EnableSpringDataWebSupport ,但幸運的是提供了有效的解析器實現。 您可以通過以下方式啟用它:

@Configuration
public class PageableWebFluxConfiguration implements WebFluxConfigurer {

    @Override
    public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
        configurer.addCustomResolver(new ReactivePageableHandlerMethodArgumentResolver());
    }

}

我建議將您的控制器重構為

public List<ItemCategory> getAllItemCategoryByPage(@RequestParam("page") int pageIndex, 
                                                   @RequestParam("size") int pageSize){
     return itemCategoryService
                   .getAllItemCategoriesByPageable(PageRequest.of(pageIndex, pageSize)).getContent();
}

我認為您在 Pageable 之前缺少注釋(您如何從客戶端發送該數據?)。

只是添加到已經給出的關於使用注釋啟用 @EnableSpringDataWebSupport 的答案。 這應該已經通過 spring boot 自動配置啟用。 您可能需要檢查您的配置,或者是否使用 java config 或在應用程序屬性中排除了此自動配置類。

   org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration

對於非 Spring Boot 代碼:

我正在處理一些沒有spring boot的現有spring mvc代碼庫,已經在xmls中注冊了一個RequestMappingHandlerAdapter bean,所以我不得不這樣做

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        ...
        <property name="customArgumentResolvers">
            <list>
                <bean class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
                </bean>
            </list>
        </property>
    </bean>

您可以將測試配置為:

@BeforeEach
public void setup(){
  mockMvc = MockMvcBuilders.standaloneSetup(messageController)
    .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
    .build();
}

見說明https://www.javafixing.com/2021/12/fixed-request-is-not-being-executed-in.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM