当前位置: 首页 > 知识库问答 >
问题:

使用spring@RequestMapping方法的Kotlin NoClassDefoundError

滕胜涝
2023-03-14

我试图使用Kotlin+IntelliJ学习一些spring的基础知识,但我遇到了一些麻烦。@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>
<?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>

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.invoke(abstractaccesslogvalve.620)org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:502)apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:684)org.apache.tomcat.util.net.aprendpoint$socketprocessor.dorun(aprendpoint.java:2521)WrappingRunnable.run(TaskThread.java:61)java.lang.Thread.Run(Thread.java:745)根本原因

共有1个答案

林修真
2023-03-14

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

有一个gradle插件可以将所有课程标记为“打开”:

buildscript{
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion")
    }
}
apply plugin: 'kotlin-spring'
 类似资料:
  • 字符串 模型 ModelandView 我不明白这些行动之间的区别。你能给我解释一下吗?

  • 我在Spring MVC中的controller类中有一个方法。

  • 问题内容: 我在春季有以下几点 但是,Spring会自动为/ hello /和/hello.*添加映射。如何完全匹配网址? 只有/ hello应该可以工作,其他任何都应该404 问题答案: 关闭后缀匹配()将解决您的问题,但是,如果在您的配置中使用它(实际上不是手动连接所有必需的基础结构bean),实际上并不是那么容易。在这种情况下,定义其他类型的bean 不会有任何效果。 您有两种选择: 删除将

  • 问题内容: 是否可以在一个方法上使用多个注释? Like : 问题答案: 有一个值参数,因此你应该能够指定多个值,如下所示:

  • 使用@RequestMapping注解的处理方法可以拥有非常灵活的方法签名,它支持的方法参数及返回值类型将在接下来的小节讲述。大多数参数都可以任意的次序出现,除了唯一的一个例外:BindingResult参数。这在下节也会详细描述。 Spring 3.1中新增了一些类,用以增强注解了@RequestMapping的处理方法,分别是RequestMappingHandlerMapping类和Requ

  • 我想知道上面哪一个更好/正确/使用最多,或者是什么。第一种是在@RequestMapping中使用值,另一种是使用路径。 两者都有效。只是想找出两者的区别。 提前谢谢!