0

I have a web application with Spring MVC.

web.xml

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

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

spring controller method:

class RealmInfoController{

    @ResponseBody
    @RequestMapping(value = {"/companies/{companyId}/realms/{realmName}"})
    public RealmInfo realmInfo(@PathVariable long companyId, @PathVariable String realmName)

Handler match:

http://localhost:6122/context/companies/15877/realms/firstRealm

When the server gets this url, the spring servlet gets called. but it cannot match the controller method.

But if I change the request mapping to "/{companyId}/realms/{realmName}" then it matches the controller method. But it is not nice to define the url mapping without '/companies'. Can Spring be instructed in some way to look for match including the url pattern specified in the servlet?

Thanks.

1
  • 2
    Because in the <url-pattern> there is /companies/* path, you are telling the container to invoke DispatherServlet whenever the URL matches the defined pattern. So in the controller you MUST NOT include /companies/ part in the @RequestMapping - it is already included. In other words, not including /companies/ part in the @RequestMapping is the right way of doing it in your case. Commented Feb 13, 2013 at 15:51

1 Answer 1

1

if you want to use "companies" in request mapping you should map your dispatcher servlet to the root:

<url-pattern>/*</url-pattern>
Sign up to request clarification or add additional context in comments.

2 Comments

i could not use <url-pattern>/*</url-pattern> or <url-pattern>/</url-pattern> because is hides other servlets and error-page definitions.
@Rag Then just do as you were doing it before. As I wrote you in the comment above - not including /companies/ in the @RequestMapping is the right way in your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.