Contents
  1. 1. Test APIs in Your Local Environment
  2. 2. Add API Configurations to Your Project
    1. 2.1. Add Configurations
    2. 2.2. Define the Configuration Bean
    3. 2.3. Access Your Configurations
  3. 3. Write Code
    1. 3.1. Define the API Interfaces
    2. 3.2. Define Constants
    3. 3.3. Write the API Implementing Class
    4. 3.4. Write Parameter Builders and Result Handlers
    5. 3.5. Use Your Interface

Test APIs in Your Local Environment

Before integrating third-party APIs in your web application, it’s better to test the APIs with an API tool like Postman to ensure the APIs work properly in your local environment. It can also let you get familiar with the APIs.

Add API Configurations to Your Project

There are some common configurations of third-party APIs you need to add to your project. For example, authorization information (appId, appSecret), API URL prefix, and so on.

Add Configurations

Add the configurations to your spring boot configuration file application.yaml:

1
2
3
4
5
6
7
thirdParty:
thirdParty1:
appId:
appSecret:
apiUrlPrefix:
thirdParty2:
...

Define the Configuration Bean

Add a spring bean to receive the configurations:

YourThirdPartyConfig.java

1
2
3
4
5
6
7
8
9
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "thirdParty.yourThirdPartyName")
@Data
public class YourThirdPartyConfig {
protected String appId;
protected String appSecret;
protected String apiPrefix;
}

Access Your Configurations

Access the configurations by @Autowired it

1
2
@Autowired
private YourThirdPartyConfig yourThirdPartyConfig;
1
2
String appId = yourThirdPartyConfig.appId;
String appSecret = yourThirdPartyConfig.appSecret;

Write Code

Define the API Interfaces

Here we use the example of the social login API service to demonstrate.

1
2
3
4
public interface SocialLoginService {
JSONObject func1(JSONObject params);
JSONObject func1(JSONObject params);
}

Why use JSONObject as parameters and return types of methods of the interface?

Different third-party API service platforms have different data structures. The parameters and return types of methods need to be extensible.

Define Constants

Keys for API parameters

1
2
3
4
5
public class Auth0Key {
public static final String CODE = "code";
public static final String MSG = "msg";
public static final String USER_NAME = "user_name";
}

API request URIs

1
2
3
4
public class Auth0Url {
public static final String LOGIN = "/login";
public static final String GET_USER_INFO = "/user/getInfo";
}

Write the API Implementing Class

1
2
3
4
5
6
7
8
9
10
11
12
@Service
public class Auth0SocialLoginService implements SocialLoginService {
@Autowired
private Auth0Config auth0Config;

public JSONObject func1(JSONObject params) {
...
}
public JSONObject func2(JSONObject params) {
...
}
}

Common third-party APIs and their class names

API Service Interface Name Implementation Name
Cloud Object Storage COSService AwsCOSService
AzureCOSService
Push Notification APIs PushNotificationService OneSignalPushService
Social Media Login APIs SocialLoginService Auth0SocialLoginService
FirebaseSocialLoginService

Write Parameter Builders and Result Handlers

Parameter builders

1
2
3
4
public interface SocialLoginParamBuilder {
JSONObject getFunc1Param(Xxx entity);
JSONObject getFunc2Param(Xxx entity);
}
1
2
3
4
5
@Component
public class Auth0SocialLoginParamBuilder {
JSONObject getFunc1Param(Xxx entity) {...}
JSONObject getFunc2Param(Xxx entity) {...}
}

Result handlers

1
2
3
4
public interface SocialLoginResultHandler {
Vo handleFunc1Result(JSONObject result);
Vo handleFunc2Result(JSONObject result);
}
1
2
3
4
5
@Component
public class Auth0SocialLoginResultHandler {
Vo handleFunc1Result(JSONObject result) {...}
Vo handleFunc2Result(JSONObject result) {...}
}

Use Your Interface

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Autowired
@Qualifier("auth0SocialLoginService")
private SocialLoginService socialLoginService;
// or
@Autowired
private SocialLoginService auth0SocialLoginService; // The SocialLoginService object name must same as the implementing class name and the first character should be lowercase.

@Autowired
@Qualifier("auth0SocialLoginParamBuilder")
private SocialLoginParamBuilder socialLoginParamBuilder;

@Autowired
@Qualifier("auth0SocialLoginResultHandler")
private SocialLoginResultHandler socialLoginResultHandler;
1
2
3
JSONObject params = auth0SocialLoginParamBuilder.getFunc1Param(entity);
JSONObject result = auth0SocialLoginService.func1(params);
Vo vo = auth0SocialLoginResultHandler.handleFunc1Result(result);
Contents
  1. 1. Test APIs in Your Local Environment
  2. 2. Add API Configurations to Your Project
    1. 2.1. Add Configurations
    2. 2.2. Define the Configuration Bean
    3. 2.3. Access Your Configurations
  3. 3. Write Code
    1. 3.1. Define the API Interfaces
    2. 3.2. Define Constants
    3. 3.3. Write the API Implementing Class
    4. 3.4. Write Parameter Builders and Result Handlers
    5. 3.5. Use Your Interface