简体   繁体   中英

Best way to setup Spring Boot + Angular to communicate with Facebook Marketing API

I am developing a Spring Boot backend and using the Facebook marketing SDK. On the frontend side, I have Angular 10. If I create eg a page or a campaign, I want to return the object to my frontend app and there I need to have the according to typescript class with all the properties of the campaign.

I tried the typescript-generator from Habarta but the Facebook java classes are complex and I get conflicting results during the generation process. For instance:

Parsing 'com.facebook.ads.sdk.ProductFeedRuleSuggestion' used in 'APIRequestGetSuggestedRules.lastResponse'
Warning: Multiple classes are mapped to 'APIRequestGetActivities' name. Conflicting classes: [class com.facebook.ads.sdk.AdAccount$APIRequestGetActivities, class com.facebook.ads.sdk.AdSet$APIRequestGetActivities]
...
[ERROR] Failed to execute goal cz.habarta.typescript-generator:typescript-generator-maven-plugin:2.25.695:generate (default-cli) on project simplephy: Execution default-cli of goal cz.habarta.typescript-generator:typescript-generator-maven-plugin:2.25.695:generate failed: Multiple classes are mapped to the same name. You can use 'customTypeNaming' or 'customTypeNamingFunction' settings to resolve conflicts or exclude conflicting class if it was added accidentally.

My plugin setup:

<plugin>
    <groupId>cz.habarta.typescript-generator</groupId>
    <artifactId>typescript-generator-maven-plugin</artifactId>
    <version>2.25.695</version>
    <executions>
        <execution>
            <id>generate</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>process-classes</phase>
        </execution>
    </executions>
    <configuration>
        <jsonLibrary>jackson2</jsonLibrary>
        <mapClasses>asClasses</mapClasses>
        <classes>
            <class>com.facebook.ads.sdk.Campaign</class>
        </classes>
        <excludeClasses>
            <excludeClass>com.facebook.ads.sdk.APINode</excludeClass>
        </excludeClasses>
        <outputKind>module</outputKind>
        <outputFileType>implementationFile</outputFileType>
    </configuration>
</plugin>

What would be the suiting way to generate all the necessary typescript classes from the Facebook java objects? Or is there any better way to send these fb objects between backend and frontend?

You need write customTypeNamingFunction, for example

<customTypeNamingFunction>
    function(name, simpleName) {
        <!-- DUPLICATES THAT HAVE TO BE RENAMED -->
        var duplicates = ['class', 'names', 'duplicated'];

        if (name.indexOf('$') !== -1) {
            var parent = name.split('$')[0];
            var parentParts = parent.split('.');
            return parentParts[parentParts.length-1]+simpleName;
        } 
        if (duplicates.indexOf(simpleName) !== -1) {
            var parts = name.split('.');
            var caps = parts[parts.length-2][0].toUpperCase() + parts[parts.length-2].slice(1);
            return caps+parts[parts.length-1];
        } 
    }
</customTypeNamingFunction>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM