Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide Aplicity to use URI templates in Interceptor Definitons [SPR-9595] #14229

Closed
spring-projects-issues opened this issue Jul 14, 2012 · 3 comments
Assignees
Labels
in: web status: declined type: enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jul 14, 2012

Adib Saikali opened SPR-9595 and commented

There does not seem to be a way to define interceptors that understand URI templates, which makes it very hard to perform common operations when using annotation driven controllers.


Affects: 3.1 GA

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jul 18, 2012

Rossen Stoyanchev commented

Can you provide more detail or describe an example case?

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Sep 10, 2012

Rossen Stoyanchev commented

Even if supported URI variables in interceptor mappings, there is no way to pass them through the HandlerInterceptor contract.

One option may be to use a @ModelAttribute method, which provides access to the path variables of the mapped controller method. Also you can gain access to all extracted URI variables (see HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE).

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Sep 13, 2012

Adib Saikali commented

Here is the use case to support reopening this ticket. Suppose that a REST api has a lot different controllers that share many common parts to the URL structure.

/api/{userId}/company/{companyId}/files/{fileId} (GET, PUT, DELET, POST without {fileid} 
/api/{userId}/company/{companyId}/news (GET,POST)
/api/{userId}/company/{companyId}/news/{newsPostId} (GET, PUT, DELETE)
/api/{userId}/company/{companyId}/contact

/api/{userId}/contacts/ (ROOT of many different sub urls for dealing with a specific users contacts)
/api/{userId}/preferences 
/api/{userId}/someOtherRoot/ (ROOT of many other subfolders)

As you you can see every URL has {userId} and many URLs and thus many controllers have another top level id such as {companyId}

I would like to be able to write an interceptor which does the following lines.


  @Interceptor 

 public class SomeInterceptor {
      
      // this method gets called first
      @RequestMapping(value="/api/{userId}", method=GET,POST,DELETE,PUT)
      public void ConfigureUserObject(@PathVariable("userId") Integer userId, WebRequest webRequest)
      {
           UserObject userObject = // go the db or cache and turn the userId into a a UserObject 
           webRequest.setAttribute("userObject",userObject);
      }

      // this method gets called second
      @RequestMapping(value="/api/{userId}/company/{companyId}", method=GET,POST,DELETE,PUT)
      public void ConfigureUserObject(@PathVariable("companyId") Integer companyId, WebRequest webRequest)
      {
           CompanyObjecc companyObject = // go the db or cache and turn the userId into a a UserObject 
           webRequest.setAttribute("company",company);
      }

  }
  public class FooCompanyController 
  {
     @RequsetMapping(value="/api/{userId}/company/{companyId}/foo", .... etc)
     public void getFoo(@RequestAttribute("userObject") UserObject userObject, @RequestAttribute Company company)
     {
           // key thing is that the foo controller gets a UserObject, and Company rather than just two 
           // integers. and given that every controller has a {userId} and a {companyId} in its path this 
           // makes the company controllers simple to implement than what can currently be done with 
           // Spring 3.1
     } 
  }

What i am really wanting is an easy way to turn common path variables into strongly typed objects in one place and not having to do it in every controller.

I know about Message Convertors but they seems overkill for something like this as i have to write an interface to go from String to Object and from Object to String. In the use case I am proposing I really just want spring to do the conversion of say {userId} into an integer and then I am only interested to go from integer --> Object and have no interest in going from Object --> Integer

Sorry not sure why the code formatting above does not work.

@spring-projects-issues spring-projects-issues added status: declined type: enhancement in: web labels Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web status: declined type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants