Spring@MatrixVariable
最近,我尝试使用Spring开发简单的Web应用程序。不幸的是,昨天我遇到了自己无法解决的问题: 1.介绍 我有以下方法,从URL收集价格范围:Spring@MatrixVariable,spring,spring-mvc,java-8,Spring,Spring Mvc,Java 8,最近,我尝试使用Spring开发简单的Web应用程序。不幸的是,昨天我遇到了自己无法解决的问题: 1.介绍 我有以下方法,从URL收集价格范围: @RequestMapping("/filter/price/{price}") public String getProductsByPrice(Model model, @MatrixVariable(pathVar = "price") Map<String, Integer> filterParams) {
@RequestMapping("/filter/price/{price}")
public String getProductsByPrice(Model model, @MatrixVariable(pathVar = "price") Map<String, Integer> filterParams) {
model.addAttribute("items", productService.getProductsByPriceFilter(filterParams));
return PRODUCTS;
}
3.问题:
有人能告诉我如何使用我的方法从一开始提到的URL中提取所需的值(以下键的值:高和低)?我正在尝试streams()等,但不幸的是每次都会遇到相同的异常。您使用的是哪个spring版本?您可以发布完整的错误堆栈跟踪吗?@Rohan堆栈跟踪更新,Spring版本4.0.3.RELEASE。您是否获得任何值映射
filterParams
?尝试在控制器中打印地图内容。此外,错误似乎出现在MemoryProductRepository.getProductsByPriceFilter中。请也发布该文件。@Rohan请仔细阅读我的评论,getProductsByPriceFilter方法已经存在。是的,filterParams不是空的。可能是我的评论不清楚。关于2.问题
,从错误堆栈跟踪java.lang.ClassCastException:java.util.LinkedList无法转换为java.lang.Integer.com.packt.webstore.domain.repository.impl.InMemoryProductRepository.getProductsByPriceFilter(InMemoryProductRepository.java:129)
,代码似乎正在尝试将LinkedList分配给Integer。查看MemoryProductRepository中的代码会给出更多的想法。
@Override
public Set<Product> getProductsByPriceFilter(Map<String, Integer> filterParams) {
Set<Product> productsByPrice = new HashSet<>();
Set<String> criterias = filterParams.keySet();
// start of the problem
BigDecimal minimalPrice = new BigDecimal(filterParams.get("LOW"));
BigDecimal maximalPrice = new BigDecimal(filterParams.get("HIGH"));
//end of the problem
if (criterias.contains(LOW) && criterias.contains(HIGH)) {
for (Product product : listOfProducts) {
if ((minimalPrice.compareTo(product.getUnitPrice()) == -1) && (maximalPrice.compareTo(product.getUnitPrice()) == 1)) {
productsByPrice.add(product);
}
}
} else if (criterias.contains(LOW)) {
for (Product product : listOfProducts) {
if ((minimalPrice.compareTo(product.getUnitPrice()) == -1)) {
productsByPrice.add(product);
}
}
} else if (criterias.contains(HIGH)) {
for (Product product : listOfProducts) {
if ((maximalPrice.compareTo(product.getUnitPrice()) == 1)) {
productsByPrice.add(product);
}
}
}
return productsByPrice;
}
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: java.util.LinkedList cannot be cast to java.lang.Integer
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
root cause
java.lang.ClassCastException: java.util.LinkedList cannot be cast to java.lang.Integer
com.packt.webstore.domain.repository.impl.InMemoryProductRepository.getProductsByPriceFilter(InMemoryProductRepository.java:129)
com.packt.webstore.service.impl.ProductServiceImpl.getProductsByPriceFilter(ProductServiceImpl.java:46)
com.packt.webstore.controller.ProductController.getProductsByPrice(ProductController.java:78)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)