gpt4 book ai didi

java - 我在 lambda 表达式中找不到结果

转载 作者:行者123 更新时间:2023-11-30 02:49:27 26 4
gpt4 key购买 nike

我将此 json 发送到我的 tomcat 服务


{
"room": "Platinum",
"products": [{
"name": "Agua",
"quantity": 2
}],
"observation": "",
"date": "2016-08-15 12:00:00",
"status": 0
}

此代码使用我的 json 并在数据库中设置数据。



@Transactional
@RequestMapping(value = "/get-order", method = RequestMethod.POST, produces = PRODUCES_JSON)
public ResponseEntity getOrder(@RequestBody String content)
throws ParseException, UnsupportedEncodingException {
try {
String result = java.net.URLDecoder.decode(content, "UTF-8");
JSONObject json = new JSONObject(result);

Order order = createOrder(json);

@SuppressWarnings("unchecked")
List products = em.createNamedQuery(Product.QUERY_LIST).getResultList();

JSONArray productsJsonArray = json.getJSONArray(JSON_PRODUCTS);
for (int i = 0; i product = products.stream().filter(p -> p.getName().equals(name)).findAny();
order.addProduct(product.get(), quantity);
}
em.persist(order);


return buildGetOrderResponse(order);

} catch (NoResultException e) {
LOGGER.error(e.getMessage(), e);
return buildErrorResponse(WebServiceError.NOT_FOUND);
}
}

当我运行调试时,显示了此行 order.addProduct(product.get(),Quantity); 中的错误

但是代码在日志错误中返回此内容:



ago 26, 2016 5:08:12 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [dispatcher] in context with path [/easy-coffee] threw exception [Request processing failed; nested exception is java.util.NoSuchElementException: No value present] with root cause
java.util.NoSuchElementException: No value present
at java.util.Optional.get(Optional.java:135)
at br.com.fourward.easycoffee.webservices.ConsummationWebService.getOrder(ConsummationWebService.java:89)
at br.com.fourward.easycoffee.webservices.ConsummationWebService$$FastClassBySpringCGLIB$$afa221ad.invoke()
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
at br.com.fourward.easycoffee.webservices.ConsummationWebService$$EnhancerBySpringCGLIB$$60d13b8.getOrder()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

我需要一点帮助,我是 jpa 的新手......谢谢你们

最佳答案

根据 Java 文档,Optional.get()(用作 addProduct 方法的参数)返回一个值,如果没有值,则返回 NoSuchElementException。仅当 Product.isPresent() 返回 true 时才应调用 addProduct()。

关于java - 我在 lambda 表达式中找不到结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39174090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com