[groovy]Unexpected error on small script
EXAMPLE 1
Input header
name: counter value: 0
Script
request.setHeader("counter", request.getHeader("counter", Integer.class) + 1);
result = request.body
Error
Invalid groovy script: 'Unable to make field private static final long java.lang.Number.serialVersionUID accessible: module java.base does not "opens java.lang" to unnamed module @71ba6d4e'
EXAMPLE 2
Input body
{
"name": "John McClusky",
"age": 45,
"city": "York"
}
Script
import java.util.UUID
// Generate a random UUID
def uuid = UUID.randomUUID()
// Convert the UUID to a string
def uuidString = uuid.toString()
request.setHeader('uuid', uuidString)
result = request.body
Error
Invalid groovy script: 'Unable to make private java.util.UUID(byte[]) accessible: module java.base does not "opens java.util" to unnamed module @71ba6d4e'
These 2 examples should work now on Next.
Basically we added the following options when running java:
--add-opens java.base/java.lang=ALL-UNNAMED
--add-opens java.base/java.util=ALL-UNNAMED
Maybe in the feature we need to add more modules.
The 2 examples work fine on next now. The next new examples give an error or an unexpected response.
EXAMPLE 3
Input header
name: uuid value: 7cbf5a21-0e95-4df0-80c8-34f2c56d2032
Script
def headerName = 'uuid'
def headerValue = exchange.message.getHeader(headerName, String)
int charCount = headerValue.length()
Expected result (equal to groovy 3.0)
body: 36
EXAMPLE 4
Script
import groovy.json.JsonSlurper
def messageBody = request.getBody(String.class)
def slurper = new JsonSlurper()
def parsedJson = slurper.parseText(messageBody)
parsedJson.each { key, value ->
exchange.in.setHeader(key, value)
}
Error
Invalid groovy script: 'class [B cannot be cast to class [C ([B and [C are in module java.base of loader 'bootstrap')'
Example 3: Don't know what error you exactly get, but I got '36', as expected. Did you set the input header?
"uuid"="7cbf5a21-0e95-4df0-80c8-34f2c56d2032"
If there is not input header then you will get "Invalid groovy script: 'Cannot invoke method length() on null object'"
Example 4 was an dependency issue. This script can be retested.
Concerning example 3, the frontend shows the expected body. The header ''uuid' is in the incoming message. If you call the flow, the body stays empty.
The jsonslurper (example 4) works properly now.