我正在尝试实现一个 Controller 方法,类似于支持 QueryDsl 的 Spring Data 的最新 Gosling 版本中记录的方法。我已经实现了 Controller ,如 http://docs.spring.io/spring-data/jpa/docs/1.9.0.RELEASE/reference/html/#core.web.type-safe 文档中的示例所示.一切都在编译,当我启动应用程序时(使用 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)
我的猜测是 QuerydslPredicateArgumentResolver
没有应用于请求,因此是异常。但是当我查询 Spring Boot 管理端点 /manage/beans
时,我看到 QuerydslPredicateArgumentResolver
被注册为一个 bean。我还确保 @EnableSpringDataWebSupport
在我的 @Configuration
类中无效。
我用 @BasePathAwareController
注释了 Controller ,因为我将它与 Spring Data REST 一起使用,并且我希望这些方法位于与 Spring Data REST 公开的方法相似的路径下。我还尝试使用 @RepositoryRestController
,但这似乎并不重要。但是,当使用 @RestController
并将其放在与 Spring Data REST 使用的基本路径不同的路径下时,一切正常。所以问题是,它应该有效吗?
现在整个 Controller 是:
@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);
}
}
最佳答案
我在 Predicate 的实例化方面遇到了同样的问题。在示例中:
@Controller
@RequiredArgsConstructor(onConstructor = @__(@Autowired) )
class UserController {
private final UserRepository repository;
@RequestMapping(value = "/", method = RequestMethod.GET)
String index(Model model, //
@QuerydslPredicate(root = User.class) Predicate predicate, //
@PageableDefault(sort = { "lastname", "firstname" }) Pageable pageable, //
@RequestParam MultiValueMap<String, String> parameters) {
(...)
( https://github.com/spring-projects/spring-data-examples/blob/master/web/querydsl/src/main/java/example/users/web/UserController.java#L42 ) 只使用@Controller 而我使用@RepositoryRestController,这似乎就是原因。 @RestController 也适用于我。
关于java - 通过 REST Controller 使用 Spring Data JPA 和 QueryDsl 的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32486860/