Dim*_*mon 17 java spring spring-mvc spring-data-jpa
我使用Spring 4.1.6.RELEASE和Spring Data Jpa 1.8.0.RELEASE.我有org.springframework.data.domain.Pageable bean创建的问题.它在我的控制器中使用:
@Controller
public class ItemsController {
@Autowired
ProductService itemsService;
@RequestMapping(value = "/openItemsPage")
public String openItemsPage() {
return "items";
}
@RequestMapping(value = "/getItems", method = RequestMethod.GET)
@ResponseBody
public Item[] getItems(Pageable pageable) {
return itemsService.getItems(pageable);
}
}
Run Code Online (Sandbox Code Playgroud)
此外,我在我的应用程序上下文中有下一个xml配置:
<context:component-scan base-package="com.mobox.controller" />
<mvc:annotation-driven>
<mvc:argument-resolvers>
<beans:bean id="sortResolver"
class="org.springframework.data.web.SortHandlerMethodArgumentResolver" />
<beans:bean
class="org.springframework.data.web.PageableHandlerMethodArgumentResolver">
<beans:constructor-arg ref="sortResolver" />
</beans:bean>
</mvc:argument-resolvers>
</mvc:annotation-driven>
Run Code Online (Sandbox Code Playgroud)
最后,我做了客户的下一次重新计划:
$.ajax({
type: "GET",
url: "getProducts?page=0&size=100",
.....
Run Code Online (Sandbox Code Playgroud)
在tomcat日志中我看到下一个:
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/a2delivery-web] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.domain.Pageable]: Specified class is an interface] with root cause
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.domain.Pageable]: 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)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:106)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
....................
Run Code Online (Sandbox Code Playgroud)
请帮我解决这个问题,谢谢!
Oli*_*ohm 25
实现此功能的最简单方法是@EnableSpringDataWebSupport在您的配置中进行设置.或者,在基于纯XML的配置中,声明SpringDataWebConfiguration为Spring bean.
这将确保必要的HandlerMethodArgumentResolver正确注册.
Tom*_*som 20
将以下内容添加到您的测试类中:
@Inject
private PageableHandlerMethodArgumentResolver pageableArgumentResolver;
Run Code Online (Sandbox Code Playgroud)
PageableHandlerMethodArgumentResolver
并在MockMvc设置期间配置它:
@Before
public void setup() {
...
this.mockMvc = MockMvcBuilders.standaloneSetup(resource)
.setCustomArgumentResolvers(pageableArgumentResolver)
.build();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14198 次 |
| 最近记录: |