最佳方式设置Spring Boot + Angular与Facebook营销API进行通信。

huangapple go评论71阅读模式
英文:

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

问题

我正在开发一个Spring Boot后端,并使用Facebook营销SDK。在前端方面,我使用的是Angular 10。如果我创建例如页面或广告系列,我希望将该对象返回到我的前端应用程序,并在那里我需要拥有相应的TypeScript类,其中包含广告系列的所有属性。

我尝试了Habarta的typescript-generator,但Facebook的Java类比较复杂,在生成过程中我会得到冲突的结果。
例如:

    正在解析在'APIRequestGetSuggestedRules.lastResponse'中使用的'com.facebook.ads.sdk.ProductFeedRuleSuggestion'
    警告:多个类被映射到'APIRequestGetActivities'名称。冲突的类:[com.facebook.ads.sdk.AdAccount$APIRequestGetActivities类,com.facebook.ads.sdk.AdSet$APIRequestGetActivities类]
    ...
    [错误] 无法执行目标cz.habarta.typescript-generator:typescript-generator-maven-plugin:2.25.695:generate(默认命令行):在目标cz.habarta.typescript-generator:typescript-generator-maven-plugin:2.25.695:generate上执行目标default-cli失败:多个类被映射到相同的名称。您可以使用'customTypeNaming'或'customTypeNamingFunction'设置来解决冲突,或者如果意外添加了冲突类,则可以将其排除。

我的插件设置:

    <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>

生成所有必要的TypeScript类以从Facebook的Java对象生成它们的合适方法是什么?或者在后端和前端之间发送这些Facebook对象有更好的方法吗?
英文:

I am developing a Spring Boot backend and using the Facebook marketing SDK. On the frontend side, I have Angular 10. If I create e.g. 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 &#39;com.facebook.ads.sdk.ProductFeedRuleSuggestion&#39; used in &#39;APIRequestGetSuggestedRules.lastResponse&#39;
Warning: Multiple classes are mapped to &#39;APIRequestGetActivities&#39; 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 &#39;customTypeNaming&#39; or &#39;customTypeNamingFunction&#39; settings to resolve conflicts or exclude conflicting class if it was added accidentally.

My plugin setup:

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

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?

答案1

得分: 1

&lt;customTypeNamingFunction&gt;
	function(name, simpleName) {
		&lt;!-- 必须重新命名的重复项 --&gt;
		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];
		} 
	}
&lt;/customTypeNamingFunction&gt;
英文:

You need write customTypeNamingFunction, for example

&lt;customTypeNamingFunction&gt;
	function(name, simpleName) {
		&lt;!-- DUPLICATES THAT HAVE TO BE RENAMED --&gt;
		var duplicates = [&#39;class&#39;, &#39;names&#39;, &#39;duplicated&#39;];

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

huangapple
  • 本文由 发表于 2020年8月25日 04:46:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63568535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定