How to set up a Java method service action
Category: Sapho Builder
Audience: Developers building Sapho micro apps
Although the Sapho platform is quite comprehensive, there can still be situations that require more business logic capabilities. For these cases, you can create your own Java code, plug it into Sapho Server, and then link to it as a button action in Sapho Builder.
Bind Java class to Sapho Server
Follow those steps to bind your Java class to Sapho Server:
- Compile your Java class into a file, such as .jar, .war, .class, etc.
- Upload this file to the Sapho Server classpath, as defined in Sapho Server configuration or the application server itself.
- Restart your application server.
At this point, your Java class and its methods will be available in Sapho Builder.
Java class whitelist
For security purposes, you can limit which Java classes will be accessible in Sapho Builder.
- Go to Settings (located in the hamburger menu) → Security
- You can specify secure classes and/or namespaces in the Java Class Whitelist text area. By default, all Java classes in the classpath are permitted (indicated by the asterisk wildcard), but you can list specific classes by typing the full class name including the namespace (Java package). Listing only the namespace will whitelist all classes within that namespace. Use a comma, space, or newline to separate any additional class names or namespaces.
Java class blacklist
Sapho includes a built-in blacklist of certain Java classes that cannot be used due to potential security risks. The list includes the following namespaces:
The Java class blacklist is an internal Sapho setting and cannot be configured.
Supported method parameter data types
The following data types are supported in method parameters:
Method return values are currently not supported and are ignored.
The method must be public and can be invoked as a static method or an instance method. When an instance method is called, each call means that a new instance of the class is created.
Set up service action
Follow these steps to use your custom Java class methods within micro apps:
- Open the micro app for editing in Sapho Builder.
- Select the page that should include the trigger for invoking your custom Java method.
- Add a Button component to the page.
- Go to the Action section.
- Select Service Action for the Action property.
- For the Service property, select Java Method.
- Fill in the full name of your custom Java class including the namespace in the Java Class field.
- Sapho Builder automatically loads all invokable methods of your custom Java class into the Java Methods dropdown. Select the one you want to use.
- If the selected method has any input parameters, the SET PARAMETERS button will become active, allowing you to set the values of each parameter.