How to set up a Java method service action

Last update:

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:

  1. Compile your Java class into a file, such as .jar, .war, .class, etc.
  2. Upload this file to the Sapho Server classpath, as defined in Sapho Server configuration or the application server itself.
  3. 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.

  1. Go to Settings (located in the hamburger menu) → Security
  2. 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:

  • java
  • javax
  • sapho
  • com.sapho

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:

  • java.lang.String
  • java.time.LocalDate
  • java.sql.Date.class
  • java.time.LocalTime.class
  • java.sql.Time.class
  • java.time.LocalDateTime.class
  • java.time.ZonedDateTime.class
  • java.util.Date
  • java.sql.Timestamp
  • java.lang.Boolean
  • boolean
  • java.math.BigInteger
  • java.lang.Long.class
  • long
  • java.lang.Integer
  • integer
  • java.lang.Short
  • short
  • java.lang.Byte
  • byte
  • java.math.BigDecimal.class
  • java.lang.Double
  • double
  • java.lang.Float
  • float
  • byte[]
  • java.lang.Void
  • void

Method return values are currently not supported and are ignored.

Technical note

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:

  1. Open the micro app for editing in Sapho Builder.
  2. Select the page that should include the trigger for invoking your custom Java method.
  3. Add a Button component to the page.
  4. Go to the Action section.
  5. Select Service Action for the Action property. 
  6. For the Service property, select Java Method
  7. Fill in the full name of your custom Java class including the namespace in the Java Class field. 
  8. Sapho Builder automatically loads all invokable methods of your custom Java class into the Java Methods dropdown. Select the one you want to use. 
  9. If the selected method has any input parameters, the SET PARAMETERS button will become active, allowing you to set the values of each parameter.