Announcement Announcement Module
Collapse
No announcement yet.
Problems with @RequestMapping Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems with @RequestMapping

    I'm trying to implement a RESTful API using Spring 3.0 and @RequestMappings but I'm encountering some issues. The API involves using email addresses as part of a REST path which I can't get correctly mapped to a @PathVariable.

    My controller code:

    Code:
    @RequestMapping(value = "/{email}", method = RequestMethod.GET)
    public PersonBean getPerson(@PathVariable("email") String email) throws NotFoundException
    {
        if ("[email protected]".equals(email))
        {
            return JOE_BLOGGS;
        }
    
        throw new NotFoundException("Cannot find [" + email + "]");
    }
    If I submit the following URL:
    Code:
    http://localhost:8080/TestServlet/testing/[email protected]
    I get the following error:
    Code:
    com.test.testing.NotFoundException: Failed to find [joe.bloggs@example]
        at com.test.testing.PersonController.getPerson()
    If I add a trailing slash to the URL:
    Code:
    http://localhost:8080/TestServlet/testing/[email protected]/
    Something seems to get confused in Spring and it gets mapped to:
    Code:
    WARNING: No mapping found for HTTP request with URI [/TestServlet/testing/[email protected]/testing/joe.bloggs@example] in DispatcherServlet with name 'test'
    Similar mapping issues seem to occur if I replace the @ with a hyphen or something similar. Other paths below the email address (such as /testing/[email protected]/name) work fine.

    I'm not sure whether the issue is with my code or Spring (I'm currently on CI build 400). The controller annotations are great but unless I can solve this I'm going to have to ditch Spring and go back to plain old servlets and manual path managing

    Any help would be appreciated.

  • #2
    Enable debug logging and check where the .com gets removed. I suspect somewhere ni the DefaultAnnotationHandlerMapping or AnnotationMethodHandlerAdapter.

    Another thing you might want to try is to set the alwaysUseFullPath property on the HandlerMapping to true..
    Last edited by Marten Deinum; Sep 23rd, 2009, 08:01 AM.

    Comment


    • #3
      The '.com' is striped and parsed as a file extension for content negotiation. If the URI template is at the end of the url, you can override this behavior by defining the regular expression pattern for the template. However, content negotiation based on the file extension is no longer possible.

      Try changing your mapping to:
      Code:
      @RequestMapping(value = "/{email:.*}", method = RequestMethod.GET)

      Comment


      • #4
        Originally posted by sandrews
        Try changing your mapping to:
        Code:
        @RequestMapping(value = "/{email:.*}", method = RequestMethod.GET)
        Thanks this resolved my mapping problem. The API that I am implementing involves sending/receiving XML so content negotiation is not an issue for me.

        Can we get this added to the reference documentation so that it doesn't trip up other users?

        Comment


        • #5
          I second the request for more info about this notation. I just went through everything in the RC1 doc about "@RequestMapping", and I didn't see anything like this "keyword:..." syntax for the @RequestMapping value. I can't even tell what this is doing.

          Comment

          Working...
          X