带有spring @RequestMapping方法的{Kotlin NoClassDefFoundError

时间:2017-07-02 00:43:16

标签: spring intellij-idea kotlin

我尝试使用Kotlin + IntelliJ学习一些春季基础知识,但我遇到了一些麻烦。 @RequestMapping带注释的方法(带参数)会导致异常,而Java等效工作则完全正常。 Kotlin代码工作正常,但只有没有" processFormTwo"方法(将工作Java代码转换为kotlin也没有帮助)。

以下是代码:

@Controller
class HelloWorldController {

    @RequestMapping("/showForm")
    fun showForm() = "helloworld-form"

    // this one works fine
    @RequestMapping("/processForm")
    fun processForm() = "helloworld"

    //this one doesn't
    @RequestMapping("/processFormTwo")
    fun processFormTwo(request: HttpServletRequest, model: Model): String {


        var theName = request.getParameter("studentName")


        theName = theName.toUpperCase()

        val result = "Yo! " + theName


        model.addAttribute("message", result)

        return "helloworld"
    }
}

配置:

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

    <!-- Step 3: Add support for component scanning -->
    <context:component-scan base-package="com.luv2code.springdemo" />

    <!-- Step 4: Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Step 5: Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">

    <display-name>spring-mvc-demo</display-name>

    <!-- Spring MVC Configs -->

    <!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

错误日志:

  

例外

     

javax.servlet.ServletException:servlet调度程序的Servlet.init()   扔了例外     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)     org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)     org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)     org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)     org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:684)     org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.doRun(AprEndpoint.java:2521)     org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.run(AprEndpoint.java:2510)     java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     java.lang.Thread.run(Thread.java:745)根本原因

     

org.springframework.beans.factory.BeanCreationException:错误   用名称&#39; helloWorldController创建bean&#39;:无法内省   bean类[com.luv2code.springdemo.mvc.HelloWorldController] for   查找方法元数据:无法找到它依赖的类;   嵌套异常是java.lang.NoClassDefFoundError:   科特林/ TypeCastException     org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:269)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1118)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1091)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)     org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)     org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)     org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)     org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)     org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)     org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)     org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)     org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)     org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)     org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)     org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)     org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:171)     javax.servlet.GenericServlet.init(GenericServlet.java:158)     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)     org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)     org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)     org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)     org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:684)     org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.doRun(AprEndpoint.java:2521)     org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.run(AprEndpoint.java:2510)     java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     java.lang.Thread.run(Thread.java:745)根本原因

     

java.lang.NoClassDefFoundError:kotlin / TypeCastException     java.lang.Class.getDeclaredMethods0(本机方法)     java.lang.Class.privateGetDeclaredMethods(Class.java:2701)     java.lang.Class.getDeclaredMethods(Class.java:1975)     org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:613)     org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:524)     org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:510)     org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:247)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1118)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1091)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)     org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)     org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)     org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)     org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)     org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)     org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)     org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)     org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)     org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)     org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)     org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)     org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:171)     javax.servlet.GenericServlet.init(GenericServlet.java:158)     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)     org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)     org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)     org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)     org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:684)     org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.doRun(AprEndpoint.java:2521)     org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.run(AprEndpoint.java:2510)     java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     java.lang.Thread.run(Thread.java:745)根本原因

     

java.lang.ClassNotFoundException:kotlin.TypeCastException     org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)     org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)     java.lang.Class.getDeclaredMethods0(本机方法)     java.lang.Class.privateGetDeclaredMethods(Class.java:2701)     java.lang.Class.getDeclaredMethods(Class.java:1975)     org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:613)     org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:524)     org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:510)     org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:247)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1118)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1091)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)     org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)     org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)     org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)     org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)     org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)     org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)     org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)     org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)     org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)     org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)     org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)     org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:171)     javax.servlet.GenericServlet.init(GenericServlet.java:158)     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)     org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)     org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)     org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)     org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:684)     org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.doRun(AprEndpoint.java:2521)     org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.run(AprEndpoint.java:2510)     java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

尝试将Controller类和函数标记为打开

有一个gradle插件,用于将所有类标记为打开:

buildscript{
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion")
    }
}
apply plugin: 'kotlin-spring'