我使用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 …Run Code Online (Sandbox Code Playgroud) 我已经使用WebMvcConfigurerAdapter已有一段时间了。由于无法使用getInterceptors()方法获取所有已注册的拦截器,因此我切换到了WebMvcConfigurationSupport,它具有许多默认的已注册Spring Bean,例如ContentNegotiationManager,ExceptionHandlerExceptionResolverusw。
现在,我已经意识到,尽管我在WebConfig类上使用了@EnableSpringDataWebSupport批注,但非常方便的DomainClassConverter(使用CrudRepository将域类ID转换为域类对象)并未默认注册。
当我像这样显式定义此bean时,它就会起作用。
@EnableSpringDataWebSupport
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Bean
public DomainClassConverter<?> domainClassConverter() {
return new DomainClassConverter<FormattingConversionService>(mvcConversionService());
}
}
Run Code Online (Sandbox Code Playgroud)
但是,为什么EnableSpringDataWebSupport无法与WebMvcConfigurationSupport一起使用?
这是我正在处理的WebConfig代码:
package hello.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/greeting").setViewName("greeting");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的Application.class
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@SpringBootApplication
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎是一个Spring-boot问题,在某些系统中不会调用这些类方法.相应的问题报告在:https: //github.com/spring-projects/spring-boot/issues/2870
我的问题是,我们可以将此类中映射的资源映射到此类之外作为临时解决方法吗?
如果是,我们该怎么做?
更新:按照Andy Wilkinson的建议我删除@EnableWebMvc了,演示应用程序开始工作.然后我尝试逐个删除项目文件,以查看错误消失的点.我发现我在项目中有两个类,一个是扩展的WebMvcConfigurationSupport,第二个来自WebMvcConfigurerAdapter.从项目中删除前一个类修复了错误. …