开发者

Single DispatcherServlet with Multiple Controllers

I am trying to create some restful web services using Spring MVC 3.0. I currently have an issue that only 1 of my 2 controllers will work at any given time. As it turns out, whichever class comes first when sorted alphabetically will work properly. The error I get is:

handleNoSuchRequestHandlingMethod No matching handler method found for servlet request: path '/polinq.xml', method 'GET', parameters map[[empty]]

I had a very simliar message earlier also, except instead of the map being empty it was something like map[v-->String(array)]

Regardless of the message though, currently the LocationCovgController works and the PolicyInquiryController doesn't. If I change the change of the PolicyInquiryController to APolicyInquiryController, then it will start funcitoning properly and the LocationCovgController will stop working.

Any assistance would be greatly appreciated.

Thank you very much, Jeremy

The information provided below includes the skeleton of both controller classes and also the s开发者_开发技巧ervlet config file that defines how spring should be setup.

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 Config

<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>


You should use @RequestParam instead of @PathVariable to bind request parameters (i.e. parameters which come after ?, you also don't need to include them into @RequestMapping). Also, you don't need @RequestMapping at class level for this configuration:

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;
    }
}


The problem I see is the annotations at class level, for your case try removing them and just use the annotations at method level. Also if you got the error again, please post the stacktrace.

Also use RequestParam to get param values and PathVariable to access the pathvariables. Simply for the URL \home\user\{username}\?p=rand, to get the username use pathvariable and use requestparam to get p


I stopped and started websphere and the code started to work. Previously I had simply "published" the changes to webpshere or "cleaned" the project within RAD (Rational Application Developer) and it appears that neither of those options are completely refreshing the code on the server. Right now the code works 100% of the time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜