Announcement Announcement Module
Collapse
No announcement yet.
Differentiating url with Path parameters Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Differentiating url with Path parameters

    We are working on a REST based service and we are using Spring MVC for the same. We are facing a url to method resolution problem. This is roughly what we are trying to do

    Suppose we have persons with their pets

    //Class level request mapping
    @RequestMapping("/persons")

    // Mapping to access a specific person inside one of the methods
    @RequestMapping(value="/{personId}", method= RequestMethod.GET
    //.... getPerson method


    // Mapping to access a specific person's pets inside one of the methods
    @RequestMapping(value="/{personId}/pets", method= RequestMethod.GET
    // getPersonPets method



    If the request comes as "/persons/P12323233" where P12323233 is person id, it resolves to the getPerson method.
    If the request comes as "/persons/P12323233/pets" where P12323233 is person id, it resolves to the getPersonPets method.

    So everything is fine till now. But
    If the request comes as "/persons/pets, the request resolves to the getPerson method. While we can handle this as an error case inside in the getPerson method, we are trying to check whether there is anyway of resolving this call to getPersonPets method.

    We are still debating whether the right place to handle this case is getPerson or getPersonPets method. That debate aside, we wanted to know whether it is even technical feasible to achieve resolution to the getPersonPets method.

    Appreciate any help.

  • #2
    Yuo can add the mapping for /persons/pet to the getPersonsPet method that way it would resolve to that method.

    However the problem is broader IMHO, what do you do when people enter invalid URLs? This is just a single case but in your app you probably are going to have multiple of these cases... What are you going to do with that.

    Comment


    • #3
      Using multipe mapping it resolved the problem of going into wrong methods. But when I tried invoking /persons/pets, it gave me a spring exception saying the path variable not provided.

      Comment


      • #4
        Well obviously because that is part of your method signature, you can set a default value and check if that value is set.

        Comment


        • #5
          Broader problem

          I agree that there is a broader problem to resolve. We are using the exception handling approach which Spring MVC provides for that. Even in this case we want to handle it as a error scenario... We just wanted to figure out where and how we should handle this scenario.

          I guess the true problem is that we tried to be smart about handling one specific typo "pets" because we had some significance for it in our mind... But without the context of which person we are dealing with, pets has no real meaning. It is as good as querying a person with id "pets" which would potentially lead to a 404. If we treat it that way things will be simpler.

          Thanks.

          Comment

          Working...
          X