我使用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);
}
}
此外,我的应用程序上下文中还有下一个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>
最后我从客户那里做了下一次重新计划:
$.ajax({
type: "GET",
url: "getProducts?page=0&size=100",
.....
在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)
....................
请帮我解决这个问题,谢谢!
答案 0 :(得分:23)
最简单的方法是在配置中设置@EnableSpringDataWebSupport
。或者,在基于纯XML的配置中,将SpringDataWebConfiguration
声明为Spring bean。
这将确保正确注册必要的HandlerMethodArgumentResolver
。
答案 1 :(得分:19)
将以下内容添加到您的测试类:
@Inject
private PageableHandlerMethodArgumentResolver pageableArgumentResolver;
PageableHandlerMethodArgumentResolver
并在MockMvc设置期间配置它:
@Before
public void setup() {
...
this.mockMvc = MockMvcBuilders.standaloneSetup(resource)
.setCustomArgumentResolvers(pageableArgumentResolver)
.build();
}
答案 2 :(得分:2)
只是为了添加Tom Van Rossom的回复,如果你使用@RunWith(MockitoJUnitRunner.class),你可以在初始化mockMvc时创建一个PageableHandlerMethodArgumentResolver实例(就像Loren所提到的那样).Eg
mockMvc = MockMvcBuilders.standaloneSetup(restController)
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
.build();
答案 3 :(得分:0)
除了其他可能对您有用的答案之外的替代方法。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item" style="margin-bottom:15px">
<label><span class="item_title">Extra TV Remotes</span><p>One TV Remote is included with each Set Top Box. Order more below for an additional $20 each.</p>
<div class="styled-select">
<select id="extra_remote" name="extra_remote">
<option data-onetimeprice="0.00" data-label="" value="0_extra_remotes">0 Extra Remotes</option>
<option data-onetimeprice="20.00" data-label="1 Extra Remote" value="1_extra_remotes">1 Extra Remote (+$20)</option>
<option data-onetimeprice="40.00" data-label="2 Extra Remotes" value="2_extra_remotes">2 Extra Remotes (+$40)</option>
<option data-onetimeprice="60.00" data-label="3 Extra Remotes" value="3_extra_remotes">3 Extra Remotes (+$60)</option>
<option data-onetimeprice="80.00" data-label="4 Extra Remotes" value="4_extra_remotes">4 Extra Remotes (+$80)</option>
<option data-onetimeprice="100.00" data-label="5 Extra Remotes" value="5_extra_remotes">5 Extra Remotes (+$100)</option>
<option data-onetimeprice="120.00" data-label="6 Extra Remotes" value="6_extra_remotes">6 Extra Remotes (+$120)</option>
<option data-onetimeprice="140.00" data-label="7 Extra Remotes" value="7_extra_remotes">7 Extra Remotes (+$140)</option>
<option data-onetimeprice="160.00" data-label="8 Extra Remotes" value="8_extra_remotes">8 Extra Remotes (+$160)</option>
<option data-onetimeprice="180.00" data-label="9 Extra Remotes" value="9_extra_remotes">9 Extra Remotes (+$180)</option>
<option data-onetimeprice="200.00" data-label="10 Extra Remotes" value="10_extra_remotes">10 Extra Remotes (+$200)</option>
</select>
</div>
</label>
</div>
答案 4 :(得分:-1)
getItems方法中使用的Pageable是一个接口,spring将无法实例化它。
你可以