Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从列表中删除第二项会使REST API崩溃_Java_List_Rest_For Loop_Http Status Code 500 - Fatal编程技术网

Java 从列表中删除第二项会使REST API崩溃

Java 从列表中删除第二项会使REST API崩溃,java,list,rest,for-loop,http-status-code-500,Java,List,Rest,For Loop,Http Status Code 500,我正在尝试向我的RESTAPI添加一个功能,它按年份过滤数据,这样只有对象才会显示与我请求的年份相同的数据 我用来测试此功能的路径是 http://localhost:8080/Assignment2C/beers?year=2019 这是一个500的错误 在Beers_Service.java中,第二次遍历列表时失败 public List<Beers> getAllBeersByYear(int year) { EntityManager em = DBUtil.getEM

我正在尝试向我的RESTAPI添加一个功能,它按年份过滤数据,这样只有对象才会显示与我请求的年份相同的数据

我用来测试此功能的路径是

  http://localhost:8080/Assignment2C/beers?year=2019
这是一个500的错误

在Beers_Service.java中,第二次遍历列表时失败

public List<Beers> getAllBeersByYear(int year) {
EntityManager em = DBUtil.getEMF().createEntityManager();

List<Beers> list = null;

try {
    list = em.createNamedQuery("Beers.findAll", Beers.class)
            .getResultList();
    if (list == null || list.isEmpty()) {
        list = null;
    }

} finally {
    em.close();
}
Calendar cal = Calendar.getInstance();
for(Beers beer: list){
    cal.setTime(beer.getLastMod());
    if(cal.get(Calendar.YEAR)!=year){

        list.remove(beer);
    }
}
return list;
当在中断for循环后处于调试模式时,它将在

                   beforeExecute(wt, task);
                    try {
                        task.run();
                        afterExecute(task, null);
                    } catch (Throwable ex) {
                        afterExecute(task, ex);
                        throw ex;
                    }
排队

afterExecute(task, null);
我得到的堆栈跟踪是

<h1>HTTP Status 500 – Internal Server Error</h1>
    <hr class="line" />
    <p><b>Type</b> Exception Report</p>
    <p><b>Message</b> Request processing failed; nested exception is java.util.ConcurrentModificationException</p>
    <p><b>Description</b> The server encountered an unexpected condition that prevented it from fulfilling the request.
    </p>
    <p><b>Exception</b></p>
    <pre>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.util.ConcurrentModificationException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
</pre>
    <p><b>Root Cause</b></p>
    <pre>java.util.ConcurrentModificationException
    java.base&#47;java.util.Vector$Itr.checkForComodification(Vector.java:1320)
    java.base&#47;java.util.Vector$Itr.next(Vector.java:1276)
    main.Beers_Service.getAllBeersByYear(Beers_Service.java:53)
    main.Beers_Controller.GetAllBreweries(Beers_Controller.java:72)
    java.base&#47;jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    java.base&#47;jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    java.base&#47;jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.base&#47;java.lang.reflect.Method.invoke(Method.java:567)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
HTTP状态500–内部服务器错误

消息请求处理失败;嵌套异常是java.util.ConcurrentModificationException

说明服务器遇到意外情况,无法满足请求。

例外情况

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是java.util.ConcurrentModificationException org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) javaservlet.http.HttpServlet.service(HttpServlet.java:634) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) javaservlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 根本原因

java.util.ConcurrentModificationException java.base/;Vector$Itr.checkForComodification(Vector.java:1320) java.base/;Vector$Itr.next(Vector.java:1276) main.Beers\u Service.getAllBeersByYear(Beers\u Service.java:53) main.Beers\u Controller.GetAllBreweries(Beers\u Controller.java:72) java.base/;jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法) java.base/;jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) java.base/;jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.base/;java.lang.reflect.Method.invoke(Method.java:567) org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) javaservlet.http.HttpServlet.service(HttpServlet.java:634) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) javaservlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
在对集合进行迭代时,不应修改集合:

例如,如果线程在使用fail fast迭代器迭代集合时直接修改集合,迭代器将引发此异常

<h1>HTTP Status 500 – Internal Server Error</h1>
    <hr class="line" />
    <p><b>Type</b> Exception Report</p>
    <p><b>Message</b> Request processing failed; nested exception is java.util.ConcurrentModificationException</p>
    <p><b>Description</b> The server encountered an unexpected condition that prevented it from fulfilling the request.
    </p>
    <p><b>Exception</b></p>
    <pre>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.util.ConcurrentModificationException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
</pre>
    <p><b>Root Cause</b></p>
    <pre>java.util.ConcurrentModificationException
    java.base&#47;java.util.Vector$Itr.checkForComodification(Vector.java:1320)
    java.base&#47;java.util.Vector$Itr.next(Vector.java:1276)
    main.Beers_Service.getAllBeersByYear(Beers_Service.java:53)
    main.Beers_Controller.GetAllBreweries(Beers_Controller.java:72)
    java.base&#47;jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    java.base&#47;jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    java.base&#47;jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.base&#47;java.lang.reflect.Method.invoke(Method.java:567)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)