Spring控制器使用ArrayIndexOutOfBoundsException为servlet分配异常

时间:2017-11-03 09:08:45

标签: java spring spring-mvc servlets model-view-controller

嗨,大家好我的一个控制器出现问题,当我去调用它时会抛出一个ArrayIndexOutOfBoundsException,我不明白为什么。我已经尝试过一遍又一遍地改变web.xml的配置和所有内容。尝试调用/ categories时,我收到以下异常:

SEVERE: StandardWrapper.Throwable
java.lang.ArrayIndexOutOfBoundsException: 3145
    at org.springframework.asm.ClassReader.readClass(Unknown Source)
    at org.springframework.asm.ClassReader.accept(Unknown Source)
    at org.springframework.asm.ClassReader.accept(Unknown Source)
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:54)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
    at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.checkConfigurationClassCandidate(ConfigurationClassBeanDefinitionReader.java:302)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:172)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:142)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:599)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:864)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1720)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1679)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

Nov 03, 2017 8:59:19 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet rest
java.lang.ArrayIndexOutOfBoundsException: 3145
    at org.springframework.asm.ClassReader.readClass(Unknown Source)
    at org.springframework.asm.ClassReader.accept(Unknown Source)
    at org.springframework.asm.ClassReader.accept(Unknown Source)
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:54)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
    at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.checkConfigurationClassCandidate(ConfigurationClassBeanDefinitionReader.java:302)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:172)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:142)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:599)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:864)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1720)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1679)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

这是FinderController类:

package uk.co.igindex.multilingualtranslation.application.rest;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import uk.co.igindex.multilingualtranslation.application.rest.response.FinderTranslationResponse;
import uk.co.igindex.multilingualtranslation.domain.Locale;
import uk.co.igindex.multilingualtranslation.eai.cache.FinderTranslationCache;
import uk.co.igindex.multilingualtranslation.eai.cache.TranslationCache;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/finder")
public class FinderController {

   FinderTranslationCache finderCache;

   TranslationCache translationCache;

   @Autowired
   public FinderController(FinderTranslationCache finderTranslationCache, TranslationCache translationCache) {
      this.finderCache = finderTranslationCache;
      this.translationCache = translationCache;
   }

   @RequestMapping(value = "/underlyings", method = RequestMethod.GET, headers = "content-type=application/json")
   @ResponseBody
   public String getUnderlyingTranslations() {
      FinderTranslationResponse f =  new  FinderTranslationResponse();

         for (Locale localeKey : finderCache.getUnderlyingTranslationKeys().keySet()) {
            for (String combinedKey : finderCache.getUnderlyingTranslationKeys().get(localeKey).keySet()) {
               f.addLocaleTranslation(localeKey.getValue(), finderCache.getUnderlyingTranslationKeys().get(localeKey).get(combinedKey),
                  translationCache.getCacheEntryFromCombinedKey(combinedKey));
            }
         }
      return jsonify(f.returnLocaleMap());
   }

   @RequestMapping(value = "/contracts", method = RequestMethod.GET, headers = "content-type=application/json")
   @ResponseBody
   public String getContractTranslations() {
      FinderTranslationResponse f =  new  FinderTranslationResponse();
      f.addLocaleTranslation("en_GB", "translationKey", "translationValue");
      f.addLocaleTranslation("en_GB", "translationKeysd", "translationValue");
      f.addLocaleTranslation("en_GB", "translationKey3", "translationValue");

      return jsonify(f.returnLocaleMap());
   }

   @RequestMapping(value = "/categories", method = RequestMethod.GET, headers = "content-type=application/json")
   @ResponseBody
   public String getCategoryList() {
      StringBuilder sb = new StringBuilder();
      for (String s : finderCache.getCategory()) {
         sb.append(s + "\n");
      }
      return sb.toString();
   }

   @RequestMapping(value = "/miscellaneous", method = RequestMethod.POST, headers = "content-type=application/json")
   @ResponseBody
   public Map<String, String> getMiscellaneousTranslations(Set<String> words, String locale){
      return new HashMap<>();
   }

   public String jsonify(Map map) {
      JSONObject jsonObject = new JSONObject();
      jsonObject.putAll(map);
      return jsonObject.toString();
   }

   public void setTranslationCache(TranslationCache translationCache) {
      this.translationCache = translationCache;
   }

   public void setFinderCache(FinderTranslationCache finderCache) {
      this.finderCache = finderCache;
   }

}

这是web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <description>Multi-lingual Translation Web Application.</description>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:properties/multilingualtranslation/multilingualtranslation-log4j.properties</param-value>
    </context-param>
    <context-param>
        <param-name>log4jExposeWebAppRoot</param-name>
        <param-value>false</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:MultilingualTranslation-jdbc.xml,
                     classpath*:MultilingualTranslation-config.xml,
                     classpath*:MultilingualTranslation-service.xml,
                     classpath*:MultilingualTranslation-jms.sf.xml,
                     classpath*:MultilingualTranslation-jndi.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>uk.co.igindex.multilingualtranslation.application.listener.MultilingualTranslationsContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>MultilingualTranslationDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- <init-param>
            <param-name>wl-dispatch-policy</param-name>
            <param-value>ClientVerificationWorkManager</param-value>
        </init-param>-->
        <load-on-startup>1</load-on-startup>
    </servlet>

   <servlet>
      <servlet-name>rest</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/rest-servlet.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>rest</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

    <!--
        Remoting servlet to support remote calls over HTTP (i.e. http
        invoker).
    -->
    <servlet>
        <servlet-name>remoting</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>remoting</servlet-name>
        <url-pattern>/remoting/*</url-pattern>
    </servlet-mapping>

 <!--
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <display-name>CXF Servlet</display-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/webservice/*</url-pattern>
    </servlet-mapping>-->

    <servlet-mapping>
        <servlet-name>MultilingualTranslationDispatcher</servlet-name>
        <url-pattern>/translation</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>MultilingualTranslationDispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <!-- MONITOR -->
    <servlet>
        <servlet-name>monitor</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/monitor-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>monitor</servlet-name>
        <url-pattern>/monitor/*</url-pattern>
    </servlet-mapping>

    <resource-ref>
       <description>Oracle DB Connection</description>
       <res-ref-name>multilingualtranslationservice/jdbc/datasource/MultilingualTranslationService</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
   </resource-ref>
</web-app>

这是rest-servlet.xml类

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">

   <context:component-scan base-package="uk.co.igindex.multilingualtranslation.application.rest" />

   <mvc:annotation-driven />

  <import resource="classes/MultilingualTranslation-service.xml"/>

   <bean id="finderController"
         class="uk.co.igindex.multilingualtranslation.application.rest.FinderController">
   </bean>

</beans>

1 个答案:

答案 0 :(得分:0)

你的问题发生在这里:

   @RequestMapping(value = "/categories", method = RequestMethod.GET, headers = "content-type=application/json")
   @ResponseBody
   public String getCategoryList() {
      StringBuilder sb = new StringBuilder();
      for (String s : finderCache.getCategory()) {
         sb.append(s + "\n");
      }
      return sb.toString();
   }

问题发生在finderCache.getCategory()。你的getCategory中没有任何项目。

请与调试器核实finderCache.getCategory()有哪些

如果这不是问题,请发布错误的logcat。