1

I'm trying to implement endpoint hierarchy in one class. I want to make something like this:

@RestResource(urlMapping = '/endpoint')
global with sharing class Class1{

    @HTTPGet
    global static void methodForGet(){
    }

    @RestResource(urlMapping = '/endpoint/v1')
    global with sharing class Class2{

        @HTTPGet
        global static void methodForGet(){
        }
}

Will SF let me do something like that and how it should be implemented?

2 Answers 2

3

@RestResource must be declared on a top-level class. If you want different versions to co-exist in the same class, you can write a router.

@RestResource(urlMapping='/endpoint/*')
global class EndpointHandler {
    @HttpGet global static void doGet() {
        if(RestContext.request.requestURI.endsWith('endpoint')) {
          // call "version 0"
        } else if(RestContext.request.requestURI.endsWith('v1')) {
          // call "version 1"
        } else if...

Typically, you'll probably want to instead keep each version in a separate top-level class, and write a utility class to avoid code duplication.

0

You are going to need a separate top-level Class for each endpoint you want to expose as an @RestResource. I'm going to recommend that you manage versions in the same way. That way v1/endpoint and v2/endpoint can live side by side without a mandatory hard cut-over date.

2
  • It won't work for me. I need to provide native names to endpoint and there will be over 10 of them, so if I won't have technical opportunity to implement endpoint hierarchy in one class, then I'll need to add another parameter to request(
    – neckobik
    Commented Aug 1, 2017 at 13:04
  • You could set them up like sub-resources then, and parse the requestURI as suggested by @sfdcfox Commented Aug 1, 2017 at 14:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .