Mar*_*tti 13 java querydsl spring-data spring-data-jpa spring-data-rest
我正在尝试实现一种控制器方法,类似于支持QueryDsl的最新Gosling发布的Spring Data发布系列中记录的方法.我已经实现了控制器,如http://docs.spring.io/spring-data/jpa/docs/1.9.0.RELEASE/reference/html/#core.web.type-文档中的示例所示.安全.一切都在编译,当我启动应用程序时(使用Spring Boot 1.2.5.RELEASE),一切都很顺利.
但是,当我尝试调用我的rest端点时,我总是得到以下异常:
org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mysema.query.types.Predicate]: Specified class is an interface
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:80)
Run Code Online (Sandbox Code Playgroud)
我的猜测是,QuerydslPredicateArgumentResolver没有应用于请求,因此异常.但是QuerydslPredicateArgumentResolver当我查询Spring Boot管理端点时,我看到它被注册为bean /manage/beans.我也确保@EnableSpringDataWebSupport我的@Configuration班级没有效果.
我对控制器进行了注释@BasePathAwareController,因为我在Spring Data REST中使用它,我希望这些方法与Spring Data REST公开的方法类似.我也尝试过使用@RepositoryRestController,但这似乎并不重要.但是,当使用@RestController并将其放在与Spring Data REST正在使用的基本路径不同的路径下时,一切正常.所以问题是,它应该有效吗?
现在整个控制器是:
@RestController
@RequestMapping(value = "/query")
public class AvailController
{
private final AvailRepository repo;
@Autowired
public AvailController(AvailRepository repository)
{
this.repo = repository;
}
@RequestMapping(value = "/avails", method = GET)
public @ResponseBody Page<Avail> getAvails(Model model,
@QuerydslPredicate(root = Avail.class) Predicate predicate,
Pageable pageable,
@RequestParam MultiValueMap<String, String> parameters)
{
return repo.findAll(predicate, pageable);
}
}
Run Code Online (Sandbox Code Playgroud)
Eri*_*der -1
当我尝试实现一个将返回值模仿为 Spring Data REST 的自定义控制器时,我也遇到了这个问题。我想将 QuerydslPredicate 注入到控制器方法中,但得到了恼人的“BeanInstantiationException”。
我通过将以下配置文件添加到我的应用程序中找到了解决此问题的方法:
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE )
public class MvcConfig extends WebMvcConfigurerAdapter {
@Autowired
@Qualifier("repositoryExporterHandlerAdapter")
RequestMappingHandlerAdapter repositoryExporterHandlerAdapter;
@Override
public void addArgumentResolvers(
List<HandlerMethodArgumentResolver> argumentResolvers) {
List<HandlerMethodArgumentResolver> customArgumentResolvers = repositoryExporterHandlerAdapter.getCustomArgumentResolvers();
argumentResolvers.addAll(customArgumentResolvers);
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅此处参考:https://jira.spring.io/browse/DATAREST-657