gpt4 book ai didi

java - 具有多个 Controller 的单个 DispatcherServlet

转载 作者:行者123 更新时间:2023-12-02 08:34:31 25 4
gpt4 key购买 nike

我正在尝试使用 Spring MVC 3.0 创建一些安静的 Web 服务。我目前遇到一个问题,即我的 2 个 Controller 中只有 1 个在任何给定时间都可以工作。事实证明,按字母顺序排序时,无论哪个类排在前面,都可以正常工作。我得到的错误是:

handleNoSuchRequestHandlingMethod 找不到 Servlet 请求的匹配处理程序方法:路径“/polinq.xml”,方法“GET”,参数映射[[空]]

我之前也收到过一条非常类似的消息,只不过 map 不是空的,而是类似 map[v-->String(array)] 的东西

无论消息如何,当前 LocationCovgController 都可以工作,而 PolicyInquiryController 则不能。如果我将 PolicyInquiryController 更改为 APolicyInquiryController,那么它将开始正常运行,并且 LocationCovgController 将停止工作。

任何帮助将不胜感激。

非常感谢,杰里米

下面提供的信息包括两个 Controller 类的框架以及定义如何设置 spring 的 servlet 配置文件。

Controller 1

package org.example;
@Controller
@RequestMapping(value = "/polinq.*")
public class PolicyInquiryController {

@RequestMapping(value = "/polinq.*?comClientId={comClientId}")
public ModelAndView getAccountSummary(
@PathVariable("comClientId") String commercialClientId) {
// setup of variable as was removed.
ModelAndView mav = new ModelAndView("XmlView",
BindingResult.MODEL_KEY_PREFIX + "accsumm", as);
return mav;
}
}

Controller 2

package org.example;

@Controller
@RequestMapping(value = "/loccovginquiry.*")
public class LocationCovgController {
@RequestMapping(value = "/loccovginquiry.*method={method}")
public ModelAndView locationCovgInquiryByPolicyNo(
@PathVariable("method")String method) {
ModelAndView mav = new ModelAndView("XmlView",
BindingResult.MODEL_KEY_PREFIX + "loccovg", covgs);
return mav;
}
}

Servlet 配置

<context:component-scan base-package="org.example." />

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:order="0">
<property name="mediaTypes">
<map>
<entry key="atom" value="application/atom+xml"/>
<entry key="xml" value="application/xml"/>
<entry key="json" value="application/json"/>
<entry key="html" value="text/html"/>
</map>
</property>

<property name="defaultContentType" value="text/html"/>
<property name="ignoreAcceptHeader" value="true"/>
<property name="favorPathExtension" value="true"/>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
</list>
</property>
</bean>

<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />

<bean id="XmlView" class="org.springframework.web.servlet.view.xml.MarshallingView">
<property name="marshaller" ref="marshaller"/>
</bean>

<oxm:jaxb2-marshaller id="marshaller">
<oxm:class-to-be-bound name="org.example.policy.dto.AccountSummary"/>
<oxm:class-to-be-bound name="org.example.policy.dto.InsuredName"/>
<oxm:class-to-be-bound name="org.example.policy.dto.Producer"/>
<oxm:class-to-be-bound name="org.example.policy.dto.PropertyLocCoverage"/>
<oxm:class-to-be-bound name="org.example.policy.dto.PropertyLocCoverages"/>
</oxm:jaxb2-marshaller>

最佳答案

您应该使用@RequestParam而不是@PathVariable来绑定(bind)请求参数(即之后的参数,您也不用需要将它们包含到 @RequestMapping 中)。此外,您不需要在类级别为此配置使用 @RequestMapping:

Controller 1

package org.example;

@Controller
public class PolicyInquiryController {
@RequestMapping(value = "/polinq")
public ModelAndView getAccountSummary(
@RequestParam("comClientId") String commercialClientId) {
// setup of variable as was removed.
ModelAndView mav = new ModelAndView("XmlView",
BindingResult.MODEL_KEY_PREFIX + "accsumm", as);
return mav;
}
}

Controller 2

package org.example;

@Controller
public class LocationCovgController {
@RequestMapping(value = "/loccovginquiry")
public ModelAndView locationCovgInquiryByPolicyNo(
@RequestParam("method") String method) {
ModelAndView mav = new ModelAndView("XmlView",
BindingResult.MODEL_KEY_PREFIX + "loccovg", covgs);
return mav;
}
}

关于java - 具有多个 Controller 的单个 DispatcherServlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2315497/

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