[SalesForce] Exposing apex class for both SOAP and REST API

I read this post and learned that same class can be used to exposed for consuming SOAP and REST API: Exposing same apex method via SOAP and REST API?

My question is: Which of the below is the recommended approach?

  1. Single method that be can be used for both SOAP and REST like below:

    @RestResource(urlmapping = '/someUrl/*')
    global class SOAPRESTClass {
        @HttpGet
        webservice static void myMethod(){`
            System.debug('From myMethod');
        }
    
    }
    
  2. Create 2 methods one can be used for SOAP and other can be used as REST.

What is the best practice. Are there any side effects for option 1?

Best Answer

Generally speaking, if you can use both REST and SOAP in the same line of code, you may as well go for it. It's less unit testing, fewer lines of code, and better API unification. I would imagine that there's a few situations where having distinct REST/SOAP methods might make sense, perhaps if you have more than 32 parameters (the limit for Apex method parameters), but in the majority of cases, I would recommend using the pattern you've described if you want the flexibility of both REST and SOAP. As an added bonus, you get to support REST XML and JSON in one broad sweep. You'll want to test your API calls in each of the three modes (SOAP, REST XML, and REST JSON) to make sure it works as you expect, but the principle is sound. There really isn't a "best practice" for this sort of thing. If you need both REST and SOAP, use it. If you only need REST or SOAP, use whichever is appropriate for your case. It's usually easy enough to add REST or SOAP support if you need it later.