0
votes

I have a Spring REST controller:

@RestController
@RequestMapping("v1/employee")
public class controller1 {
 getMethod(@PathVariable String param1, String param2){}
 postMethod(){}
}

I want to introduce a new endpoint version v2 that resolves the params: param1,param2 and forward to the v1 request urls.

I want the new clients to be able to call v2 while the old clients are still able to call v1 endpoints. I looked at Interceptors and Filters. So I should either,

  1. forward the requests to v1 or,
  2. call controller methods internally

Is there a better way to do this? Thanks.

1

1 Answers

1
votes

It really depends on the nature of your application

first of all, controllers are supposed to only encapsulate HTTP related stuff, so probably the business logic is implemented in Services.

If so, then there is no need to redirect the request if you're talking about the same java application, it's better just to inject the service to the new controller and call it.

class OldController {
   MyService srv;

   @GetMapping(...)
   public oldGet() {
       srv.doSomething();
   }
}

class NewController {
   MyService srv;

   @GetMapping(...)
   public newGet() {
       srv.doSomething();
       //or 
       srv.doSomethingElse(); 
   }
}

Now as for the routing, you can even do it externally (like in some kind of proxy that does url rewriting for you and redirects to the correct controller that can even reside on the different server. Solutions for these are:

  • NGNIX
  • Zuul
  • HA-Proxy

there are others