gpt4 book ai didi

java - 使用 MultiActionController 定义 Spring Rest API

转载 作者:行者123 更新时间:2023-12-01 14:47:56 27 4
gpt4 key购买 nike

我正在致力于在 JBoss servlet 引擎中使用 Java/Spring/Maven 并利用 MultiActionController 来设置 REST api。

什么I want to do is to group requests to a Single controller例如:所有/requestA goes to Controller a ,有几种方法可以满足不同的请求,例如-/requestA/add.do goes to add-function

  • /requestA/delete.do goes to delete-function.

我一直在阅读 Spring 文档: http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch18s02.htmlhttp://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch16s03.html#mvc-controller-multiaction

但是我在 xml 中的调度程序配置中遇到了一些问题。有谁知道如何在 Spring MVC 中实现这一点的好例子。

springapp-servlet.xml

    <?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean name="/test/*.do" class="org.test.TestControllerA" />
</beans>

web.xml

   <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springTest</display-name>

<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

和 Java Controller

    public class TestControllerA extends MultiActionController {

@RequestMapping("/TEST")
public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("Test42");
return new ModelAndView();
}
}

最佳答案

您缺少 servlet 映射

<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

您的网址具有扩展名*.do,但未映射到springapp。因此,将上述配置添加到您的 web.xml

关于java - 使用 MultiActionController 定义 Spring Rest API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15221685/

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